用C#發郵件
阿新 • • 發佈:2018-03-16
eth ica tpc 名稱 res address htm client net
以下有2種方法:
第一種:
using System; using System.Net.Mail; //當時測試的是QQ郵箱 class TestMail { static void Main(string[] args) { string mailContent = "郵件內容"; MailMessage msg = new System.Net.Mail.MailMessage(); msg.To.Add("[email protected],[email protected]"); //收件人 //發件人信息 msg.From = newMailAddress("[email protected]", "發送人姓名", System.Text.Encoding.UTF8); msg.Subject = "這是測試郵件"; //郵件標題 msg.SubjectEncoding = System.Text.Encoding.UTF8; //標題編碼 msg.Body = mailContent; //郵件主體 msg.BodyEncoding = System.Text.Encoding.UTF8; msg.IsBodyHtml = true; //是否HTMLmsg.Priority = MailPriority.High; //優先級 SmtpClient client = new SmtpClient(); //註意:下面的這個密碼是授權碼,不是你登錄QQ的密碼,查看方法:QQ郵箱---設置裏面 client.Credentials = new System.Net.NetworkCredential("[email protected]", "asdfghjkl"); client.EnableSsl = true; client.Port = 587; //443 client.Host = "smtp.qq.com";//mail.adient.com object userState = msg; try { client.Send(msg); //MessageBox.Show("發送成功"); } catch (Exception ex) { //MessageBox.Show(ex.Message, "發送郵件出錯"); } } }
第二種:
//當時測試的是outlook 延鋒的郵箱 using System.Net.Mail; namespace ConsoleApplication11 { class Program { static void Main(string[] args) { MailMessage mail = new MailMessage(); //設置郵件的標題 mail.Subject = "測試郵件"; //設置郵件的發件人 //mail.From = new MailAddress("[email protected]", "Xiao_1006"); //發件人的郵箱地址,後面參數 張猛:收件人看到的發件人的名稱 mail.From = new MailAddress("[email protected]", "張猛"); //設置郵件的收件人,ZMENG沒有什麽用 mail.To.Add("[email protected],[email protected]"); //設置郵件的抄送人 // mail.CC.Add(new MailAddress("[email protected]", "Nick")); //設置郵件的內容 mail.Body = "就是測試用111"; mail.BodyEncoding = System.Text.Encoding.UTF8; mail.IsBodyHtml = true; mail.Priority = MailPriority.Normal; mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess; SmtpClient client = new SmtpClient(); //郵件服務器的名稱,要問對方公司才知道 client.Host = "smtp.ga.adient.com"; //發郵件的端口號,要問對方公司才知道 client.Port = 25; client.UseDefaultCredentials = false; //client.Credentials = new System.Net.NetworkCredential("[email protected]", "Xiao_1006"); //發件人的賬號和密碼 client.Credentials = new System.Net.NetworkCredential("[email protected]", "Yfas123"); client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Send(mail); } } }
用C#發郵件