废话不多说,直接上代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net.Mail; namespace MailSender { class Program { static string strHost = string.Empty; static string strAccount = string.Empty; static string strPwd = string.Empty; static string strFrom = string.Empty; static void Main(string[] args) { strHost = "smtp.qq.com"; //STMP服务器地址 strAccount = "xxx@126.com"; //SMTP服务帐号 strPwd = "123456"; //SMTP服务密码 strFrom = "xxx@126.com"; //发送方邮件地址 Console.WriteLine(sendMail("cnkker@qq.com", "这是一封测试邮件", "这是一封测试邮件的正文内容") ? "Success" : "Unsuccess"); Console.ReadLine(); } /// <summary> /// 发送邮件 /// </summary> /// <param name="to">接收方邮件地址</param> /// <param name="title">邮件标题</param> /// <param name="content">邮件正文内容</param> /// <returns></returns> /// <author>Jailu</author> /// <date>2007-04-10</date> static bool sendMail(string to, string title, string content) { SmtpClient _smtpClient = new SmtpClient(); _smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式 _smtpClient.Host = strHost; ;//指定SMTP服务器 _smtpClient.Credentials = new System.Net.NetworkCredential(strAccount, strPwd);//用户名和密码 MailMessage _mailMessage = new MailMessage(strFrom, to); _mailMessage.Subject = title;//主题 _mailMessage.Body = content;//内容 _mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码 _mailMessage.IsBodyHtml = true;//设置为HTML格式 _mailMessage.Priority = MailPriority.High;//优先级 try { _smtpClient.Send(_mailMessage); return true; } catch(Exception ex) { return false; } } } }