C# 发送邮件的记录(qq,126,Gmail)
需求:用C#做一個發送郵件的接口。?
 說明:?
 1.smtp
SMTP(Simple Mail Transfer Protocol)即簡單郵件傳輸協議,它是一組用于由源地址到目的地址傳送郵件的規則,由它來控制信件的中轉方式。SMTP協議屬于TCP/IP協議簇,它幫助每臺計算機在發送或中轉信件時找到下一個目的地。通過SMTP協議所指定的服務器,就可以把E-mail寄到收信人的服務器上了,整個過程只要幾分鐘。SMTP服務器則是遵循SMTP協議的發送郵件服務器,用來發送或中轉發出的電子郵件。?
 這是原理說明(蒙蔽,有時間再研究下)?
 http://blog.csdn.net/kerry0071/article/details/28604267
2.smtp服務器地址和端口
25:如果不啟用SSL認證,一般是這個端口;?
 587/465:啟動SSL認證。?
 以下是常用郵箱的端口:?
 http://blog.csdn.net/whyhonest/article/details/7289420?
 QQ:smtp.qq.com:465?
 126:smtp.126.com:25?
 Gmail:smtp.gmail.com:587
步驟:?
 1.先啟動郵箱smtp功能:
QQ(126類似)–這兩個要啟用SSL都需要專門的授權碼,可以在郵箱通過賬號獲取。?
 http://jingyan.baidu.com/article/0f5fb099dffe7c6d8334ea31.html?
 Gmail:?
 需要fan墻,操作類似;除了在郵箱頁面進行設置之外,還需要在客戶端進行設置。(win10 用OutLook比較方便)?
 參考:http://www.xujiahua.com/848.html?
 官方:https://support.google.com/mail/answer/7126229?visit_id=1-636278354684802140-826274024&hl=zh-Hans&rd=1
2.編碼部分:在C#中QQ 和 126/Gmail 所用類庫不一樣。QQ用System.Web.Mail;126用System.Net.Mail(QQ用會出操作超時的異常)。?
 QQ
public static void SendQQEmailtest(string server, int port, string sender, string recipient, string subject, string body, bool isBodyHtml, Encoding encoding, string authentication, string userName, params string[] files)
 ? ? ? ? {
 ? ? ? ? ? ? System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
 ? ? ? ? ? ? mail.To = recipient;
 ? ? ? ? ? ? mail.From = sender;
 ? ? ? ? ? ? mail.Subject = subject;
 ? ? ? ? ? ? mail.BodyFormat = System.Web.Mail.MailFormat.Text;
 ? ? ? ? ? ? mail.Body = body;
 ? ? ? ? ? ? //添加附件
 ? ? ? ? ? ? mail.Attachments.Clear();
 ? ? ? ? ? ? if (files != null && files.Length != 0)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? for (int i = 0; i < files.Length; ++i)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? Attachment attach = new Attachment(files[i]);
 ? ? ? ? ? ? ? ? ? ? mail.Attachments.Add(attach);
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
? ? ? ? ? ? mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", authentication); //這個密碼要注意:如果是一般賬號,要用授權碼;企業賬號用登錄密碼 ?
? ? ? ? ? ? mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份驗證 ?
 ? ? ? ? ? ? mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //郵箱登錄賬號,這里跟前面的發送賬號一樣就行 ?
? ? ? ? ? ? mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", port);//端口 ?
 ? ? ? ? ? ? mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密 ?
? ? ? ? ? ? System.Web.Mail.SmtpMail.SmtpServer = server; ? ?//企業賬號用smtp.exmail.qq.com ?
 ? ? ? ? ? ? System.Web.Mail.SmtpMail.Send(mail); ?
 ? ? ? ? }
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 Gmail/126
public static void Send2test(string server, int port, string sender, string recipient, string subject,
 ? ? string body, bool isBodyHtml, Encoding encoding, string authentication, string userName, params string[] files)
 ? ? ? ? {
 ? ? ? ? ? ? //確定發件人地址、收件人地址
 ? ? ? ? ? ? MailMessage message = new MailMessage(sender, recipient);
 ? ? ? ? ? ? message.IsBodyHtml = isBodyHtml;
? ? ? ? ? ? message.SubjectEncoding = encoding;
 ? ? ? ? ? ? message.BodyEncoding = encoding;
? ? ? ? ? ? message.Subject = subject;
 ? ? ? ? ? ? message.Body = body;
? ? ? ? ? ? message.Attachments.Clear();
 ? ? ? ? ? ? if (files != null && files.Length != 0)
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? for (int i = 0; i < files.Length; ++i)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? Attachment attach = new Attachment(files[i]);
 ? ? ? ? ? ? ? ? ? ? message.Attachments.Add(attach);
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? }
? ? ? ? ? ? //根據 郵件服務器 地址建立客戶端
 ? ? ? ? ? ? SmtpClient smtpClient = new SmtpClient(server, port);//如果不寫端口就會出現異常:AUTH fist!
 ? ? ? ? ? ? smtpClient.Timeout = 50000;
 ? ? ? ? ? ? smtpClient.EnableSsl = true;//從抓包的情況來看是有STARTTLS的
