Mailkit 发送的电子邮件发件人地址不正确

Mailkit is sending emails with the incorrect from address

我写了一个邮件方法,我正在设置发件人电子邮件地址,但是当客户收到它时,它与用于验证电子邮件的用户名相同。

我试过在发送前设置邮件,结果还是出错:

当我检查消息时,如上所示,发件人是正确的,但客户收到的却是用户名字段。

public static EmailResults SendEmail(EmailSettings emailSettings)
{
  var emailResults = new EmailResults();

  try
  {
    // using mimekit that the msdn smtpclient webpage suggested using
    // http://www.mimekit.net/docs/html/Introduction.htm

    if (emailSettings.TestModeEnabled)
    {
      emailResults.IsSuccessful = true;
      emailResults.Message = "SendEmail disabled due to TestModeEnabled being set to true.";
      return emailResults;
    }

    // use the mimemessage to create the message to send
    var message = new MimeMessage();

    message.From.Add(emailSettings.FromEmail);
    message.Subject = emailSettings.EmailSubject;

    // to email has the option for multiple emails to be sent to
    // loop through them and add them all to the message
    foreach (var mailboxAddress in emailSettings.ToEmail)
    {
      message.To.Add(mailboxAddress);
    }

    // attach file if present
    var builder = new BodyBuilder();
    if (emailSettings.FileAttachments != null)
    {
      foreach (var file in emailSettings.FileAttachments)
      {
        if (File.Exists(file))
        {
          builder.Attachments.Add(file);
        }
      }
    }

    builder.HtmlBody = emailSettings.EmailBody;
    message.Body = builder.ToMessageBody();

    //// http://www.mimekit.net/docs/html/Creating-Messages.htm
    //// A TextPart is a leaf-node MIME part with a text media-type. The first argument to the
    //// TextPart constructor specifies the media-subtype, in this case, plain. Another media
    //// subtype you are probably familiar with is the html subtype. Some other examples
    //// include enriched, rtf, and xml.
    //message.Body = new TextPart("html")
    //{
    //  Text = emailSettings.EmailBody
    //};

    // bcc has the option for multiple emails to be sent to
    // loop through them and add them all to the message
    if (emailSettings.BccEmail != null)
    {
      foreach (var mailboxAddress in emailSettings.BccEmail)
      {
        message.Bcc.Add(mailboxAddress);
      }
    }

    // *************** SEND EMAIL *******************
    var client = emailSettings.EnableSmtpLog ? new MailKit.Net.Smtp.SmtpClient(new ProtocolLogger(GlobalVariables.SmptpLogFile)) : new MailKit.Net.Smtp.SmtpClient();

    using (client)
    {
      if (emailSettings.SmtpServer.Contains("gmail.com"))
      {
        // Note: since we don't have an OAuth2 token, disable
        // the XOAUTH2 authentication mechanism.
        client.AuthenticationMechanisms.Remove("XOAUTH2");
      }

      client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;

      //accept all SSL certificates
      client.ServerCertificateValidationCallback = (s, c, h, e) => true;

      // client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.IsSslEnabled);
      client.Connect(emailSettings.SmtpServer, emailSettings.SmtpPort, emailSettings.AuthType);

      if (emailSettings.IsAuthenticationRequired)
      {
        // Note: only needed if the SMTP server requires authentication
        client.Authenticate(emailSettings.SmtpUsername, emailSettings.SmtpPassword);
      }

      if (emailSettings.TimeOut == 0) emailSettings.TimeOut = 10;
      client.Timeout = emailSettings.TimeOut * 1000;

      //message.From.Add(new MailboxAddress("someone@somewhere.net"));

      client.Send(message);
      client.Disconnect(true);
    }

    // if we reached this far, then the email was sent successfully
    emailResults.Message = "Successfully sent.";
    emailResults.IsSuccessful = true;
    return emailResults;

  }

  catch (AuthenticationException e)
  {
    Logging.LogException("SmtpClient.SendEmail", "Error attempting to send email.", e);
    emailResults.IsSuccessful = false;
    emailResults.Message = "Invalid username or password.";
    return emailResults;
  }

  catch (Exception e)
  {
    Logging.LogException("SmtpClient.SendEmail", "Error attempting to send email.", e);
    emailResults.IsSuccessful = false;

    if (e.Message.Contains("error occurred while attempting to establish an SSL or TLS connection"))
    {
      emailResults.Message = "An error occurred while attempting to establish a secure connection.\r\n\r\nPlease check your email settings.";
    }
    else
    {
      emailResults.Message = e.Message;
    }

    return emailResults;
  }
}

有人对我做错了什么有什么建议吗?

对于再次遇到此问题的任何人...

此特定问题特定于 Gmail。我不确定其他电子邮件主机是否也这样做。

如果您将发件人电子邮件地址设置为“person1@email.com”但使用“person2@gmail.com”进行身份验证,Gmail 将使用身份验证电子邮件地址覆盖发件人电子邮件地址。你无法控制这个。

转到 Gmail 帐户,进入设置,然后在“发送邮件为”部分添加别名地址。外发电子邮件现在应该显示别名地址而不是身份验证地址。