C# · 12月 29, 2021

c# – 使用System.Net.Mail.MailMessage设置“From”地址?

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

这是我要做的:

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中完成这个?

解决方法 原来我正在领先于自己.

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