title: Mailkit is sending emails with the incorrect from address
body: <p>I've written a mail method and I'm setting the from email address but when the customer receives it, it's the same as the username that is used to authenticate the email.</p>
  <p>I've tried to set the email right before the send and it still comes out wrong:</p>
  <p><a href="https://i.stack.imgur.com/YvlDH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YvlDH.png" alt="enter image description here" /></a></p>
  <p>When I check the message, as shown above, the from is correct but the customer receives it from the username field instead.</p>
  <pre><code>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 = &quot;SendEmail disabled due to TestModeEnabled being set to true.&quot;;
        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(&quot;html&quot;)
      //{
      //  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(&quot;gmail.com&quot;))
        {
          // Note: since we don't have an OAuth2 token, disable
          // the XOAUTH2 authentication mechanism.
          client.AuthenticationMechanisms.Remove(&quot;XOAUTH2&quot;);
        }
  
        client.SslProtocols = System.Security.Authentication.SslProtocols.Tls12;
  
        //accept all SSL certificates
        client.ServerCertificateValidationCallback = (s, c, h, e) =&gt; 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(&quot;someone@somewhere.net&quot;));
  
        client.Send(message);
        client.Disconnect(true);
      }
  
      // if we reached this far, then the email was sent successfully
      emailResults.Message = &quot;Successfully sent.&quot;;
      emailResults.IsSuccessful = true;
      return emailResults;
  
    }
  
    catch (AuthenticationException e)
    {
      Logging.LogException(&quot;SmtpClient.SendEmail&quot;, &quot;Error attempting to send email.&quot;, e);
      emailResults.IsSuccessful = false;
      emailResults.Message = &quot;Invalid username or password.&quot;;
      return emailResults;
    }
  
    catch (Exception e)
    {
      Logging.LogException(&quot;SmtpClient.SendEmail&quot;, &quot;Error attempting to send email.&quot;, e);
      emailResults.IsSuccessful = false;
  
      if (e.Message.Contains(&quot;error occurred while attempting to establish an SSL or TLS connection&quot;))
      {
        emailResults.Message = &quot;An error occurred while attempting to establish a secure connection.\r\n\r\nPlease check your email settings.&quot;;
      }
      else
      {
        emailResults.Message = e.Message;
      }
  
      return emailResults;
    }
  }
  </code></pre>
  <p>Anyone have any suggestions on what I'm doing wrong?</p>