SMTP Live.com”;发送邮件失败”;
本文关键字:失败 Live SMTP com | 更新日期: 2023-09-27 18:20:22
这些代码有什么问题?它总是抛出异常"{"发送邮件失败。"}"!我不知道为什么。请帮帮我。
编辑:内部异常:{"无法连接到远程服务器"}
string smtpAddress = "smtp.live.com";
int portNumber = 587;
bool enableSSL = true;
string emailFrom = "myemail@live.com";
string password = "myPassword";
string emailTo = "otherEmail@live.com";
string subject = "Hello";
string body = "Hello, I'm just writing this to say Hi!";
using (MailMessage mail = new MailMessage())
{
mail.From = new MailAddress(emailFrom);
mail.To.Add(emailTo);
mail.Subject = subject;
mail.Body = body;
mail.IsBodyHtml = false;
using (SmtpClient smtp = new SmtpClient(smtpAddress, portNumber))
{
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);
}
}
发送邮件时使用关键字删除,如下所示:
来自:
using (SmtpClient smtpClient = new SmtpClient())
至:
SmtpClient smtpClient = new SmtpClient();
去除支架也像这样:
smtp.Credentials = new NetworkCredential(emailFrom, password);
smtp.EnableSsl = enableSSL;
smtp.Send(mail);