Chris Johnson

Chris Johnson

  • NA
  • 41
  • 4.6k

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 [email protected] 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 [email protected] to [email protected]

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

MailMessage onboarding = new MailMessage();
onboarding.From = new MailAddress("[email protected]");
onboarding.To.Add(new MailAddress("[email protected]"));
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("[email protected]", "Password");
try
{
    client.Send(onboarding);
    lblError.ForeColor = System.Drawing.Color.Blue;
    lblError.Text = "Onboarding request successful";

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

 


Answers (6)