1. 程式人生 > >.Net 發送郵件

.Net 發送郵件

n) ews sage source util smtp display 主題 live

private static string Host = System.Configuration.ConfigurationManager.AppSettings["SMTPURL"].ToString(); //SMTP服務器地址
private static string Account = System.Configuration.ConfigurationManager.AppSettings["SMTPACCOUNT"].ToString(); //SMTP服務帳號
private static string Pwd = System.Configuration.ConfigurationManager.AppSettings["SMTPPWD"].ToString(); //SMTP服務密碼
private static string From = System.Configuration.ConfigurationManager.AppSettings["SMTPFROM"].ToString(); //發送方郵件地址

技術分享圖片
        public static int SendMail(string subject, string body, string toMail,ref string msg,string icon="")
        {
            int reslult = -1;
            string To = System.Web.HttpUtility.UrlDecode(toMail.Trim());   // 收件方郵件地址

            SmtpClient _smtpClient = new SmtpClient();
            _smtpClient.DeliveryMethod 
= SmtpDeliveryMethod.Network;//指定電子郵件發送方式 _smtpClient.Host = Host; ;//指定SMTP服務器 _smtpClient.Credentials = new System.Net.NetworkCredential(Account, Pwd);//用戶名和密碼 MailMessage _mailMessage = new MailMessage(From, To); AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(body, null
, "text/html"); if (!string.IsNullOrEmpty(icon)) { LinkedResource lrImage = new LinkedResource(icon, "image/gif"); lrImage.ContentId = "weblogo"; htmlBody.LinkedResources.Add(lrImage); _mailMessage.AlternateViews.Add(htmlBody); } _mailMessage.Subject = System.Web.HttpUtility.UrlDecode(subject); //主題 _mailMessage.Body = System.Web.HttpUtility.UrlDecode(body);//內容 _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文編碼 _mailMessage.IsBodyHtml = true;//設置為HTML格式 _mailMessage.Priority = MailPriority.High;//優先級 try { _smtpClient.Send(_mailMessage); reslult = 1; msg = "發送成功"; } catch (Exception ex) { reslult = -1; msg = ex.Message; } return reslult; }
View Code

.Net 發送郵件