分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

.Net 发送邮件

发布时间:2023-09-06 01:33责任编辑:胡小海关键词:邮件

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 发送邮件

原文地址:https://www.cnblogs.com/plming/p/8142975.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved