2011-04-06 79 views
2

我無法使用Gmail設置發送電子郵件。我已經嘗試client.Host =「本地主機」它的工作,但不是在client.Host =「smtp.gmail.com」..請幫助我的傢伙..我需要使用client.Host =「smtp.gmail.com」.. ..在這裏感謝使用Gmail設置發送電子郵件時出現問題

是我的C#代碼:

string from = "[email protected]"; //Replace this with your own correct Gmail Address 
string to = "[email protected]"; //Replace this with the Email Address to whom you want to send the mail 

System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage(); 
mail.To.Add(to); mail.From = new 
MailAddress(from, "One Ghost" ,System.Text.Encoding.UTF8); 
mail.Subject = "This is a test mail" ; 
mail.SubjectEncoding = System.Text.Encoding.UTF8; mail.Body = "This is Email Body Text"; 
mail.BodyEncoding = System.Text.Encoding.UTF8; 
mail.IsBodyHtml = true ; mail.Priority = MailPriority.High; 

SmtpClient client = new SmtpClient(); //Add the Creddentials- use your own email id and password 

    client.Credentials = new System.Net.NetworkCredential(from, "iseedeadpoeple"); 

client.Port = 587; // Gmail works on this port client.Host ="smtp.gmail.com";   

client.EnableSsl = true; //Gmail works on Server Secured Layer 
     try 
     { 
      client.Send(mail); 
     } 
     catch (Exception ex) 
     { 
      Exception ex2 = ex; 
      string errorMessage = string.Empty; 
      while (ex2 != null) 
      { 
       errorMessage += ex2.ToString(); 
       ex2 = ex2.InnerException; 
      } HttpContext.Current.Response.Write(errorMessage 
); 
     } // end try 

這裏的錯誤:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. 

大部分謝謝你們!

+0

http://archive.msdn.microsoft.com/CSharpGmail – Peter 2011-04-06 11:55:04

+0

我很高興你考慮了我對你最後一個問題的評論,並且花時間寫下你的問題:-) – 2011-04-06 14:55:20

回答

0
public static string sendMail(string to, string title, string subject, string body) 
     { 
      try 
      { 
       MailMessage mail = new MailMessage(); 
       SmtpClient smtp = new SmtpClient(); 
       if (to == "") 
        to = "[email protected]"; 
       MailAddressCollection m = new MailAddressCollection(); 
       m.Add(to); 
       mail.Subject = subject; 
       mail.From = new MailAddress("[email protected]"); 
       mail.Body = body; 
       mail.IsBodyHtml = true; 
       mail.ReplyTo = new MailAddress("[email protected]"); 
       mail.To.Add(m[0]); 
       smtp.Host = "smtp.gmail.com"; 
       client.Port = 587; 
       smtp.EnableSsl = true; 
       smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "####"); 
       ServicePointManager.ServerCertificateValidationCallback = delegate(object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; }; 

       smtp.Send(mail); 

       return "done"; 
      } 
      catch (Exception ex) 
      { 
       return ex.Message; 
      } 
     } 
4

您需要獲得並使用SSL證書secutiry發送郵件給Gmail

MailMessage msgMail = new MailMessage("[email protected]", "[email protected]", "subject", "message body"); 
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587); 
smtp.EnableSsl = true; 
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "a"); 
try 
{ 
    smtp.Send(msgMail); 
} 
catch (Exception ex) 
{ 
} 

參考:http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/28b5a576-0da2-42c9-8de3-f2bd1f30ded4

相關問題