C# 发送邮件整理,包括控制台程序、WPF、WebForm 及 ASP.NET MVC
一直想把发送邮件的功能掌握,总是各种情况拖着了,这两天终于看了一下,整理一下,希望能帮到想学的。
发送邮件使用SMTP服务器,有两种方案,一种是使用IIS的SMTP功能;另一种是直接使用邮件供应商的SMTP,比如Gmail、Sina、QQ等,使用这些SMTP服务器必须得注册帐号,一般可以直接用邮箱及密码,但是有些邮箱必须开启POP3/SMTP服务才可以,比如QQ邮箱默认是关闭的,可以在“设置”->“账户”里面找到。我今天整理的都是用的第二种。
早期的.NET版本用的是 System.Web.Mail 类提供的功能来发邮件;2.0版本推出了 System.Net.Mail 类来代替 System.Web.Mail ,但是我在 WebForm 里面使用的时候用System.Net.Mail 老是触发异常,后来改用 System.Web.Mail 才成功了,可以看下我代码里的区别;ASP.NET MVC 3 提供了 WebMail 类来发送邮件,更加方便。
控制台程序、WPF、WebForm 及 ASP.NET MVC 我都测试了一下,控制台程序和 WPF 都用了 System.Net.Mail ,WebForm 和 ASP.NET MVC 都可以用 System.Web.Mail ,而 ASP.NET MVC 3 直接用 WebMail 更方便。下面我把代码分别贴出来。
控制台程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail;//添加引用 namespace SendEmail { class Program { static void Main(string[] args) { Program p = new Program(); bool flag = p.SendEmail(); if (flag) { Console.Write("Success !"); } Console.Read(); } public bool SendEmail() { MailMessage msg = new MailMessage(); msg.To.Add("1234567@qq.com");//邮件接收者的帐号 msg.From = new MailAddress("1234567@qq.com", "nickname", System.Text.Encoding.UTF8);//发送邮件的帐号及显示名称和字符编码 msg.Subject = "Subject";//邮件标题 msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 msg.Body = "Content";//邮件内容 msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 msg.IsBodyHtml = false;//是否是HTML邮件 msg.Priority = MailPriority.High;//邮件优先级 SmtpClient client = new SmtpClient(); client.Credentials = new System.Net.NetworkCredential("1234567@qq.com", "password");//注册的邮箱和密码,就QQ邮箱而言,如果设置了独立密码,要用独立密码代替密码 client.Host = "smtp.qq.com";//QQ邮箱对应的SMTP服务器 object userState = msg; try { client.SendAsync(msg, userState); return true; } catch (Exception ex) { Console.WriteLine(ex.Message); return false; } } } }
WPF:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Net.Mail;//添加引用 5 using System.Text; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace WPFSendMail 17 { 18 /// <summary> 19 /// MainWindow.xaml 的交互逻辑 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 } 27 28 private void Button_Click(object sender, RoutedEventArgs e) 29 { 30 bool flag = SendMail(); 31 if (flag) 32 { 33 MessageBox.Show("Success !","Tips"); 34 } 35 } 36 37 public bool SendMail() 38 { 39 MailMessage message = new MailMessage(); 40 message.To.Add("1234567@qq.com"); 41 message.From = new MailAddress("1234567@qq.com","nickname",Encoding.UTF8); 42 message.Subject = "Sending e-mail in WPF !"; 43 message.SubjectEncoding = Encoding.UTF8; 44 message.Body = "Awesome"; 45 message.BodyEncoding = Encoding.UTF8; 46 message.IsBodyHtml = false; 47 message.Priority = MailPriority.Normal; 48 Attachment att = new Attachment("text.txt");//添加附件,确保路径正确 49 message.Attachments.Add(att); 50 51 SmtpClient smtp = new SmtpClient(); 52 smtp.Credentials = new System.Net.NetworkCredential("1234567@qq.com","password"); 53 smtp.Host = "smtp.qq.com"; 54 object userState = message; 55 56 try 57 { 58 smtp.SendAsync(message,userState); 59 return true; 60 } 61 catch (Exception ex) 62 { 63 MessageBox.Show(ex.Message,"Tips"); 64 return false; 65 } 66 } 67 } 68 }
WebForm:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Web; 6 using System.Web.UI; 7 using System.Web.UI.WebControls; 8 using System.Web.Mail;//添加引用 9 10 namespace WebFormSendEmail 11 { 12 public partial class WebForm1 : System.Web.UI.Page 13 { 14 protected void Page_Load(object sender, EventArgs e) 15 { 16 17 } 18 19 protected void Button1_Click(object sender, EventArgs e) 20 { 21 bool flag = SendMail(); 22 if (flag) 23 { 24 Response.Write("<script>alert('Success !');</script>"); 25 } 26 } 27 28 public bool SendMail() 29 { 30 MailMessage message = new MailMessage(); 31 message.To = "1234567@qq.com"; 32 message.From = "1234567@qq.com"; 33 message.Subject = "Sending e-mail in Web !"; 34 message.Body = "Awesome"; 35 //基本权限 36 message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); 37 //用户名 38 message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "1234567@qq.com"); 39 //密码 40 message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "password"); 41 42 SmtpMail.SmtpServer = "smtp.qq.com"; 43 try 44 { 45 SmtpMail.Send(message); 46 return true; 47 } 48 catch (Exception ex) 49 { 50 Response.Write(ex.Message); 51 return false; 52 } 53 } 54 } 55 }
ASP.NET MVC :
try { WebMail.SmtpServer = "smtp.qq.com"; WebMail.SmtpPort = 25;//端口号,不同SMTP服务器可能不同,可以查一下 WebMail.EnableSsl = false;//禁用SSL WebMail.UserName = "1234567@qq.com"; WebMail.Password = "password"; WebMail.From = "1234567@qq.com"; WebMail.Send("1234567@qq.com","Subject",“Content"); } catch (Exception) { }