.NET SMTP Client - Client does not have permissions to send as this sender

后端 未结 8 1754
不思量自难忘°
不思量自难忘° 2020-12-16 12:03

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.

         


        
8条回答
  •  醉梦人生
    2020-12-16 12:23

    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.

提交回复
热议问题