I\'m getting strange occurance on our servers when I am trying to send an email using SmtpClient class via an ASP MVC3 project. This is the code I am using.
Had the same issue - the credentials from ASP.Net were never going to be something that could send e-mail in my environment. So I figured out this path through the mess (which also includes the possibility that NTLM doesn't always work right and that I was putting the mail configuration info in web.config):
System.Net.Configuration.SmtpSection section = ConfigurationManager.GetSection("system.net/mailSettings/smtp") as System.Net.Configuration.SmtpSection;
// set up SMTP client
SmtpClient smtp = new SmtpClient();
System.Net.CredentialCache myCache = new System.Net.CredentialCache();
NetworkCredential myCred = new NetworkCredential(section.Network.UserName, section.Network.Password);
string host = section.Network.Host;
int port = section.Network.Port;
// do this because NTLM doesn't work in all environments....
if (myCred != null)
{
myCache.Add(host, port, "Digest", myCred);
myCache.Add(host, port, "Basic", myCred);
myCache.Add(host, port, "Digest-MD5", myCred);
myCache.Add(host, port, "Digest MD5", myCred);
myCache.Add(host, port, "Plain", myCred);
myCache.Add(host, port, "Cram-MD5", myCred);
myCache.Add(host, port, "Cram MD5", myCred);
myCache.Add(host, port, "Login", myCred);
//myCache.Add(host, port, "NTLM", myCred);
}
smtp.Credentials = myCache;
smtp.UseDefaultCredentials = false;
//smtp.EnableSsl = true;
Depending on your configuration, you might need to uncomment the last line.