发送Mail Asp.Net时出现“身份验证失败”错误

时间:2015-10-12 08:02:45

标签: c# asp.net email ssl smtp

搜索了许多网站后仍未找到答案

我正在使用VS2010(Frame work 4.0)和SQL 2012,我们正在使用交换服务器....相同的邮件配置在java应用程序中工作但不在c#中工作

按钮点击代码是:

    try
    {         
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "mail.myorganization.com"
        smtp.Port = "587";
        smtp.Credentials = new System.Net.NetworkCredential("abc@myorganization.com", "password");
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;        
            smtp.EnableSsl = true;
        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("abc@myorganization.com");                  
        msg.To.Add(new MailAddress("receiver@something.com"));         
        msg.Subject = "Test";
        msg.Body = "Test mail";
        smtp.Timeout = 60000;
        smtp.Send(msg);
        result = true;
    }
    catch (Exception ex)
    {

    }

我的异常错误是

System.Net.Mail.SmtpException was caught
  HResult=-2146233088
  Message=Authentication failed.
  Source=System
  StackTrace:
       at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
       at System.Net.Mail.SmtpClient.GetConnection()
       at System.Net.Mail.SmtpClient.Send(MailMessage message)
       at MailConfiguration.TestMail() in

1 个答案:

答案 0 :(得分:0)

我有一个非常类似的问题。尝试使用wireshark来了解出了什么问题。在我的情况下,我使用System.Web.Mail解决了这个问题。我知道它已被弃用,但你的情况类似于我的情况,我认为没有其他办法可以解决这个问题。

my solution

System.Web.Mail.MailMessage msg = new System.Web.Mail.MailMessage();
    msg.Body = message.Body;

    string smtpServer = "mail.business.it";
    string userName = "username";
    string password = "password";
    int cdoBasic = 1;
    int cdoSendUsingPort = 2;
    if (userName.Length > 0)
    {
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserver", smtpServer);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 25);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusing", cdoSendUsingPort);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", cdoBasic);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", userName);
        msg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password);
    }
    msg.To = message.Destination;
    msg.From = "me@domain.it";
    msg.Subject = message.Subject;
    msg.BodyFormat = MailFormat.Html;//System.Text.Encoding.UTF8;
    SmtpMail.SmtpServer = smtpServer;
    SmtpMail.Send(msg);
相关问题