我已经有一个邮件服务,但是我需要使用不同于地址的服务,因为这个服务被许多服务使用。现在,我有这个代码,它工作正常:
public static bool SendMail(Mail mail)
{
var smtp = new SmtpClient();
var credential = (NetworkCredential) smtp.Credentials;
var mailMessage = new MailMessage
{
From = new MailAddress(credential.UserName, mail.DisplayName),
Subject = mail.Subject,
Body = mail.Body,
IsBodyHtml = true
};
mailMessage.To.Add(new MailAddress(mail.To));
if (!string.IsNullOrEmpty(mail.TemplatePath))
mailMessage = embedImages(mailMessage, mail);
smtp.Send(mailMessage);
return true;
}
> And the web.config:
<mailSettings>
<smtp from="mail@gmail.com">
<network host="smtp.gmail.com" enableSsl="true" port="587" userName="mail@gmail.com" password="123456" />
</smtp>
</mailSettings>
> The Mail parameter, is an object:
public class Mail
{
public string Subject { get; set; }
public string Body { get; set; }
public string To { get; set; }
public string TemplatePath { get; set; }
public string DisplayName { get; set; }
public string From { get; set; }
}
因此,默认情况下,它应该使用 mailSettings,但是,如果属性 mail.From != null,它应该由该邮件发送。
谢谢