通過.Net FrameWork 2.0下提供的“System.Net.Mail”可以輕松的實現,本文列舉了3種途徑來發送:
1.通過Localhost;
2.通過普通SMTP;
3.通過SSL的SMTP;
下面一個一個來說:
1.通過LocalHost
public void SendMailLocalhost() { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.To.Add("[email protected] "); msg.To.Add("[email protected] "); /* * msg.To.Add("[email protected]"); * msg.To.Add("[email protected]"); * msg.To.Add("[email protected]");可以發送給多人 */ msg.CC.Add("[email protected] "); /* * msg.CC.Add("[email protected]"); * msg.CC.Add("[email protected]");可以抄送給多人 */ msg.From = new MailAddress("[email protected] ", "AlphaWu ", System.Text.Encoding.UTF8); /* 上面3個參數分別是發件人地址(可以隨便寫),發件人姓名,編碼*/ msg.Subject = "這是測試郵件 ";//郵件標題 msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件標題編碼 msg.Body = "郵件內容 ";//郵件內容 msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼 msg.IsBodyHtml = false ;//是否是HTML郵件 msg.Priority = MailPriority.High;//郵件優先級 SmtpClient client = new SmtpClient(); client.Host = "localhost "; object userState = msg; try { client.SendAsync(msg, userState); //簡單一點兒可以client.Send(msg); MessageBox.Show("發送成功 "); } catch (System.Net.Mail.SmtpException ex) { MessageBox.Show(ex.Message, "發送郵件出錯 "); } }2.通過普通SMTP