1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 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)
- {
- 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;
- 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());
- }
-
- }
- }
-
- }
|