在ASP .Net中发送电子邮件

时间:2011-11-23 09:54:24

标签: asp.net email smtp

如何使用Outlook地址在ASP .Net上发送电子邮件?

我尝试过这段代码,但没有运气:

Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()

mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))

mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"

Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)

smtpClient.Send(mailMessage) //--> got error here

但是当我执行代码时,它收到了以下错误信息:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated

我尝试添加一行代码:

smtpClient.Credentials =新的System.Net.NetworkCredential(用户名,密码)

但仍无法发送电子邮件。

任何人都可以提供帮助吗?

6 个答案:

答案 0 :(得分:1)

我猜您使用Exchange 2007或更高版本作为后端?

无论如何,您的邮件服务器不允许您匿名发送邮件。您需要在代码中提供用户名/密码,或者允许从您的网络服务器进行未经身份验证的中继。

与您的IT人员讨论他们的偏好。

答案 1 :(得分:1)

  • 在设置新凭据
  • 之前尝试smtpClient.UseDefaultCredentials = false;
  • 尝试根据您的环境将smtpClient.EnableSsl设置为true / false

答案 2 :(得分:0)

我是用c#做的。这可能对你有帮助。请检查。

   MailMessage msg1 = new MailMessage();
                        msg1.From = strEFrom;
                        msg1.To = strETo;
                        msg1.Cc = strECC;
                        msg1.Subject = "Hi";
                        msg1.Priority = MailPriority.High;
                        msg1.BodyFormat = MailFormat.Html;
                        msg1.Body = strBody;
                        SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
                        SmtpMail.Send(msg1);

并在web.config文件中

<appSettings>
          <add key="MailServer" value=""/>
</appSettings>

答案 3 :(得分:0)

尝试使用这些设置检查.EnableSSL属性true/false以及smtp post number收听外发邮件的mail server

如果未设置在Outlook for gmail outlook配置中启用SSL设置,则会出现相同的错误,但原因是找到了SSL设置..

试试这可能会解决你的问题..

msg.IsBodyHtml = true;
            msg.Body = mailContent;

            SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
            NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
            mailClient.UseDefaultCredentials = false;
            mailClient.Credentials = NetCrd;
            mailClient.EnableSsl = false;
            mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            mailClient.Send(msg);

如果它无法解决您的问题,请检查您的邮件服务器/客户端是否存在问题。

答案 4 :(得分:0)

      public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
      {
        try
        {
         NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress(from);
         msg.To.Add(new MailAddress(to));
         msg.Subject = subject;
         msg.Body = message;
         msg.IsBodyHtml = false;
         SmtpClient client = new SmtpClient("smtp.gmail.com");
         client.EnableSsl = true;
         client.UseDefaultCredentials = false;
         client.Credentials = loginInfo;
         client.Send(msg);

         return true;
        }
       catch (Exception)
       {
         return false;
       }

   }

我使用此代码从我的Gmail帐户发送邮件。

答案 5 :(得分:0)

有同样的错误,移动了client.UseDefaultCredentials = false;在设置client.Credentials之前一切正常。