1、适合于发件人、收件人等信息全部是string类型
| using System.Collections; |
| using System.Net; |
| using System.Net.Mail; |
| using System.Text; |
| |
| private void SendMail(string toAddress,string ccAddress,string subject,string body) |
| { |
| string[] arrToAddress = toAddress.Split(';'); |
| string[] arrCcAddress = ccAddress.Split(';'); |
| |
| MailMessage mailMessage = new MailMessage(); |
| SmtpClient smtpClient = new SmtpClient(); |
| if (toAddress.Length > 0) |
| { |
| foreach (string item in arrToAddress) |
| { |
| if (!string.IsNullOrEmpty(item.Trim())) |
| { |
| mailMessage.To.Add(item.Trim()); |
| } |
| } |
| } |
| if (ccAddress.Length > 0) |
| { |
| foreach (string item in arrCcAddress) |
| { |
| if (!string.IsNullOrEmpty(item.Trim())) |
| { |
| mailMessage.To.Add(item.Trim()); |
| } |
| } |
| } |
| try |
| { |
| mailMessage.From = new MailAddress(""); |
| mailMessage.Subject = subject; |
| mailMessage.Body = body; |
| mailMessage.IsBodyHtml = true; |
| mailMessage.BodyEncoding = System.Text.Encoding.UTF8; |
| mailMessage.Priority = MailPriority.Normal; |
| |
| |
| |
| |
| smtpClient.Host = "172.20.168.106"; |
| smtpClient.Port = 25; |
| smtpClient.UseDefaultCredentials = false; |
| smtpClient.EnableSsl = false; |
| smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; |
| smtpClient.Send(mailMessage); |
| } |
| catch (Exception e) |
| { |
| throw (e); |
| } |
| } |
2、适合于暗抄送、并且是ArrayList类型
| using System.Collections; |
| using System.Net; |
| using System.Net.Mail; |
| using System.Text; |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| public static bool SendEmail(ArrayList? To, ArrayList? CC, ArrayList? BCC, string Subject, string Body, ArrayList? Attach, bool IsHTML, out string Msg) |
| { |
| try |
| { |
| Msg = "Y|"; |
| SmtpClient smtpClient = new() |
| { |
| Host = "127.0.0.1", |
| DeliveryMethod = SmtpDeliveryMethod.Network, |
| Credentials = new NetworkCredential("username", "password"), |
| }; |
| MailMessage mailMessage = new() |
| { |
| BodyEncoding = Encoding.UTF8, |
| Subject = Subject, |
| Body = Body, |
| IsBodyHtml = IsHTML, |
| From = new MailAddress("fajianren@163.com") |
| }; |
| if (To != null) |
| { |
| foreach (string item in To) |
| { |
| mailMessage.To.Add(item); |
| } |
| } |
| |
| if (CC != null) |
| { |
| foreach (string item2 in CC) |
| { |
| mailMessage.CC.Add(item2); |
| } |
| } |
| |
| if (BCC != null) |
| { |
| foreach (string item3 in BCC) |
| { |
| mailMessage.Bcc.Add(item3); |
| } |
| } |
| |
| if (Attach != null) |
| { |
| foreach (string Attachment in Attach) |
| { |
| mailMessage.Attachments.Add(new Attachment(Attachment)); |
| } |
| } |
| smtpClient.Send(mailMessage); |
| mailMessage.Dispose(); |
| return true; |
| } |
| catch (Exception ex) |
| { |
| Msg = "N|"+ex.Message; |
| return false; |
| } |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库