电子邮件不在ASP.net中使用SMTP发送

时间:2017-01-30 13:24:21

标签: asp.net email smtp smtpclient

我尝试在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>

2 个答案:

答案 0 :(得分:1)

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

enter image description here

如果您的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;

答案 1 :(得分:0)

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

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