傳送郵件封裝的方法
阿新 • • 發佈:2018-11-29
/// <summary> /// 傳送郵件 /// </summary> /// <param name="fromMail">發件人郵箱</param> /// <param name="toMail">收件人郵箱</param> /// <param name="subject">主題</param> /// <param name="body">內容</param> /// <param name="clientHost">設定用於 SMTP 事務的主機的名稱或 IP 地址</param> /// <param name="roleCode">生成授權碼</param> /// <returns></returns> public static bool SendMessage(string fromMail, string toMail, string subject, string body, string clientHost, string roleCode) { MailMessage mailMessage= new MailMessage(); //發件人郵箱 mailMessage.From = new MailAddress(fromMail); //收件人郵箱 mailMessage.To.Add(new MailAddress(toMail)); //主題 mailMessage.Subject = subject; //內容 mailMessage.Body = body; mailMessage.Sender= new MailAddress(fromMail); SmtpClient client = new SmtpClient(); client.Host = clientHost; //使用安全加密連線。 client.EnableSsl = true; //不和請求一塊傳送。 client.UseDefaultCredentials = false; //驗證發件人身份(發件人的郵箱,郵箱裡的生成授權碼); client.Credentials = new NetworkCredential(fromMail, roleCode); //傳送 try { client.Send(mailMessage); } catch (Exception ex) { return false; } return true; }