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