邮件发送
来源:互联网 发布:网络陪聊怎么赚钱 编辑:程序博客网 时间:2024/05/24 08:33
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Configuration;namespace TCEEmail{ class Program { static void Main(string[] args) { //接收者的邮件地址 string strReceivers = ConfigurationManager.AppSettings["receiver"].ToString(); string[] ArrMailto = strReceivers.Split(';'); //抄送者的邮件地址 string strCopytos = ConfigurationManager.AppSettings["copyto"].ToString(); string[] ArrMailcc = strCopytos.Split(';'); string Content = "this is the test"; EmailHelp.SendEmail(Content, ArrMailto, ArrMailcc); Console.ReadKey(); } }}
发送类:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;using System.IO;using System.Text.RegularExpressions;namespace TCEEmail{ class EmailHelp { /// <summary> /// 邮件发送程序 /// </summary> /// <param name="strbody">发送的主体信息</param> /// <param name="ArrMailTo">接收人</param> /// <param name="ArrMailCc">抄送</param> public static void SendEmail(string strbody, string[] ArrMailTo, string[] ArrMailCc) { try { int Port = 587; int.TryParse(System.Configuration.ConfigurationManager.AppSettings["Port"].ToString(), out Port); string Email = System.Configuration.ConfigurationManager.AppSettings["Email"].ToString(); string EmailPwd = System.Configuration.ConfigurationManager.AppSettings["EmailPwd"].ToString(); string Smtp = System.Configuration.ConfigurationManager.AppSettings["Smtp"].ToString(); //验证是否为正确的邮件地址 Regex regMail = new Regex(@"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$"); MailMessage mail = new MailMessage(); SmtpClient SmtpServer = new SmtpClient(Smtp); mail.From = new MailAddress(Email); foreach (var item in ArrMailTo) { if (regMail.Match(item).Success) { mail.To.Add(item); } } foreach (var item in ArrMailCc) { if (regMail.Match(item).Success) { mail.CC.Add(item); } } mail.Subject = "xxxxxx"; if (strbody.Equals("")) { mail.Body = DateTime.Today.ToLocalTime().ToString() + "—" + DateTime.Now.ToString() + "xxxxxxx"; } else { mail.Body = strbody; } mail.BodyEncoding = Encoding.Default; //发送优先级 mail.Priority = MailPriority.High; SmtpServer.Credentials = new System.Net.NetworkCredential(Email, EmailPwd); SmtpServer.EnableSsl = true; SmtpServer.Send(mail); Console.WriteLine("Mail sent successfully"); ErrorLog("SendmailLog" + ".txt", "【" + string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + "】:Mail sent successfully"); } catch (Exception ex) { Console.WriteLine("the mail sends out the failure:" + ex.ToString()); ErrorLog("SendmailLog" + ".txt", "【" + string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now) + "】:the mail sends out the failure \n" + ex.ToString()); } } /// <summary> /// record the error log /// </summary> /// <param name="FileName">file name of log</param> /// <param name="FileContent">error information</param> private static void ErrorLog(string FileName, string FileContent) { try { string strPath = System.IO.Directory.GetCurrentDirectory() + "\\Log"; if (!Directory.Exists(strPath)) { // Create the directory it does not exist. Directory.CreateDirectory(strPath); } if (FileName.IndexOf("/") != -1) { FileName = FileName.Replace("/", "-"); } string strLogName = strPath + "\\" + FileName; FileInfo LogFile = new FileInfo(strLogName); StreamWriter TextFile = LogFile.AppendText(); //如果日志文件大于50M if (LogFile.Length < 50000000) { TextFile.WriteLine(FileContent); } TextFile.Close(); } catch (Exception e) { Console.WriteLine(e.ToString()); } } }}配置文件:
<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <!--接收人邮箱--> <add key="receiver" value="xxx@google.com"/> <!--抄送的邮箱--> <add key="copyto" value="xxx@google.com;"/> <!--Mail参数--> <add key ="Email" value="xxx@google.com"/> <add key ="EmailPwd" value="123456"/> <add key ="Smtp" value="smtp.gmail.com"/> <add key ="Port" value="587"/> </appSettings></configuration>
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 发送邮件
- 邮件发送
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 邮件发送
- 邮件发送
- 发送邮件
- 邮件发送
- 邮件发送
- 发送邮件
- 发送邮件
- 发送邮件
- 规则引擎--移动结算系统开发
- Ubuntu中SVN客户端安装+使用
- Android OpenGL ES 简明开发教程三:3D绘图基本概念
- Spring3 MVC+JdbcTemplate(含分页) 目前最详细登陆实例
- sc2440串口驱动
- 邮件发送
- JS事件--数字与字母之间转换
- 几米经典语录
- 规则引擎集成数据库操作
- send a client certificate by using the HttpWebRequest and HttpWebResponse classes
- java下载文件的几种方式
- Oracle On Demand(OOD)项目中EBS打补丁的详细步骤
- 堆排序算法
- libnids中的API文档翻译