我想通过asp.net发送邮件但发生错误

时间:2017-09-22 15:53:49

标签: asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.ComponentModel;// for backgroundworker class
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Configuration;
namespace mail_demo_project
{
  public partial class register_mail_demo : System.Web.UI.Page
  {
      protected void Page_Load(object sender, EventArgs e)
      {

      }
      protected void submit_Click(object sender, EventArgs e)
      {
        try
        {
            string From = "rashmi1810214@gmail.com";
            MailAddress mailAddress = new MailAddress(From, "Contact Us");
            MailMessage mailMessage = new MailMessage();
            mailMessage.To.Add("upadhyaya.monika@gmail.com");
            mailMessage.From = mailAddress;
            mailMessage.Subject = subject_textbox.Text;
            string mailBody = "User Email: " + message_box_textbox.Text + "
            <br/> User Name: " + to_username_textbox.Text + "<br/> Message: 
            " + message_box_textbox.Text;
            mailMessage.Body = mailBody;
            mailMessage.Priority = MailPriority.Normal;
            mailMessage.IsBodyHtml = true;
            System.Net.NetworkCredential credential = new 
            System.Net.NetworkCredential("rashmi1810214@gmail.com", 
            "***password***");
            SmtpClient smtpClient = new SmtpClient();
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.EnableSsl = false;
            smtpClient.Host = "smtp.gmail.com";
            smtpClient.Port = 587;
            smtpClient.UseDefaultCredentials = true;
            smtpClient.Credentials = credential;
            smtpClient.Send(mailMessage);
            Response.Write("code successfully executed");
        }
        catch (Exception ex)
        {
            Response.Write("Exception caught in CreateTestMessage1(): {0}"+ 
            ex.ToString());
        }
    }
}
}

发生错误: -

  

CreateTestMessage1()中捕获到异常:{0} System.Net.Mail.SmtpException:SMTP服务器需要安全连接或客户端未经过身份验证。服务器响应为:5.7.0必须首先发出STARTTLS命令。 d4sm6963507pfb.185 - 位于系统的System.Net.Mail.MailCommand.Send(SmtpConnection conn,Byte []命令,MailAddress from,Boolean allowUnicode)的System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode,String response)中的gsmtp。 Net.Mail.SmtpTransport.SendMail(MailAddress sender,MailAddressCollection recipients,String deliveryNotify,Boolean allowUnicode,SmtpFailedRecipientException&amp; exception)at system.Net.Mail.SmtpClient.Send(MailMessage message)mail_demo_project.register_mail_demo.submit_Click(Object sender,EventArgs e) )在e:\ dot net \ projects \ mail demo project \ mail demo project \ _ register_mail_demo.aspx.cs:第76行

1 个答案:

答案 0 :(得分:0)

邮件服务器需要SSL连接,但您已将其禁用

试试这个

smtpClient.EnableSsl = true;