使用System.Net.Mail.MailMessage时设置“发件人”地址?

时间:2013-06-21 21:56:43

标签: c# asp.net-mvc email system mailmessage

我正在尝试发送密码重置电子邮件,但我无法确定如何指定发件人的地址。

这是我正在尝试做的事情:

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);

我确信这是可能的,那么如何在ASP.Net中实现这一目标呢?

1 个答案:

答案 0 :(得分:16)

事实证明我已经超越了自己。

Address删除mail.From.Address,我可以设置值,但需要MailAddress类型。

以下是解决方案:

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);