日期:2014-05-18 浏览次数:21204 次
using System; using System.Collections.Generic; using System.Net.Sockets; using System.Text; using System.Net.Mail; using System.Threading; namespace mail { class Program { static void Main(string[] args) { SendMailUseGmail(); Console.ReadLine(); } public static void SendMailUseGmail() { System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage(); msg.From = new MailAddress("XXXXX@163.com", "foolfire", System.Text.Encoding.UTF8); //发件人地址(可以随便写),发件人姓名,编码*/ msg.Subject = "give your some emails by foolfire1";//邮件标题 msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码 msg.Body = "sdaaaaaaaaaaaaaaaaa";//邮件内容 msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码 msg.IsBodyHtml = false;//是否是HTML邮件 msg.Priority = MailPriority.High;//邮件优先级 SmtpClient client = new SmtpClient(); //发件邮箱和密码 ,注意这里要与发件人地址的邮箱一致 client.Credentials = new System.Net.NetworkCredential("XXXXX@163.com", "XXXXXXXXX"); //client.Port = 25;//Gmail使用的端口587 //client.EnableSsl = true;//经过ssl加密 client.DeliveryMethod = SmtpDeliveryMethod.Network; client.Host = "smtp.163.com"; object userState = msg; //这里可以随便写什么,只要是规范的email地址 msg.To.Add("XXXXXX@tom.com"); try { /***************************/ /*请注意SmtpClient类还有两个用于发送Email的方法: * Send()和SendAsync()。Send()方法在它的发送操作完成前阻塞其它的程序执行; * SendAsync()方法异步地发送Email。 * 不像Send()方法,SendAsync()方法不必等待检测发送操作是否成功。*/ /***************************/ //client.SendAsync(msg, userState); ////client.Send(msg); //Console.WriteLine("success!"); System.Threading.Thread.Sleep(100); client.Send(msg); Console.WriteLine("发送成功!"); } catch (System.Net.Mail.SmtpException ex) { Console.WriteLine(ex.StackTrace); } } } }