123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using MailKit.Net.Smtp;
- using MimeKit;
- namespace wispro.sp.utility
- {
- public class MailUtil
- {
- public static void SendEmail(string subject, string body,string toMailName, string toEmail,string AttachFiles=null)
- {
- MimeMessage message = new MimeMessage();
- string strMail = ConfigHelper.GetSectionValue("MailSetting:mail");
- MailboxAddress from = new MailboxAddress("绩效系统",strMail);
- message.From.Add(from);
- MailboxAddress to = new MailboxAddress(toMailName, toEmail);
- message.To.Add(to);
-
- message.Subject = subject;
-
- BodyBuilder bodyBuilder = new BodyBuilder();
- bodyBuilder.HtmlBody = body;
- if (!string.IsNullOrEmpty(AttachFiles))
- {
- var files = AttachFiles.Split(';');
- foreach(var file in files)
- {
- if (!string.IsNullOrEmpty(file.Trim()))
- {
- if (System.IO.File.Exists(file))
- {
- bodyBuilder.Attachments.Add(file);
- }
- }
- }
- }
- message.Body = bodyBuilder.ToMessageBody();
- SmtpClient client = new SmtpClient();
- var strServer = ConfigHelper.GetSectionValue("MailSetting:Server");
- var strPort = ConfigHelper.GetSectionValue("MailSetting:Port");
- var strAccount = ConfigHelper.GetSectionValue("MailSetting:Account");
- var strPassword = ConfigHelper.GetSectionValue("MailSetting:Password");
- client.Connect(strServer , int.Parse(strPort),true); //例如:smtp.exmail.qq.com,465
- client.Authenticate(strAccount, strPassword); //发送邮件的账户密码
- client.Send(message);
- client.Disconnect(true);
- client.Dispose();
- }
- private static void SendCompletedCallback(object sender, AsyncCompletedEventArgs e)
- {
- if(e.Error != null){
- System.Diagnostics.Debug.WriteLine(e.Error.ToString());
- }
-
- }
- }
-
- }
|