C#代码:
/// <summary> /// 发送邮件 /// </summary> /// <param name="subject"></param> /// <param name="emails"></param> /// <param name="filePath"></param> public static void SendEmail(string subject, string[] emails, string filePath) { string text = ""; string html = "您好,这是系统自动发送的邮件。请不要直接回复。"; string sendEmailUserName = System.Configuration.ConfigurationManager.AppSettings["SmtpEmailUserName"]; string sendEmail = System.Configuration.ConfigurationManager.AppSettings["SmtpEmailAddress"]; string sendEmailPWD = System.Configuration.ConfigurationManager.AppSettings["SmtpEmailPassword"]; string SmtpServer = System.Configuration.ConfigurationManager.AppSettings["SmtpServer"]; int SmtpServerPort = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SmtpServerPort"]); using (SmtpClient smtpClient = new SmtpClient()) { using (MailMessage msg = new MailMessage()) { msg.Subject = subject; msg.From = new MailAddress(sendEmail); foreach (var item in emails) { msg.To.Add(new MailAddress(item)); } msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(text, null, MediaTypeNames.Text.Plain)); msg.AlternateViews.Add(AlternateView.CreateAlternateViewFromString(html, null, MediaTypeNames.Text.Html)); msg.Attachments.Add(new Attachment(filePath)); System.Net.NetworkCredential credentials = new System.Net.NetworkCredential(sendEmailUserName, sendEmailPWD); smtpClient.Host = SmtpServer; smtpClient.Port = SmtpServerPort; smtpClient.Credentials = credentials; smtpClient.EnableSsl = false; smtpClient.Send(msg); } } }
haha
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /// <summary> /// 发送邮件 /// </summary> /// <param name="context"></param> /// <returns></returns> public string SendMail(HttpContext context) { try { if (! string .IsNullOrEmpty(CookiesHelper.getCookie( "send_mail_limit" ))) { return "-5" ; //每分钟只能发送一次 } string email = context.Request[ "email" ]; if ( string .IsNullOrEmpty(email) || !CommonHelper.IsValidEmail(email)) { return "-1" ; //传值为空 } //依据模板生成发送内容 string sendText = "" ; string tempPath = context.Server.MapPath( "~/EmailTemp/ModifyPwd.txt" ); using (StreamReader sr = new StreamReader(tempPath)) { sendText = sr.ReadToEnd(); } sendText = sendText.Replace( "{UserName_CH}" , "星辰" ); sendText = sendText.Replace( "{UserName_EN}" , "star" ); sendText = sendText.Replace( "{VCode}" , "abks" ); CommonHelper.SendEmail(email, sendText, Resource.Lang.RetrievePassword); CookiesHelper.setCookie( "send_mail_limit" , "SendMail" , 1.00); return "1" ; //成功 } catch (Exception) { return "-4" ; //异常 } } |
邮件模板:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 亲爱的 <b>{UserName_CH}</b>,您好! <br/> 您在本平台上提交了修改密码的请求。 <br/> 验证码为:<b>{VCode}</b>,注意区分大小写! <br/> 请按照页面提示完成密码的修改。 <br/> (系统邮件,请勿回复) <br/> <br/> <br/> Dear <b>{UserName_EN}</b> , <br/> You have submitted a request to change the password on the platform. <br/> Verificationcode is <b>{VCode}</b> ,please note that the code is case sensitive! <br/> Enjoy your time ! <br/> (Please do not reply.) |
C#发送代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /// <summary> /// 发送邮件1 /// </summary> /// <param name="AcceptEmail"></param> /// <param name="sendText"></param> public static void SendEmail( string AcceptEmail, string sendText, string title) { SendSMTPEMail(mail_smtp, mail_main, mail_pwd, AcceptEmail, title, sendText); } /// <summary> /// 发送邮件2 /// </summary> /// <param name="strSmtpServer"></param> /// <param name="strFrom"></param> /// <param name="strFromPass"></param> /// <param name="strto"></param> /// <param name="strSubject"></param> /// <param name="strBody"></param> public static void SendSMTPEMail( string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody) { SmtpClient client = new SmtpClient(strSmtpServer); client.UseDefaultCredentials = false ; client.Credentials = new System.Net.NetworkCredential(strFrom, strFromPass); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Port = mail_port; client.EnableSsl = mail_ssl == "yes" ; MailMessage message = new MailMessage(strFrom, strto, strSubject, strBody); message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true ; client.Send(message); } |
C#配置代码:
1 2 3 4 5 6 | //邮件配置 public static string mail_smtp = System.Configuration.ConfigurationManager.AppSettings[ "mail_smtp" ]; public static string mail_main = System.Configuration.ConfigurationManager.AppSettings[ "mail_main" ]; public static string mail_pwd = System.Configuration.ConfigurationManager.AppSettings[ "mail_pwd" ]; public static int mail_port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings[ "mail_port" ]); public static string mail_ssl = System.Configuration.ConfigurationManager.AppSettings[ "mail_ssl" ]; |
web.config:
1 2 3 4 5 6 | <!--邮件配置--> <add key= "mail_smtp" value= "smtp.ym.163.com" /> <add key= "mail_main" value= "xxxxx@xxxxx.com" /> <add key= "mail_pwd" value= "xxxxxx" /> <add key= "mail_port" value= "25" /> <add key= "mail_ssl" value= "no" /> |
使用CDO.Message发送邮件:腾讯企业邮箱有点特别,以上的方法都发送不了,最后找到这个方法可以发送。要引用一个dll,地址:C:\Windows\System32\cdosys.dll
public static void SendMailByCDO() { CDO.Message objMail = new CDO.Message(); try { objMail.To = "xxx@qq.com";//要发送给哪个邮箱 objMail.From = "xxx@xxx.cn";//你的邮件服务邮箱 objMail.Subject = "这是标题";//邮件主题 objMail.HTMLBody = "这里可以填写html内容";//邮件内容 html objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;//设置端口 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "smtp.exmail.qq.com"; objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = "xxx@xxx.cn";//发送邮件账户 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = "xxx@xxx.cn";//发送邮件账户 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = "xxx@xxx.cn";//发送邮件账户 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = "xxx@xxx.cn";//发送邮件账户 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = "xxx";//发送邮件账户密码 objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//是否使用ssl //防止中文乱码 objMail.HTMLBodyPart.Charset = "utf-8"; objMail.BodyPart.Charset = "utf-8"; objMail.Configuration.Fields.Update(); objMail.Send(); } catch (Exception ex) { throw ex; } finally { } System.Runtime.InteropServices.Marshal.ReleaseComObject(objMail); objMail = null; }
System.Web.Mail:http://www.codingwhy.com/view/616.html
分类:
.NET
标签:
CDO.Message
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】