title: Using .NET SmtpClient to Send mail alters the From name
body: <p>I have written a method to send mail with attachments and authentication. Everything works perfectly except the <code>From</code> email address gets altered in the message received.</p>
  
  <p>For example,</p>
  
  <pre><code>foo@example.com
  </code></pre>
  
  <p>Gets changed to,</p>
  
  <pre><code>-unknown-@example.com
  </code></pre>
  
  <p>The class I wrote is as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Net;
  using System.Net.Mail;
  using System.IO;
  
  class Email {
  
      public MailAddress From { get; set; }
      public string Host { get; set; }
      public int Port { get; set; }
  
  
      public Credentials SMTPAuth { get; set; }
  
      public class Credentials {
          public string Username { get; set; }
          public string Password { get; set; }
      }
  
      public Email() {
          SMTPAuth = new Credentials();
      }
  
      public List&lt;Attachment&gt; Attachments(params string[] file) {
          List&lt;Attachment&gt; r = new List&lt;Attachment&gt;();
          foreach ( string f in file ) {
              if ( File.Exists( f ) ) {
                  r.Add(new Attachment(f));
              }
          }
          return r;
      }
  
      public async Task&lt;bool&gt; SendMail( string Subject, string Message, MailAddressCollection To, MailPriority Priority, List&lt;Attachment&gt; Attachments ) {
          bool result = false;
          using ( SmtpClient s=new SmtpClient() ) {
              NetworkCredential auth=new NetworkCredential( SMTPAuth.Username, SMTPAuth.Password );
              s.Host=Host;
              s.Port = Port;
              s.UseDefaultCredentials = false;
              s.Credentials=auth;
  
  
              using ( MailMessage m=new MailMessage() ) {
                  m.From = From;
                  m.Subject = Subject;
                  m.IsBodyHtml = true;
                  m.Priority = Priority;
                  m.Body = Message;
                  foreach ( MailAddress t in To ) {
                      m.To.Add( t );
                  }
                  foreach(Attachment a in Attachments) {
                      m.Attachments.Add(a);
                  }
  
                  try {
                      await s.SendMailAsync(m);
                      result = true;
                  } catch ( SmtpFailedRecipientException ef ) {
  
                  } catch ( SmtpException es ) {
  
                  } catch ( Exception e ) {
  
                  }
  
              }
              return result;
  
          }
      }
  
  }
  </code></pre>
  
  <p>And the way I am currently using it (testing), is as follows :</p>
  
  <pre><code>    private async void button2_Click( object sender, EventArgs e ) {
          Email m = new Email();
          m.SMTPAuth.Username = "foo@example.com";
          m.SMTPAuth.Password="mypassword";
          m.Host = "mail.example.com";
          m.Port = 366;
          m.From = new System.Net.Mail.MailAddress("foo@example.com","Company Name - NoReply");
  
          System.Net.Mail.MailAddressCollection c = new System.Net.Mail.MailAddressCollection();
          c.Add("some.account@gmail.com");
  
          List&lt;Attachment&gt; a = m.Attachments( @"D:\code.png" );
  
          bool sent = await m.SendMail( "Testing", "&lt;h1&gt;Hello&lt;/h1&gt;", c, System.Net.Mail.MailPriority.High, a );
          this.Text = "Message Sent : " + sent.ToString();
      }
  </code></pre>
  
  <p>Mail headers (from message received)</p>
  
  <pre><code>From: "Company Name - NoReply" &lt;-unknown-@example.com&gt;
  To: example@gmail.com, support@example.com
  Reply-To: "Company Name - NoReply" &lt;noreply@example.com&gt;
  X-Priority: 1
  Priority: urgent
  Importance: high
  Date: 1 May 2015 21:15:29 -0400
  Subject: Testing
  Content-Type: multipart/mixed;
   boundary=--boundary_0_7310d152-dff2-4139-9db1-2558b3e70e73
  X-ACL-Warn: {
  X-AntiAbuse: This header was added to track abuse, please include it with any abuse report
  X-AntiAbuse: Primary Hostname - mail.example.com
  X-AntiAbuse: Original Domain - gmail.com
  X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12]
  X-AntiAbuse: Sender Address Domain - example.com
  X-Get-Message-Sender-Via: mail.example.com: acl_c_relayhosts_text_entry: -unknown-@example.com|example.com
  X-From-Rewrite: rewritten was: [noreply@example.com], actual sender does not match
  </code></pre>
  
  <p>How can I prevent it from changing the string preceding the @ symbol in my from email address to <code>-unknown-</code> ? -- and why is it doing this ?</p>