C#使用SMTP发送电子邮件

时间:2015-12-07 10:20:19

标签: c# smtp

  

例外:

     
    

发送邮件失败。

  
     

内部例外:

     
    

无法连接到远程服务器。

  
MailMessage mail = new MailMessage();
mail.Subject = "Your Subject";
mail.From = new MailAddress("abc@gmail.com");
mail.To.Add("xyz@yahoo.co.in");
mail.Body = "Hello! your mail content goes here...";
mail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("abc@gmail.com", "xxx");
smtp.Credentials = netCre;

try
{
     smtp.Send(mail);
 }
 catch (Exception ex)
 {
 }

我ping smtp.gmail.com它运行正常。

2 个答案:

答案 0 :(得分:2)

MailMessage mm = new MailMessage();
SmtpClient smtp = new SmtpClient(); 

mm.From = new MailAddress("From", "DisplayName", System.Text.Encoding.UTF8);
mm.To.Add(new MailAddress("To"));
mm.Subject = "Subject";
mm.Body = "Body";

mm.IsBodyHtml = true;
smtp.Host = "smtp.gmail.com";
if (ccAdd != "")
{
    mm.CC.Add(ccAdd);
}
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "xyz@gmail.com";//gmail user name
NetworkCred.Password = "Password";// password
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587; //Gmail port for e-mail 465 or 587
smtp.Send(mm);

它会正常工作

答案 1 :(得分:-3)

ASP.Net的电子邮件脚本

op

我希望它会对你有所帮助。它的工作