How to send emails in .NET part 11: credentials
September 10, 2014 Leave a comment
In the previous part in this mini-series we looked at how to handle exceptions. We’ll briefly look at authentication in this post.
Some SMTP servers require you to provide a username and password in order to send emails.
You can set the default network credentials as follows:
string smtpServer = "mail.blahblah.com"; SmtpClient client = new SmtpClient(smtpServer); client.UseDefaultCredentials = true;
Another way to achieve the same is the following:
client.Credentials = CredentialCache.DefaultNetworkCredentials;
…where CredentialCache is found in the System.Net namespace.
You can also set the username and password manually as follows:
client.Credentials = new NetworkCredential("name", "password");
Read all posts related to emailing in .NET here.