1.先添加引入命名空間 System.Net.Mail
2.組件代碼:
/// <summary>
/// 郵件發送組件
/// </summary>
public class EmailUtils
{
static EmailUtils() {
MailHost = ConfigurationManager.AppSettings["mailHost"];
MailUserName = ConfigurationManager.AppSettings["mailUserName"];
MailPsw = ConfigurationManager.AppSettings["mailPsw"];
MailFrom = ConfigurationManager.AppSettings["mailFrom"];
}
public static string MailFrom {
get;
set;
}
public static string MailHost {
get;
set;
}
public static string MailUserName {
get;
set;
}
public static string MailPsw {
get;
set;
}
/// <summary>
/// 發送郵件
/// </summary>
/// <param name="mailto">發送到</param>
/// <param name="title">標題</param>
/// <param name="msg"></param>
/// <returns></returns>
public static bool Send(string mailto, string title, string msg) {
var client = new SmtpClient();
client.Host = MailHost;
client.UseDefaultCredentials = false;
client.Credentials = new System