2012-03-11 55 views
5
i need solution for this error 

tôi chạy thời gian đó xảy ra lỗi: Gửi Email không thành công. Máy chủ SMTP yêu cầu kết nối an toàn hoặc máy khách không được xác thực. Phản hồi của máy chủ là: 5.7.0 Phải phát hành một lệnh STARTTLS trước. i1sm8651517pbj.70cách gửi thư bằng cách sử dụng smtp trong asp.net

using System; 
    using System.Configuration; 
    using System.Web; 
    using System.Web.Security; 
    using System.Web.UI; 
    using System.Web.UI.WebControls; 
    using System.Web.UI.HtmlControls; 
    using System.Net.Mail; 

    public partial class _Default : System.Web.UI.Page 
    { 
     #region "Send email" 
     protected void btnSendmail_Click(object sender, EventArgs e) 
     { 
      // System.Web.Mail.SmtpMail.SmtpServer is obsolete in 2.0 
      // System.Net.Mail.SmtpClient is the alternate class for this in 2.0 
      SmtpClient smtpClient = new SmtpClient(); 
      MailMessage message = new MailMessage(); 

      try 
      { 
       MailAddress fromAddress = new MailAddress(txtEmail.Text, txtName.Text); 

       // You can specify the host name or ipaddress of your server 
       // Default in IIS will be localhost 
       smtpClient.Host = "smtp.gmail.com"; 

       //Default port will be 25 
       smtpClient.Port = 587; 

       //From address will be given as a MailAddress Object 
       message.From = fromAddress; 

       // To address collection of MailAddress 
       message.To.Add("[email protected]"); 
       message.Subject = "Feedback"; 

       // CC and BCC optional 
       // MailAddressCollection class is used to send the email to various users 
       // You can specify Address as new MailAddress("[email protected]") 
       message.CC.Add("[email protected]"); 
       message.CC.Add("[email protected]"); 

       // You can specify Address directly as string 
       message.Bcc.Add(new MailAddress("[email protected]")); 
       message.Bcc.Add(new MailAddress("[email protected]")); 

       //Body can be Html or text format 
       //Specify true if it is html message 
       message.IsBodyHtml = false; 

       // Message body content 
       message.Body = txtMessage.Text; 

       // Send SMTP mail 
       smtpClient.Send(message); 

       lblStatus.Text = "Email successfully sent."; 
      } 
      catch (Exception ex) 
      { 
       lblStatus.Text = "Send Email Failed.<br>" + ex.Message; 
      } 
     } 
     #endregion 

     #region "Reset" 
     protected void btnReset_Click(object sender, EventArgs e) 
     { 
      txtName.Text = ""; 
      txtMessage.Text = ""; 
      txtEmail.Text = ""; 
     } 
     #endregion 
    } 

Trả lời

2

Bạn cần phải đặt SmtpClient.Credentials tài sản:

smtpClient.Credentials = new NetworkCredentials("yourUserName", "yourPassword"); 

Đây là những gì được sử dụng để xác thực để gửi tin nhắn. Bạn cũng có thể cần phải đảm bảo rằng SSL được kích hoạt:

smtpClient.EnableSsl = true; 

SmtpClient.Credentials Property MSDN Reference

+0

Nó phải là System.Net.NetworkCredential (không có s). – zbarrier

1

Có vẻ như bạn đang cố gắng để gửi một email bằng GMail, đòi hỏi SSL.
Xem điều này Google reference post.

Vì vậy, trong web.config của bạn, kích hoạt SSL theo cách này:

<system.net> 
    <mailSettings> 
     <smtp deliveryMethod="network"> 
     <network host="smtp.gmail.com" port="587" enableSsl="true" userName="YOURUSERNAME" password="YOURPASSWORD" /> 
     </smtp> 
    </mailSettings> 
    </system.net> 

Ngoài ra, bạn có thể thiết lập nó programmatically this way:

smtpClient.EnableSsl = true; 
0

Tôi nghĩ bạn đã quên đặt thuộc tính EnableSSL thành true là bắt buộc đối với gmail.

Đây là đoạn mã mẫu:

protected void btnSend_Click(object sender, EventArgs e) 
{ 
    try 
    {    
     MailMessage msg = new MailMessage(); 
     msg.From = new MailAddress(txtFrom.Text);     
     msg.To.Add(new MailAddress(txtTo.Text)); 
     msg.Subject = txtSubject.Text; 
     msg.Body = txtBody.Text;       

     SmtpClient mySmtpClient = new SmtpClient(); 
     System.Net.NetworkCredential myCredential = new System.Net.NetworkCredential(txtFrom.Text,txtPwd.Text); 
     mySmtpClient.Host = "smtp.gmail.com"; 
     mySmtpClient.Port=587; 
     mySmtpClient.EnableSsl = true; 
     mySmtpClient.UseDefaultCredentials = false; 
     mySmtpClient.Credentials = myCredential;     

     mySmtpClient.Send(msg); 
     msg.Dispose(); 
     lberr.Text="Message sent successfully"; 
     clrtxt(); 
    } 
    catch(SmtpException) 
    { 
     lberr.Text="SMTP Error handled"; 
    } 
} 
-1
public void SendMail() 
{ 
    System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
    mail.To.Add(MailTo.Text); 
    mail.From = new MailAddress(MailFrom.Text,"Invoice"); 
    mail.Subject = Subject.Text; 
    mail.Body = Body.Text; 
    mail.IsBodyHtml = true; 



    string FileName = Path.GetFileName(FileUploadAttachments.PostedFile.FileName); 
    Attachment attachment = new Attachment(FileUploadAttachments.PostedFile.InputStream ,FileName); 
    mail.Attachments.Add(attachment);    

    SmtpClient client = new SmtpClient(); 
    client.Credentials = new System.Net.NetworkCredential("[email protected]", "Your_Email_Password"); 
    client.Host = "smtpout.secureserver.net"; 
    client.Port = 80; 
    try 
    { 
     client.Send(mail); 
    } 
    catch (Exception ex) 
    { 
     System.Windows.Forms.MessageBox.Show(ex.Message); 
    } 
} 
+0

Gửi email với BÀI ĐĂNG từ GODADDY bằng C# Asp.Net –

+0

Bạn có thể thêm nhận xét và giải thích cho mã của bạn trong câu trả lời. – HDJEMAI

+0

Câu trả lời của bạn không hoạt động cho Gmail và không giải quyết được lỗi trong câu hỏi. –

Các vấn đề liên quan