static void Main(string[] args) { //普通测试 SmtpMailClient client = new SmtpMailClient("smtp.163.com", 25, true, "XXX", "XXX"); client.Timeout = 18000; MailMessage msg = new MailMessage(); msg.From = new MailAccount("*****@*****.**", "janky"); msg.AddTo(new MailAccount("*****@*****.**", "测试1")); msg.AddTo(new MailAccount("*****@*****.**")); msg.AddCC(new MailAccount("*****@*****.**")); //msg.AddCC(new MailAccount("*****@*****.**")); msg.MailContent = "<p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>"; msg.Subject = "测试"; msg.IsHtml = true; client.Send(msg); //附件测试 client = new SmtpMailClient("smtp.163.com", 25, true, "XXX", "XXX"); client.Timeout = 18000; msg = new MailMessage(); msg.From = new MailAccount("*****@*****.**", "janky"); msg.AddTo(new MailAccount("*****@*****.**", "测试1")); msg.AddTo(new MailAccount("*****@*****.**")); msg.AddCC(new MailAccount("*****@*****.**")); msg.MailContent = "<img src='cid:test1234' /><p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>"; msg.Subject = "测试"; msg.IsHtml = true; msg.AddAttachment(@"c:\test\test.txt"); msg.AddAttachment(@"c:\test\taiwan.jpg", "test1234"); //这里是content-id msg.AddAttachment(@"c:\test\filechktool.rar"); client.SendAsync(msg); //ssl 测试 SmtpServerInfo info = new SmtpServerInfo("smtp.gmail.com", 465, true, "XXX", "janky,.XXX"); info.EnableSsl = true; client = new SmtpMailClient(info); client.Timeout = 18000; msg = new MailMessage(); msg.From = new MailAccount("*****@*****.**", "系统"); msg.AddTo(new MailAccount("*****@*****.**", "测试1")); msg.AddTo(new MailAccount("*****@*****.**")); msg.AddCC(new MailAccount("*****@*****.**")); msg.MailContent = "<p>IHU 发信IP因发送垃圾邮件或存在异常的连接行为,被暂时挂起。请检测发信IP在历史上的发信情况和发信程序是否存在异常;</p><a href='http://163.com'>163 邮箱</a>"; msg.Subject = "测试"; msg.IsHtml = true; client.Send(msg); Console.ReadKey(); }
/// <summary> /// 邮件头发送 /// </summary> /// <param name="writer"></param> /// <param name="message"></param> public static void SendHeader(Stream writer, MailMessage message) { Writer(Formater.DateFormat(DateTime.Now), message.Encoding, writer); //日期 Writer(Formater.FormFormat(message.DisplayFrom, message.Encoding), message.Encoding, writer); //发件人 Writer(Formater.TosFormat(message.Tos, message.Encoding), message.Encoding, writer); //收件人 Writer("REPLY-TO:" + message.DisplayFrom.MailAddress, message.Encoding, writer); //回复地址 if (message.CCs.Count > 0) { Writer(Formater.CCsFormat(message.CCs, message.Encoding), message.Encoding, writer); //抄送 } if (message.Mccs.Count > 0) { Writer(Formater.MCCsFormat(message.Mccs, message.Encoding), message.Encoding, writer);//密送 } Writer(Formater.PriorityFormat(message.priority), message.Encoding, writer); //优先级 Writer(Formater.SubjectFormat(message.Subject, message.Encoding), message.Encoding, writer); //标题 Writer(Formater.EncodingFormat("base64"), message.Encoding, writer); //编码 Writer("X-Mailer:DS Mail Sender V1.0", message.Encoding, writer); //发件人 Writer(Formater.MessageIdFormat(System.Guid.NewGuid().ToString()), message.Encoding, writer); Writer("MIME-Version:1.0", message.Encoding, writer); Writer("Content-Type: multipart/mixed; boundary=\"mailboundary\"\r\n", message.Encoding, writer); //分隔符 Writer(SP, message.Encoding, writer); //分隔符 }
/// <summary> /// 线程发送 /// </summary> /// <param name="message"></param> public void SendAsync(MailMessage message) { Thread t = new Thread(new ParameterizedThreadStart(this.SendMail)); t.IsBackground = true; t.Start(message); }
/// <summary> /// 发送邮件 /// </summary> /// <param name="message"></param> public void Send(MailMessage message) { this.SendMail(message); }
/// <summary> /// 验证数据 /// </summary> /// <param name="message"></param> private void CheckMailMessage(MailMessage message) { if (message == null) { throw new InfoMissingException("MailMessage"); } if (message.From == null) { throw new InfoMissingException("From"); } if (message.DisplayFrom == null) { message.DisplayFrom = message.From; } if (message.Tos.Count < 1) throw new InfoMissingException("To"); try { if (System.Text.Encoding.GetEncoding(message.Encoding) == null) { throw new InfoMissingException("Encoding"); } } catch { throw new InfoMissingException("Encoding"); } foreach (var account in message.Tos) { if (!CommonValidator.CheckEmail(account.MailAddress)) { throw new EmailAddressNotValidatorException(); } } foreach (var account in message.CCs) { if (!CommonValidator.CheckEmail(account.MailAddress)) { throw new EmailAddressNotValidatorException(); } } foreach (var att in message.Attachment) { if (!CommonValidator.CheckFile(att.FileName)) { throw new AttchmentException("附件不正确"); } } if (!CommonValidator.CheckEmail(message.From.MailAddress)) { throw new EmailAddressNotValidatorException(); } }