gpt4 book ai didi

c# - 无法在 mvc 应用程序中发送电子邮件

转载 作者:行者123 更新时间:2023-11-30 22:08:17 26 4
gpt4 key购买 nike

从我的应用程序发送电子邮件时遇到一些问题..

我得到以下异常

An exception of type 'System.Net.Mail.SmtpException' occurred in System.dll but was not handled in user code

Additional information: Failure sending mail.

运行时弹出异常:smtpClient.Send(mailMessage);

发送邮件的函数:

public static void SendEmail(string toAddress, string subject, string body, bool isBodyHtml = true)
{
var mailMessage = new MailMessage();
mailMessage.To.Add(toAddress);
mailMessage.Subject = subject;
mailMessage.Body = body;
mailMessage.IsBodyHtml = isBodyHtml;

var smtpClient = new SmtpClient { EnableSsl = false };
smtpClient.Send(mailMessage);
}

WebConfig 配置

 <system.net>
<mailSettings>
<!-- Method#1: Send emails over the network -->
<smtp deliveryMethod="Network" from="myusername@gmail.com">
<network host="gmail.com" userName="myusername" password="mypw" port="465" />
</smtp>
</mailSettings>
</system.net>

感谢任何帮助

最佳答案

这就是我发送 gmail 的方式:

    static void SendMail(string sSubject, string sBody)
{
const string senderID = "ImTheSender@gmail.com"; // use sender's email id here..
const string toAddress = "ImTheRecip@gmail.com";
const string senderPassword = "passwords are fun"; // sender password here...
try
{
SmtpClient smtp = new SmtpClient
{
Host = "smtp.gmail.com", // smtp server address here...
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new System.Net.NetworkCredential(senderID, senderPassword),
Timeout = 30000,
};
MailMessage message = new MailMessage(senderID, toAddress, sSubject, sBody);
smtp.Send(message);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
LogThis("Error sending mail:" + ex.Message);
Console.ResetColor();
}
}

关于c# - 无法在 mvc 应用程序中发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22428241/

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