Set "From" address when using System.Net.Mail.MailMessage?
Asked Answered
W

1

15

I'm trying to send a password reset email, but I'm having trouble figuring out how to specify the sender's address.

Here's what I'm trying to do:

MailMessage mail = new MailMessage();
mail.From.Address = "[email protected]";
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "<a href=\"" + url + "\">Click here to reset your password.</a>";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);

I'm sure it's possible, so how can I accomplish this in ASP.Net?

Warmongering answered 21/6, 2013 at 21:56 Comment(3)
What's your code? What have you tried?Plexiform
Are you using MailMessage class or something else?Choli
Oh sorry, my fault. So what is wrong with your code? Does it not work?Choli
W
21

It turns out I was getting ahead of myself.

Removing Address from mail.From.Address allowed me to set the value, but needed the type MailAddress.

Here's the solution:

MailMessage mail = new MailMessage();
mail.From = new MailAddress("[email protected]");
mail.To.Add(Email);
mail.Subject = "Forgot Password";
mail.Body = "<a href=\"" + url + "\">Click here to reset your password.</a>";
SmtpClient smtp = new SmtpClient();
smtp.SendAsync(mail, null);
Warmongering answered 21/6, 2013 at 22:1 Comment(1)
Great, glad you posted your own solution.Choli

© 2022 - 2024 — McMap. All rights reserved.