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

我運行那個時候發生了一些錯誤:發送電子郵件失敗。 SMTP服務器需要安全連接或客戶端未通過身份驗證。服務器響應是:5.7.0必須首先發出STARTTLS命令。 i1sm8651517pbj.70如何通過在asp.net中使用smtp發送郵件

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 
    } 

回答

2

您需要設置SmtpClient.Credentials屬性:

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

這就是使用,以發送郵件進行身份驗證。您可能還需要確保啓用SSL:

smtpClient.EnableSsl = true; 

SmtpClient.Credentials Property MSDN Reference

+0

應當System.Net.NetworkCredential(沒有或多個)。 – zbarrier 2014-05-26 22:46:25

1

看來你要發送使用GMail一封電子郵件,要求SSL
看到這個Google reference post

所以在你的web.config,啓用SSL這樣:

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

或者,你可以將它設置programmatically this way

smtpClient.EnableSsl = true; 
0

我想你忘了將EnableSSL屬性設置爲true,這是gmail所必需的。

下面是示例代碼:

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

使用C#Asp.Net發送電子郵件與附件從GODADDY – 2016-07-16 15:23:16

+0

您可以在您的答案中添加註釋和解釋到您的代碼。 – HDJEMAI 2016-07-16 15:45:22

+0

您的答案對Gmail無效,並且不能解決問題中的錯誤。 – 2016-07-16 15:49:52