[研究][ASP.NET][C#][WebForm]使用hotmail寄信 (System.Net.Mail)
2022-01-22
2022-06-22 補
********************************************************************************
Outlook.com 的 POP、IMAP 和 SMTP 設定
https://support.microsoft.com/zh-tw/office/outlook-com-%E7%9A%84-pop-imap-%E5%92%8C-smtp-%E8%A8%AD%E5%AE%9A-d088b986-291d-42b8-9564-9c414e2aa040
SMTP 伺服器名稱 smtp-mail.outlook.com
SMTP 埠 587
SMTP 加密:STARTTLS
********************************************************************************
2022-08-15補
hotmail 似乎做了某些調整,程式不能用了。調整疑似在2022/08/12, 8/13 或 8/14 調整的,上周四 or 五印象仍可用。
程式寄信回成功,但收不到信。用瀏覽器登入 hotmail,寄信,可以收到。
查知 SMTP 從 smtp-mail.outlook.com 變更為:伺服器名稱: smtp.office365.com,連接埠: 587,加密方法: STARTTLS;修改伺服器名稱和連接埠後測試 ( StartTLS 不知要如何改)。程式寄信回成功,但仍收不到信,暫時不知道還要如何改。
Google 查 SmtpClient 好像不支援 StartTLS。
********************************************************************************
Visual Studio 2022 + ASP.NET + C# + Web Application + WebForm
using System; using System.Net.Mail; namespace WebApplication1 { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { try { // MailMessage ( FromEmailAddress, ToEmailAddress) MailMessage message = new MailMessage("名稱<XXX@hotmail.com>", "收件者信箱");//MailMessage(寄信者, 收信者) message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8;//E-mail編碼 message.SubjectEncoding = System.Text.Encoding.UTF8;//E-mail編碼 message.Priority = MailPriority.Normal;//設定優先權 message.Subject = "Test3"; //E-mail主旨 message.Body = "Test3";//E-mail內容 SmtpClient MySmtp = new SmtpClient("smtp-mail.outlook.com");//設定gmail的smtp //System.Net.NetworkCredential(帳號,密碼),hotmail的帳號是整的Email,不是只有@前面的 MySmtp.Credentials = new System.Net.NetworkCredential("XXX@hotmail.com", "密碼"); MySmtp.EnableSsl = true;//開啟ssl MySmtp.Send(message); MySmtp = null; message.Dispose(); Label1.Text = "Send !"; } catch (Exception ex) { Label1.Text = "不明錯誤。"; if (ex!=null) { Label1.Text = ex.Message.ToString(); } } } } } |
**********
2022-06-22補
如果收件者有多人,
MailMessage message = new MailMessage("XX網站<XXX@hotmail.com>", address); |
改成
message.From = new MailAddress("XXX@hotmail.com", "XX網站", System.Text.Encoding.UTF8); |
修改後結果
#region == public static string SendNoCertMailByHotmail(string mysubject, string myContent, string toAddressList, string ccAddress, string bccAddress) == private static string SendNoCertMailByHotmail(string mysubject, string myContent, string toAddressList, string ccAddress, string bccAddress) { string localIP = Common.GetLocalIPv4(); // 傳回非 127.0.0.1、192.168.x.x 的 IP string sendMailResult = ""; //預設成功,回傳空字串 try { //string emailContent = @"【此為系統自動發信,請勿直接回信。】<br />【若收到重複信不用理會,因Mail Server間沒有100%相容。】<br />" + myContent; //string emailContent = @"【此為系統自動發信,請勿直接回信。】<br />" + myContent; string emailContent = @"信件於 " + DateTime.Now.ToString() + " 寄出。<br />【此為系統自動發信,請勿直接回信。】<br />" + myContent; // MailMessage ( FromEmailAddress, ToEmailAddress) // MailMessage (寄信者Email, 收信者Email) //public MailMessage (); //public MailMessage (System.Net.Mail.MailAddress from, System.Net.Mail.MailAddress to); //public MailMessage (string from, string to); //public MailMessage (string from, string to, string subject, string body); if (toAddressList == "") return "沒有收件者"; //MailMessage message = new MailMessage("XXX網站<XXX@hotmail.com>", address); message.From = new MailAddress("XXX@hotmail.com", "XXX網站", System.Text.Encoding.UTF8); message.Subject = mysubject; //E-mail主旨 if (!string.IsNullOrEmpty(toAddressList.Trim())) { toAddressList = toAddressList.Replace(" ", ""); foreach (var item in toAddressList.Trim().Split(',', ';')) { //var to = new SecureMailAddress(item); //message.To.Add(new SecureMailAddress(item)); message.To.Add(new MailAddress(item)); } } message.IsBodyHtml = true; message.BodyEncoding = System.Text.Encoding.UTF8;//E-mail編碼 message.SubjectEncoding = System.Text.Encoding.UTF8;//E-mail編碼 message.Priority = MailPriority.Normal;//設定優先權 message.Subject = mysubject + "(" + localIP + ")" ; //E-mail主旨,非正式機加上IP,分辨一下開發機 or 測試機 message.Body = emailContent;//E-mail內容 // message.ReplyTo 過時 message.ReplyToList.Add(new MailAddress("回信者@abcdef.com.tw", "XXX網站")); //public MailAddress (string address); //public MailAddress (string address, string displayName); //public MailAddress (string address, string displayName, System.Text.Encoding displayNameEncoding); //message.From = new MailAddress("XXX@hotmail.com", "XXX網站"); //message.To.Add(address); //if (address != "") // message.To.Add(address); //else // return "沒有收件者"; if (ccAddress!="") message.CC.Add(ccAddress); if (bccAddress !="") message.Bcc.Add(bccAddress); SmtpClient MySmtp = new SmtpClient("smtp-mail.outlook.com");//設定smtp //gmail的帳號密碼System.Net.NetworkCredential(帳號,密碼) MySmtp.Credentials = new System.Net.NetworkCredential("XXX@hotmail.com", " Nccst2016$"); MySmtp.EnableSsl = true;//開啟ssl MySmtp.Send(message); MySmtp = null; message.Dispose(); //Label1.Text = "成功。"; //sendMailResult = "OK。已送出郵件" + DateTime.Now.ToString(); } catch (Exception ex) { //throw; // 胡淯婷 或廠商 會亂填 Email 測試 sendMailResult = "寄信給" + toAddressList + "失敗,不明錯誤。"; if (ex != null) { sendMailResult = "寄信給" + toAddressList + "失敗。" + ex.Message.ToString(); } return sendMailResult; } return sendMailResult; // -------------------------------------------------------------------------------- } #endregion |
(完)
相關
[研究] 用 Outlook 2010 收發 Hotmail / Outlook.com 信件(2016年)(一)
http://shaurong.blogspot.com/2016/01/outlook-2010-hotmail-outlookcom-2016.html
沒有留言:
張貼留言