我有一个Office 365家庭订阅(因此使用outlook.com),并试图从我正在工作的C#应用程序发送电子邮件。有人知道这是否可能吗?从我的研究来看,似乎有很多人对这种方法有异议,但我正在努力找出这种方法是否得到支持
发布于 2022-02-08 09:38:33
完成所需工作的方式有多种:
using (SmtpClient client = new SmtpClient()
{
Host = "smtp.office365.com",
Port = 587,
UseDefaultCredentials = false, // This require to be before setting Credentials property
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential("alias@fulldomain.com", "password"), // you must give a full email address for authentication
TargetName = "STARTTLS/smtp.office365.com", // Set to avoid MustIssueStartTlsFirst exception
EnableSsl = true // Set to avoid secure connection exception
})
{
MailMessage message = new MailMessage()
{
From = new MailAddress("alias@fulldomain.com"), // sender must be a full email address
Subject = subject,
IsBodyHtml = true,
Body = "<h1>Hello World</h1>",
BodyEncoding = System.Text.Encoding.UTF8,
SubjectEncoding = System.Text.Encoding.UTF8,
};
var toAddresses = recipients.Split(',');
foreach (var to in toAddresses)
{
message.To.Add(to.Trim());
}
try
{
client.Send(message);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}SmtpClient在.NET内核中是可用的,但不推荐使用它。相反,请考虑使用https://github.com/jstedfast/MailKit。
发布于 2022-02-09 05:10:05
对于那些在使用smtp.office 365.com和通过代码发送电子邮件方面有问题的人来说。您需要在您的Microsoft帐户中添加一个“应用密码”。登录到outlook.com时使用的常规密码将无法工作。
https://stackoverflow.com/questions/71029199
复制相似问题