gpt4 book ai didi

C# 使用 SMTP 发送电子邮件

转载 作者:太空宇宙 更新时间:2023-11-03 17:00:58 25 4
gpt4 key购买 nike

Exception :

Failure sending mail.

内部异常:

Unable to connect to the remote server.

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 它工作正常。

最佳答案

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);

它会很好地工作

关于C# 使用 SMTP 发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34131191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com