C# 发送smtp,exchange邮件
1.exchange邮件
微软的Exchange邮件服务不同与一般的邮件server,他不能简单使用SmtpClient等组件实现邮件收发的功能。
那么怎么通过Exchange服务发送邮件呢?
微软的Exchange服务都有webservice接口, 通过其提供的webservice发送邮件也是最简单的方法。
验证Exchange服务的webservice是否正常:https://xx.xxxxx.com/ews/exchange.asmx, 能打开就是正常。可能须要登录。
可是这个webservice不同于普通的webservice,不能简单的加入web引用来调用。
首先我们须要下载一个Microsoft.Exchange.WebServices.dll,然后引用到我们的项目里边,是个msi,安装好之后就有那个dll,然后就能够開始了。。
以下是一个发送邮件的样例:
using Microsoft.Exchange.WebServices; using Microsoft.Exchange.WebServices.Data; private void SendEmail() { ExchangeService service = new ExchangeService(); // 获取身份验证, 能够尝试你的邮箱名, 域用户ID等 // user: 登录username,邮箱登录ID或者域登录ID // password:你懂的 // domain:域,不是域名,域用户的的话就得填这个,选填 service.Credentials = new NetworkCredential("user", "password", "domain"); service.TraceEnabled = true; service.AutodiscoverUrl("zhangsan@yuming.com"); // 这个是发件然的邮箱地址,完整的(xx@dd.com). EmailMessage message = new EmailMessage(service); message.Subject = "Email的主题"; message.Body = "Email的内容"; message.ToRecipients.Add("收件人的邮件地址"); message.Save(); message.SendAndSaveCopy();// 发送 }
2.smtp 邮件
2.1 outlook
for (int j = 0; j < 10; j++) { try { SmtpClient smtp3 = new SmtpClient(); smtp3.UseDefaultCredentials = false; smtp3.Credentials = new System.Net.NetworkCredential("用户名@outlook.com", "密码"); //设置用于验证发件人身份的凭证 smtp3.Host = "smtp.office365.com"; smtp3.TargetName = "邮件程序" + j; smtp3.Port = 587; smtp3.EnableSsl = true; var eml = new MailMessage(); eml.Sender = new MailAddress("用户名@outlook.com"); eml.From = eml.Sender; eml.To.Add("收件人@qq.com"); eml.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869");//防止垃圾邮箱 string as_MailContent11 ="呵呵呵"; string body11 = "<html><body style=\"margin: 0px,0px,0px,0px; text-indent: 30px; height: 1700px; width:1200px;\"> <div style='width:700px;text-align:center'><img src='cid:bImg' border=\"0\"/></div> <div style=\"text-indent: 30px;height: 1700px; margin:0px auto; width=1200px; padding-left: 0px; padding-top: 0px; \">" + as_MailContent11 + "</div></body></html>"; eml.Subject = "K11 outlook 邮件异步测试(带附件)" + j; eml.Body = body11; eml.BodyEncoding = Encoding.UTF8; eml.IsBodyHtml = true; //smtp2.DeliveryMethod = SmtpDeliveryMethod.Network;//指定邮件发送方式 eml.Priority = MailPriority.Normal;//邮件优先级(High,Low,Normal) var attachment11 = new Attachment("图片附件路径"); attachment11.ContentId = "bImg";//cid eml.Attachments.Add(attachment11); //var attachment211 = new Attachment(Attachments2); //eml.Attachments.Add(attachment211); try { smtp3.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted); smtp3.SendAsync(eml, "true"); Log.AppendLog("20200813异步测试邮件-------发送成功"); } catch (Exception e) { Console.WriteLine(e.Message); } //smtp3.Dispose(); } catch (Exception ex) { errorCount++; Log.AppendLog("20200813异步测试邮件-------发送失败" + ex.Message); } } void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { try { SmtpClient callBack = (SmtpClient)sender; Log.AppendLog("TargetName:"+callBack.TargetName); if (e.UserState.ToString() == "true") { Log.AppendLog("邮件异步回调:发送成功" + sender.ToString()); Log.AppendLog("邮件异步回调:发送成功" + e.Error); } else { Log.AppendLog("邮件异步回调:发送失败" + sender.ToString()); Log.AppendLog("邮件异步回调:发送失败" + e.Error); } } catch (Exception ex) { Log.AppendLog(ex.Message); } }
2.2 163
#region 签名的方式 string MailContent = "<html><body style=\"margin: 0px,0px,0px,0px; text-indent: 30px; height: 1700px; width:1200px;\"> <div style='width:700px;text-align:center'><img src='cid:bImg' border=\"0\"/></div> <div style=\"text-indent: 30px;height: 1700px; margin:0px auto; width=1200px; padding-left: 0px; padding-top: 0px; \">" + "哈哈哈" + "</div></body></html>"; SmtpClient smtp2; smtp2 = new SmtpClient("smtp.163.com", 25); smtp2.UseDefaultCredentials = false; smtp2.EnableSsl = true;//是否使用SSL加密连接 smtp2.DeliveryMethod = SmtpDeliveryMethod.Network;//指定邮件发送方式 smtp2.Credentials = new System.Net.NetworkCredential("qqxxxxx", "163邮件设置里面开通"); //设置用于验证发件人身份的凭证 没有后缀@163 MailMessage msg = new MailMessage();//邮件信息 msg.From = new MailAddress("qqxxxxx@163.com", "163邮件设置里面开通", Encoding.UTF8); msg.Subject = "邮件测试UAT 测试";//邮件标题 msg.SubjectEncoding = Encoding.UTF8; msg.Body = MailContent;//邮件正文 msg.IsBodyHtml = true; msg.BodyEncoding = Encoding.UTF8; msg.To.Add("收件人@outlook.com"); msg.Priority = MailPriority.Normal;//邮件优先级(High,Low,Normal) //附件图片(Body 的 <img src='cid:bImg') var attachment = new Attachment("附件路径"); attachment.ContentId = "bImg";//与CID 对应 msg.Attachments.Add(attachment); //常规附件 //var attachment2 = new Attachment("附件2"); //msg.Attachments.Add(attachment2); try { smtp2.Send(msg); Console.WriteLine("发送成功!"); } catch (Exception e) { Console.WriteLine(e.Message); } msg.Dispose(); #endregion