2011-07-09 95 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); 
}