Send email in C# without configuring username and password

5.3k views Asked by At

In a scenario, I need to send emails to the users. But the problem is that I dont want to hardcode or configure my password and username for email. If anyone aware of PHP MAILER, it does support this facility where one does not need to give his own username and password for sending emails. And these emals can be directly send through localhosts. But this wont work with c#

The question is can we do same in c#? If yes then how? because I have searched almost everywhere but count find anything except that we need to find the SMTP client which can send mail without verifying the credentials.

4

There are 4 answers

3
Ole Albers On

You always can save username and password encrypted in the config.file.

Use this command to encrypt any section from the config %windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis -pef "connectionStrings" C:\documents and settings\bob\projects\myproject

Be aware, that the encryption is MACHINE-Specific. So you must do that on the target machine

More details on MSDN: http://msdn.microsoft.com/en-us/library/dtkwfdky(v=vs.100).aspx

0
Marco On

Of course this can be done, but like you said:

emals can be directly send through localhosts

In which case there is an smtp server present and preconfigured to allow for anonymous access / default credentials. The same goes for C# or any .Net applicaiton for that matter. If you have an SMTP server present, which will allow to send out emails for your machine you are good to go and do not need to authenticate.

0
Patrick Hofman On

You can use Windows authentication for example by setting the credentials to CredentialCache.DefaultNetworkCredentials:

SmtpClient smtpClient = new SmtpClient();
smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
0
JimmyB On

You can always roll your own SMTP implementation, which is not very hard to do, or get a 3rd party library which features SMTP.

You'd then have to do a DNS lookup for the 'mx' record of the destination domain (could be like 'example.org' -> 'mx01.example.org'), connect to that mx server and do your SMTP conversation with it.

This "should" officially work, but for SPAM filter reasons a given mail server may not be willing to accept an SMTP connection from an unknown/untrusted or not-whitelisted host. In that case you will have to go via a trusted MTA, which basically implies that this one will require authentication.