從類傳送電子郵件
這種方式可能非常有用,但是,有些人(像我一樣)是重複程式碼的遺骸,就像你在我們身上一樣,這意味著我需要在每個我們擁有的專案中建立一個具有相同程式碼的聯絡控制器,所以,我覺得這也有幫助
這是我的類,可以在 DLL 或其他任何東西上
public class Emails
{
public static void SendHtmlEmail(string receiverEmail, string subject, string body, bool Ssl = false)
{
//Those are read it from webconfig or appconfig
var client = new SmtpClient(ConfigurationManager.AppSettings["MailServer"], Convert.ToInt16
(ConfigurationManager.AppSettings["MailPort"]))
{
Credentials = new NetworkCredential(ConfigurationManager.AppSettings["MailSender"], ConfigurationManager.AppSettings["MailSenderPassword"]),
EnableSsl = Ssl
};
MailMessage message = new MailMessage();
message.From = new MailAddress(ConfigurationManager.AppSettings["MailSender"]);
message.To.Add(receiverEmail);
// message.To.Add("sgermosen@praysoft.net");
message.Subject = subject;
message.IsBodyHtml = true;
message.Body = body;
client.Send(message);
}
}
就像你看到它會從 webconfig 中讀取一樣,所以,我們需要配置它,這個配置適用於 Gmail,但是,每個主機都有自己的配置
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
<add key="AdminUser" value="sgrysoft@gmail.com" />
<add key="AdminPassWord" value="123456789" />
<add key="SMTPName" value="smtp.gmail.com" />
<add key="SMTPPort" value="587" />
</appSettings>