Chris Johnson

Chris Johnson

  • 2k
  • 34
  • 977

Is it possible to change the FROM email address in c#?

Jul 7 2021 10:14 AM

Hi,

I have a business application but I want the email address Onboarding@Mycompany.com to give a more meaningful from address.
As you can see below I am using gmail but I wish to use our official url as the email address. I could and will of course use our official ORMS server but this is just a system I wish to demonstrate. 

So onboarding.Mycompany@gmail.com to Onboarding@mycompany.com

I thought the code below would work but it still arrives as the Gmail address.

MailMessage onboarding = new MailMessage();
onboarding.From = new MailAddress("onboarding@Mycompany.com");
onboarding.To.Add(new MailAddress("cjohnson@Mycompany.com"));
onboarding.Subject = "You have an Onboarding request";
onboarding.Body = "You have a new onboarding request from " + " " + Requestor.Text + " for " + Name.Text + ". " + "\r\n";
onboarding.IsBodyHtml = true;
SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential("onboarding.MyCompany@gmail.com", "Password");
try
{
    client.Send(onboarding);
    lblError.ForeColor = System.Drawing.Color.Blue;
    lblError.Text = "Onboarding request successful";

}
catch
{
    lblError.Text = "Mail failed to send";
}

 


Answers (4)