1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net.Mail; 6 using System.Net; 7 namespace ConsoleApplication1 8 { 9 class Program 10 { 11 static void Main(string[] args) 12 { 13 sendMail("**@**.cn","深师家园", "admin@**.com", "admin@***.com", "***", "您好!", "这是一封测试邮件!"); 14 } 15 16 /// <summary> 17 /// 向用户发送邮件 18 /// </summary> 19 /// <param name="ReceiveUser">接收邮件的用户</param> 20 /// <param name="SendUser">发送者显求的邮箱地址,可为空</param> 21 /// <param name="DisplayName">收件人显示发件人的联系人名,可为中文</param> 22 /// <param name="SendUserName">发送者的邮箱登陆名,可以与发送者地址一样</param> 23 /// <param name="UserPassword">发送者的登陆密码</param> 24 /// <param name="MailTitle">发送标题</param> 25 /// <param name="MailContent">发送的内容</param> 26 public static void sendMail(string ReceiveUser, string DisplayName,string SendUser, string SendUserName, string UserPassword, string MailTitle, string MailContent) 27 { 28 MailAddress toMail = new MailAddress(ReceiveUser);//接收者邮箱 29 MailAddress fromMail = new MailAddress(SendUser, DisplayName);//发送者邮箱 30 MailMessage mail = new MailMessage(fromMail, toMail); 31 mail.Subject = MailTitle; 32 mail.IsBodyHtml = true;//是否支持HTML 33 mail.Body = MailContent; 34 SmtpClient client = new SmtpClient(); 35 client.Host = "smtp.exmail.qq.com";//设置发送者邮箱对应的smtpserver 36 client.UseDefaultCredentials = false; 37 //client.Port = 465; 38 client.Credentials = new NetworkCredential(SendUserName, UserPassword); 39 client.DeliveryMethod = SmtpDeliveryMethod.Network; 40 try 41 { 42 client.Send(mail); 43 } 44 catch(SmtpException ex) 45 { 46 Console.Write(ex.Message); 47 } 48 Console.ReadKey(); 49 } 50 } 51 }