简体   繁体   中英

Set “From” address when using System.Net.Mail.MailMessage?

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 = "support@mycompany.com";
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?

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("support@mycompany.com");
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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM