【问题标题】:Email not sending using SMTP in ASP.net电子邮件未在 ASP.net 中使用 SMTP 发送
【发布时间】:2018-06-18 22:11:37
【问题描述】:

我尝试在 ASP.net 中通过 SMTP 发送电子邮件,但电子邮件未发送且未给出任何错误。我的代码:

//create the mail message
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
//set the FROM address
mail.From = new MailAddress("aaa@abc.com");
//set the RECIPIENTS
mail.To.Add("bbb@abc.com");
//enter a SUBJECT
mail.Subject = "Set the subject of the mail here.";
//Enter the message BODY
mail.Body = "Enter text for the e-mail here.";
//set the mail server (default should be smtp.1and1.com)
SmtpClient smtp = new SmtpClient("smtp.1and1.com");
//Enter your full e-mail address and password
smtp.Credentials = new NetworkCredential("example@abc.com", "xxxxxxx");
//send the message 
smtp.Send(mail);

而我在 web.config 中的代码是

<system.net>
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory">
        <specifiedPickupDirectory pickupDirectoryLocation="C:\Mail\" />
      </smtp>
    </mailSettings>
    <defaultProxy enabled="true" />
  </system.net>

【问题讨论】:

  • 您的 smtp 端口可能被阻止或凭据可能错误

标签: asp.net email smtp smtpclient


【解决方案1】:

使用断点显示每个参数的值。即使代码没有错误,参数的值也可能为空或错误。在断点之前,使用try-catch,如下所示;

                try
                {
                    // Your codes
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message.ToString());
                }

【讨论】:

    【解决方案2】:

    首先检查你的 TLS 版本。 在 IE(Internet Explorer) 浏览器中打开您的站点并转到页面属性。

    如果您的 TLS 1.2 则在 client.Send(Object); 之前添加以下代码;

    ServicePointManager.Expect100Continue = true;
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
    

    如下所示

     System.Net.Mail.SmtpClient objSMTPClient = new System.Net.Mail.SmtpClient();
     SmtpClient client = new SmtpClient();
     client.EnableSsl = true;
     ServicePointManager.Expect100Continue = true;
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
     client.Send(oMail);
     client.Dispose();
     client = null;
    

    【讨论】:

      猜你喜欢
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      相关资源
      最近更新 更多