本帖最后由 tail88 于 2023-10-21 09:55 编辑
背景因为工作需要,有时候需要通过邮件发送附件,但是邮件的附件大小又被限制在了25m,对于一些比较大的文件只能切片分开发送. 所以,如果切片很多的话,就很苦恼. 于是写了个批量发送邮件的小工具. 程序是C#写的,不是很熟写的比较粗糙.有很多想法还没有时间实现,后面大家需要的话我再补充叭 实现功能- 登录主流邮件服务
- 登陆后会收到一封测试的邮件,用来检验服务是否OK
- 不选择附件的话,只是单一的发送邮件
>这个功能很鸡肋,用主流邮件软件就好啦 - 选择附件文件夹后,会将文件夹中的附件依次发送到指定邮箱
> 如果失败了,再次点击发送就好,支持断点续发. 如果不放心重新选择附件文件夹 - 计划在邮件内容中新增魔法变量
>比如 {data} -> 表示时间 / {number} -> 表示邮件编号 / {fileName} - > 表示附件名称 > 暂时没时间,等后面如果大家需要我再补充 - 计划将Input框替换成富文本编辑,支持更多的内容格式
- 计划支持Html格式内容
核心代码由于工作原因软件源码就不放出来了,也不难,贴核心工具类叭: 邮件配置类 using System; using System.Net; using System.Net.Mail; namespace SendEmail.Util { public class MailConfig { private int protInt; //邮件服务的端口 private string smtpAddress; //SMTP地址 private string userAddress; //发送人地址 private string userPwd; //发送人密码or授权码 private Boolean enableSsl; // 是否开启SSL加密 /// <summary> /// 初始化函数 /// </summary> /// <param name="protStr">端口号</param> /// <param name="smtpAddress">SMTP地址</param> /// <param name="userAddress">登录用户地址</param> /// <param name="userPwd">登录用户密码</param> /// <param name="enableSsl">是否开启SSL加密通讯</param> public MailConfig(int protInt,String smtpAddress,String userAddress,String userPwd,Boolean enableSsl) { //这里优先校验是否初始化邮件的配置 this.ProtInt = protInt; this.SmtpAddress = smtpAddress; this.UserAddress = userAddress; this.UserPwd = userPwd; this.EnableSsl = enableSsl; } /// <summary> /// 尝试获取邮件服务的连接 /// </summary> public SmtpClient getClient() { SmtpClient client = new SmtpClient(smtpAddress); // SMTP的服务地址 client.Port = protInt; // 邮件服务的端口 client.Credentials = new NetworkCredential(userAddress, userPwd); client.EnableSsl = enableSsl; // 是否使用SSL连接 return client; } private int ProtInt { get => protInt; set => protInt = value; } private string SmtpAddress { get => smtpAddress; set => smtpAddress = value; } public string UserAddress { get => userAddress; set => userAddress = value; } private string UserPwd { get => userPwd; set => userPwd = value; } private bool EnableSsl { get => enableSsl; set => enableSsl = value; } } }
邮件发送类 using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Net; using System.Net.Mail; using System.Net.Mime; using System.Windows.Forms; namespace SendEmail.Util { public class MailUtils { // private static string toEmailAddress = "your_email@example.com";//收件人地址 // private static string ccEmailAddress = "your_email@example.com";//抄送人地址 // private static string attachmentPath = "your_email@example.com";//附件地址 /// <summary> /// 尝试发送测试邮件 /// </summary> /// <param name="client">邮件链接</param> /// <param name="userAddressStr">收件人地址</param> public Boolean sendTestEmail(SmtpClient client,String userAddressStr) { Boolean falg = false; if (client==null) { return falg; } MailMessage message = new MailMessage { From = new MailAddress(userAddressStr), Subject = "Test Email", Body = "如果您看到这条消息,意味着您的邮件配置一切正常." }; List<String> toEmailAddressList = new List<String>(); toEmailAddressList.Add(userAddressStr); var messageStr = sendEmail(client,message, toEmailAddressList, null, null); if (messageStr.Equals("Success")) { falg = true; } return falg; } /// <summary> /// 发送邮件 /// </summary> /// <param name="client">邮件链接</param> /// <param name="message">邮件内容</param> /// <param name="toEmailAddressList">发送地址列表</param> /// <param name="ccEmailAddressList">抄送地址列表</param> /// <param name="attachmentPath">附件</param> /// <returns></returns> public String sendEmail(SmtpClient client,MailMessage message,List<String> toEmailAddressList,List<String> ccEmailAddressList,List<String> attachmentPath ) { try { if (client==null) { return "连接不能为空!"; } if (toEmailAddressList!=null && toEmailAddressList.Count>0) { foreach (String toEmailAddress in toEmailAddressList) { message.To.Add(toEmailAddress); // 收件人地址 } } else { return "收件人不能为空!"; } if (ccEmailAddressList!=null && ccEmailAddressList.Count>0) { foreach (String ccEmailAddress in ccEmailAddressList) { message.CC.Add(ccEmailAddress); // 抄送人地址 } } if (attachmentPath!=null && attachmentPath.Count>0) { foreach (var attachmentStr in attachmentPath) { //添加附件 if (!String.IsNullOrEmpty(attachmentStr)) { if (File.Exists(attachmentStr)) { Attachment attachment = new Attachment(attachmentStr, MediaTypeNames.Application.Octet); ContentDisposition disposition = attachment.ContentDisposition; disposition.CreationDate = File.GetCreationTime(attachmentStr); disposition.ModificationDate = File.GetLastWriteTime(attachmentStr); disposition.ReadDate = File.GetLastAccessTime(attachmentStr); message.Atta[attach]2650272[/attach] } } } client.Send(message); return "Success"; } catch (Exception ex) { return $"出现异常: {ex.Message}"; } } } }
下方隐藏内容为本帖所有文件或源码下载链接:
游客你好,如果您要查看本帖隐藏链接需要登录才能查看,
请先登录
|