【发布时间】:2021-05-24 13:02:15
【问题描述】:
我正在尝试通过 SMTP 在 .NET 中发送电子邮件。我将多个自定义别名链接到 office365 中的帐户。 (例如 no-reply@domain-a.com、no-reply@domain-b.com)
但是邮件来自 No-Reply@mydomain.onmicrosoft.com。即使我在“from”参数中传入了自定义域。
这是我的代码:
var smtpClient = new SmtpClient(_settings.Endpoint, int.Parse(_settings.Port))
{
UseDefaultCredentials = false,
EnableSsl = true,
Credentials = new NetworkCredential(_settings.UserName, _settings.Password),
};
var mailMessage = new MailMessage
{
From = new MailAddress(message.From.Email, message.From.Name),
Subject = message.Subject,
Body = message.HtmlMessage,
IsBodyHtml = true
};
foreach (var addressee in message.Tos)
{
mailMessage.To.Add(addressee.Email);
}
try
{
smtpClient.Send(mailMessage);
}
catch (Exception e)
{
_logger.LogError(e, "Error sending email");
throw;
}
我使用 myaccount@mydomain.onmicrosoft.com 作为用户名。 我在这里缺少什么? office365/domain 配置应该没有问题,因为当我尝试使用 powershell 发送邮件时它可以工作
$O365Cred = Get-Credential #the myaccount@mydomain.onmicrosoft.com credentials
$sendMailParams = @{
From = 'no-reply@mydomain-a.com'
To = 'me@gmail.com'
Subject = 'some subject'
Body = 'some body'
SMTPServer = 'smtp.office365.com'
Port = 587
UseSsl = $true
Credential = $O365Cred
}
Send-MailMessage @sendMailParams
【问题讨论】:
-
仔细检查一下这可能是stackoverflow.com/a/32475872/2946329
-
@SalahAkbar 我正在使用 office365。我应该检查类似的东西吗?