gpt4 book ai didi

c# - 使用C#的电子邮件发送问题

转载 作者:行者123 更新时间:2023-12-03 22:56:24 25 4
gpt4 key购买 nike

我正在使用c#发送电子邮件,并从web.config文件中使用的appsettings获取所有值,但我的问题是尽管没有异常或错误,但我没有收到任何电子邮件。请帮助我解决我的问题,这是我的代码

public static bool SendMail(string to,string subject, string body)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
using (SmtpClient smtp = new SmtpClient())
{
smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.SendMailAsync(mailMessage);
return true;
}
}
catch (Exception ex)
{
return false;
}
}


这是我的web.config设置

 <add key="smtpServer" value="smtp.gmail.com" />
<add key="smtpPort" value="587" />
<add key="smtpUser" value="offey2018@gmail.com" />
<add key="smtpPass" value="*******" />

最佳答案

试试这个

如果您打算使用SendMailAsync方法,则可以按照以下步骤进行

public static async Task SendMail(string to,string subject, string body)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
using (SmtpClient smtp = new SmtpClient())
{
smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
await smtp.SendMailAsync(mailMessage);
}
}
catch (Exception ex)
{
throw ex;
}
}


否则您可以使用以下方式通过使用非异步方法发送邮件

public static bool SendMail(string to,string subject, string body)
{
try
{
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(ConfigurationManager.AppSettings["smtpUser"]);
mailMessage.To.Add(new MailAddress(to));
mailMessage.Subject = subject;
mailMessage.IsBodyHtml = true;
mailMessage.Body = body;
using (SmtpClient smtp = new SmtpClient())
{
smtp.EnableSsl = true;
smtp.Host = ConfigurationManager.AppSettings["smtpServer"];
smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(mailMessage);
return true;
}
}
catch (Exception ex)
{
return false;
}
}


注意

异步方法只能有两种返回类型1)无效2)任务。因此,您可以根据需要使用任何一种方式。

我希望这能帮到您。如果您有任何疑问或问题,请告诉我。

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

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