? ? ? ? ? ? if (string.IsNullOrEmpty(authentication))
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? smtpClient.UseDefaultCredentials = true;
 ? ? ? ? ? ? }
 ? ? ? ? ? ? else
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? //smtpClient.UseDefaultCredentials = true;//看來這句是可有可無的
 ? ? ? ? ? ? ? ? smtpClient.Credentials = new NetworkCredential(userName,
 ? ? ? ? ? ? ? ? ? ? authentication);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? //smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//通過網絡發送
 ? ? ? ? ? ? try
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? smtpClient.Send(message);
 ? ? ? ? ? ? }
 ? ? ? ? ? ? catch (Exception ex)
 ? ? ? ? ? ? {
? ? ? ? ? ? }
 ? ? ? ? ? ? finally{
 ? ? ? ? ? ? ? ? smtpClient.Dispose();
 ? ? ? ? ? ? }
? ? ? ? }
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 有意思的是在Gmail中:smtpClient.UseDefaultCredentials = true 在官方的解釋中:?
 如果 UseDefaultCredentials 屬性設置為 false,則連接到服務器時會將 Credentials 屬性中設置的值用作憑據。 如果 UseDefaultCredentials 屬性設置為 false并且尚未設置 Credentials 屬性,則將郵件以匿名方式發送到服務器。?
 然而調試中會出異常:?
 其他信息: SMTP 服務器要求安全連接或客戶端未通過身份驗證。 服務器響應為:5.5.1 Authentication Required.?
 當注釋掉smtpClient.UseDefaultCredentials這句后,就能發送郵件了。
PS:關于其中遇到的問題及解決:?
 1.126
不允許使用郵箱名稱。 服務器響應為:authentication is required,126 smtp4,?
 原因:?
 輸入了地址,但是port沒有明確|沒有輸入驗證信息?
 -所以輸入對應的port + 確認驗證信息
2.關于Gmail這個大坑
1.調試中是否需要翻墻:?
 否;否則在連接服務器的時候會出現各種失敗;?
 2.Gmail端口?
 587而非465!否則會報操作超時;?
 3.Gmail-tls?
 在C#中,smtpClient.EnableSsl = true;相當于啟用了tls,從wireshark中可以看出STARTTLS。不用花心思去琢磨用C#實現tls。以下是原理:?
 4.是否需要修改Hosts?
 參考:http://www.williamlong.info/archives/4455.html?
 發現其中關于修改是針對OutLook的。我在實際操作中是修改了Hosts之后操作的OutLook,并且在該環境中進行調試并發送郵件成功的。但是會存在不穩定的問題,有時候會顯示操作超時,但是郵件仍然收不到的情況;?
 待還原Hosts文件后再測試下效果。?
 5.Gmail有可能要設置“允許不夠安全的應用訪問”
參考文章:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000?
 于是我堅定了從587端口嘗試的決心 %>_<%,不過看起來Python好方便啊。465端口在嘗試中發現與服務器連接各種失敗、操作超時。?
 http://stackoverflow.com/questions/2057227/c-sharp-asp-net-send-email-via-tls?
 于是我放棄了琢磨TLS?
 http://qa.helplib.com/198009?
 于是我開始懷疑官方文檔。。并且這里的答案是可信的。而且經過嘗試,smtpClient.UseDefaultCredentials 可以不用管它。
2017年4月24日 更新?
 后來為了解決System.web.mail過時方法的警告,采用了另外的解決方案。
http://www.codingwhy.com/view/614.html
public static void Send(string server, int port, string sender, string password, string recipient, string subject, string body, bool isBodyHtml, params string[] files)
 ? ? ? ? {
 ? ? ? ? ? ? CDO.Message objMail = new CDO.Message();
 ? ? ? ? ? ? try
 ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? objMail.To = recipient;
 ? ? ? ? ? ? ? ? objMail.From = sender;
 ? ? ? ? ? ? ? ? objMail.Subject = subject;//郵件主題string strHTML = @"";
 ? ? ? ? ? ? ? ? if (isBodyHtml == true)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? //strHTML = strHTML + "這里可以填寫html內容";
 ? ? ? ? ? ? ? ? ? ? objMail.HTMLBody = body;//郵件內容
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? else
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? objMail.TextBody = body;
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = port;//設置端口
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = server;
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = sender;//"發送郵件賬號";
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpuserreplyemailaddress"].Value = sender;//"發送郵件賬號";
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpaccountname"].Value = sender;//"發送郵件賬號";
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = sender;//"發送郵件賬號";
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = password;//"發送郵件賬號登錄密碼";
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1;
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"].Value = "true";//這一句指示是否使用ssl
 ? ? ? ? ? ? ? ? if (files != null && files.Length != 0)
 ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? for (int i = 0; i < files.Length; ++i)
 ? ? ? ? ? ? ? ? ? ? {
 ? ? ? ? ? ? ? ? ? ? ? ? objMail.AddAttachment(files[i], "", "");//"C:\\Hello.txt"
 ? ? ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? }
 ? ? ? ? ? ? ? ? objMail.Configuration.Fields.Update();
 ? ? ? ? ? ? ? ? objMail.Send();
 ? ? ? ? ? ? }
 ? ? ? ? ? ? catch (Exception ex) { throw ex; }
 ? ? ? ? ? ? finally { objMail = null; }
 ? ? ? ? }
 原文:https://blog.csdn.net/u013244192/article/details/70194595?
總結
以上是生活随笔為你收集整理的C# 发送邮件的记录(qq,126,Gmail)的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mp4文件格式解析(一)
- 下一篇: 程序员考核的五大死因(上)
