您好,登錄后才能下訂單哦!
這篇文章主要介紹“C#怎么使用System.Net.Mail類實現郵件發送”,在日常操作中,相信很多人在C#怎么使用System.Net.Mail類實現郵件發送問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”C#怎么使用System.Net.Mail類實現郵件發送”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
有些 SMTP 服務器要求在代表客戶端發送電子郵件前驗證客戶端的身份。當此 SmtpClient 對象應該使用當前登錄用戶的默認憑據進行身份驗證(如果服務器要求)時,請將此屬性設置為 true。對于客戶端應用程序,這是在大多數情況下都需要的行為。也可以使用應用程序和計算機配置文件指定憑據信息。有關更多信息,請參見 <mailSettings> 元素(網絡設置)。
如果 UseDefaultCredentials 屬性設置為 false,則連接到服務器時會將 Credentials 屬性中設置的值用作憑據。如果 UseDefaultCredentials 屬性設置為 false 并且尚未設置 Credentials 屬性,則將郵件以匿名方式發送到服務器。UseDefaultCredentials 的默認值為false。
如果提供用于基本身份驗證的憑據,則憑據將以明文形式發送到服務器。這可能會產生安全問題,因為憑據可以被他人看見從而加以利用。但是向163的郵箱發送郵件是必須要帶上發送者的憑據信息,否則郵件是發送不出去的。
SmtpClient也可以這樣建立:SmtpClient client=new SmtpClient(smtp_server)
,因為郵件發送服務器的默認端口是25。
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.qq.com"; int port = 25; string mail_from = "***from_mail_username***@qq.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@163.com"; string receiver = "***to_mail_username***@"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server, port); client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.163.com"; int port = 25; string mail_from = "***from_mail_username***@163.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@163.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.163.com"; int port = 25; string mail_from = "***from_mail_username***@163.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@qq.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***"); client.Send(message); }
private static void TimeEvent(object source, ElapsedEventArgs e) { string smtp_server="smtp.qq.com"; int port = 25; string mail_from = "***from_mail_username***@qq.com"; string sender = "***from_mail_username***"; string mail_to = "***to_mail_username***@qq.com"; string receiver = "***to_mail_username***"; string subject = "Title Test" string body = "Content Test"; try { SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body); } catch(Exception ex) { MessageBox.Show(ex.Message); } } public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body) { MailAddress from = new MailAddress(mail_from, sender); MailAddress to = new MailAddress(mail_to, receiver); MailMessage message = new MailMessage(from, to); message.BodyEncoding = Encoding.UTF8; message.IsBodyHtml = true; message.Subject = subject; message.Body = body; SmtpClient client = new SmtpClient(smtp_server); client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***"); client.Send(message); }
到此,關于“C#怎么使用System.Net.Mail類實現郵件發送”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。