【问题标题】:.NET : Unable to change from in SMTP email.NET:无法从 SMTP 电子邮件中更改
【发布时间】: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 

【问题讨论】:

标签: c# .net smtp


【解决方案1】:

Google (GMail) 和 Microsoft (Office365) 都将 From 标头替换为用于发送邮件的帐户的电子邮件地址,以减少欺骗。

如果您不希望这样,那么您需要使用另一个 SMTP 服务器或设置您自己的。

【讨论】:

  • 不正确,当我将邮件发送到我的个人电子邮件帐户(或我组织之外的任何帐户)时,它可以工作
【解决方案2】:

我发现它在将电子邮件发送到我的个人 Gmail 时有效。意思是代码没有问题,但是我的office365/AD域的配置有问题。

显然,Outlook“通讯录”会自动填写电子邮件中的“发件人/发件人”部分,这是因为我将邮件发送到与我的 SMTP 帐户使用的域相同的域。 (例如 me@domain-a.com 和 noreply@domain-a.com)。

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 1970-01-01
    • 2018-04-30
    • 2022-07-20
    • 2022-01-21
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2020-03-02
    相关资源
    最近更新 更多