C# Send email using Office 365 SMTP doesn't change given email display name

1.3k views Asked by At

I am using C# MailMessage for sending Email through office 365, and I want to change the display name of the sender in the email.

I have tried using mailMessage MailAddress Constructor like this

mailMessage.From = new MailAddress("email","display name");

but it doesn't solve the problem

But when I tried to use Gmail instead, the display name is changed.

1

There are 1 answers

1
Frank Ball On

This is our generic SMTP email function. It includes the sender's email address and name.

 public static bool EmailReport(
               String Subject,
               String Body,
               String FromAddress,
               String FromName
               String[] To,
               String[] CC,
               out String sError)
        {
            MailMessage m = new MailMessage();
            SmtpClient smtp = new SmtpClient("<insert your email server name here i.e.: mail.Mycompany.com>");
            smtp.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
            m.Subject = Subject;
            m.Body = Body;
            m.From = new MailAddress(FromAddress, FromName);
            foreach (String sTo in To)
            {
                m.To.Add(sTo);
            }
            if (CC != null)
            {
                foreach (String sCC in CC)
                {
                    m.CC.Add(sCC);
                }
            }
            try
            {
                smtp.Send(m);
                sError = "";
                return true;
            }
            catch (Exception ex)
            {
                sError = ex.Message + "\r\n\r\n" + ex.StackTrace;
                return false;
            }
        }