2011-07-09 93 views
0

我有一個關於使用asp.net發送smtp郵件的問題。當我嘗試通過smtp協議發送時,出現以下錯誤。通過ASP.Net發送SMTP郵件

The server response was: 5.5.1 Authentication Required 

我使用這樣的事情;

string mess = ""; 

    mess += "<b>You have mail</b></br>"; 
    mess += "<b>Name Surname</b>" + textName.Text + "</br>"; 
    mess += "<b>Email adress</b>" + textEmail.Text + "</br>"; 
    mess += "<b>Subject</b>" + textSubject.Text + "</br>"; 
    mess += "<b>Message</b>" + textMessage.Text + "</br>"; 
    mess += "<b>Date</b>" + DateTime.Now.ToString(); 



    MailMessage msg = new MailMessage(); 
    msg.IsBodyHtml = true; 
    msg.To.Add("[email protected]"); 
    msg.From = new MailAddress("[email protected]", "Esma oruc",System.Text.Encoding.UTF8); 
    msg.Subject = textSubject.Text; 
    msg.Body = mess; 

    SmtpClient smp = new SmtpClient(); 
    smp.Credentials = new NetworkCredential("[email protected]", "my password"); 
    smp.Port = 587; 
    smp.Host = "smtp.gmail.com"; 
    smp.EnableSsl = true; 


    smp.Send(msg); 
} 

由於提前,

+0

[通過Gmail在.NET中發送電子郵件(HTTP的可能重複:// stackoverflow.com/questions/32260/sending-email-in-net-through-gmail) – naveen

回答

0

對於通過SMTP在ASP.NET發送Gmail時,使用此

var smtp = new SmtpClient 
      { 
       Host = "smtp.gmail.com", 
       Port = 587, 
       EnableSsl = true, 
       DeliveryMethod = SmtpDeliveryMethod.Network, 
       UseDefaultCredentials = false, 
       Credentials = new NetworkCredential("[email protected]", "my password") 
      }; 
using (var message = new MailMessage("[email protected]", "[email protected]") 
        { 
         Subject = "This is the title of the mail", 
         Body = "This is the body" 
         //,IsBodyHtml = true //optional 
        }) 
{ 
    smtp.Send(message); 
}