1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > c#发送邮件(带抄送 密送 群发 附件)

c#发送邮件(带抄送 密送 群发 附件)

时间:2023-02-27 05:41:30

相关推荐

c#发送邮件(带抄送 密送 群发 附件)

using System;using System.Collections.Generic;using System.IO;using System.Linq;using .Mail;using .Mime;using System.Text;using System.Threading.Tasks;namespace Test.Demo.Mvc.Hepler{///

/// 发送邮件帮助类///public class EmailHelper{//申明邮件操作类private MailMessage mail = new MailMessage();////// 发送邮件,(抄送人,密送人,回复地址,所有指示标题非必须,可为null)//////

发送人的邮箱///

发送邮箱的授权码///

收件人集合,list类型///

邮件的标题,如(xxxx问题反馈)///

邮件的正文部分///

附件地址///

抄送人集合,list类型///

密送人集合,list类型///

邮件指示标题,如(xxxx问题反馈)///

抄送人的指示标题///

密送人的指示标题///

邮件的回复地址邮箱///

邮件的回复地址邮箱指示标题///

指定发送邮件的服务器地址或IP ///

指定发送邮件端口 ,默认端口/// boolpublic bool sendEmail(string SenderName, string SendCode, ListTosend, string title, string content, string filepath, ListCC, ListBCC, string SendTitle, string CCtitle, string BCCtitle, string Accept_reply, string acctitle, string host, int post){bool success = false;//string SenderName = "xxxx@";//发送人的邮箱,后期读取配置文件中对应的数据//string SendCode = "xxxxx";//发送邮箱的授权码bool fj = false;//判断附件是否添加成功try{foreach (var item in Tosend){mail.To.Add(item);//将多个收件人添加到队列中}mail.From = new MailAddress(SenderName, SendTitle);//将发送人添加到队列中,第二个参数表示邮件中看到的抬头部分mail.Subject = title;//标题mail.SubjectEncoding = Encoding.UTF8;//设置编码格式mail.Body = content;//邮件内容mail.BodyEncoding = Encoding.UTF8;mail.IsBodyHtml = true;mail.Priority = MailPriority.High; //发送邮件的优先等级 if (!string.IsNullOrWhiteSpace(Accept_reply)) {if (!string.IsNullOrWhiteSpace(acctitle)) {mail.ReplyTo = new MailAddress(Accept_reply, acctitle, Encoding.GetEncoding(936));//回复的邮箱}else{mail.ReplyTo = new MailAddress(Accept_reply);//回复的邮箱}}//抄送if (null != CC && CC.Count > 0) {foreach (var item in CC){if (!string.IsNullOrWhiteSpace(CCtitle)){.Add(new MailAddress(item, CCtitle, Encoding.GetEncoding(936)));}else{.Add(new MailAddress(item));}}}if (null != BCC && BCC.Count > 0) {//密送foreach (var item in BCC){if (!string.IsNullOrWhiteSpace(BCCtitle)){mail.Bcc.Add(new MailAddress(item, BCCtitle, Encoding.GetEncoding(936)));}else{mail.Bcc.Add(new MailAddress(item));}}}SmtpClient sc = new SmtpClient();//允许应用程序使用简单的邮件传输协议sc.EnableSsl = true;//是否SSL加密// sc.Host = ""; //指定发送邮件的服务器地址或IP //sc.Port = 25; //指定发送邮件端口 ,默认端口sc.Host = host; //指定发送邮件的服务器地址或IP sc.Port = post; //指定发送邮件端口 ,默认端口sc.Credentials = new workCredential(SenderName, SendCode); //指定登录服务器的用户名和密码(注意:这里的密码是开通上面的pop3/smtp服务提供给你的授权密码,不是你的qq密码)if (!string.IsNullOrEmpty(filepath) && filepath != ""){if (!ToEnclosure(filepath))//添加附件{msg = "添加附件失败";}else{sc.Send(mail);msg = "发送邮件成功";}}//ToEnclosure("C:\\Users\\fanhuquan\\Downloads\\extjs-ux-master.zip");//sc.Send(mail);success = true;}catch (Exception ex){success = false;}return success;}///

/// 发送附件,返回bol类型//////

附件的路径private bool ToEnclosure(string filepath){try{string[] path = filepath.Split(','); //以什么符号分隔可以自定义Attachment data;ContentDisposition disposition;for (int i = 0; i < path.Length; i++){data = new Attachment(path[i], MediaTypeNames.Application.Octet);disposition = data.ContentDisposition;disposition.CreationDate = System.IO.File.GetCreationTime(path[i]);disposition.ModificationDate = System.IO.File.GetLastWriteTime(path[i]);disposition.ReadDate = System.IO.File.GetLastAccessTime(path[i]);mail.Attachments.Add(data);//添加附件}return true;}catch (Exception ex){return false;}}}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。