using System; using System.Collections; using System.Text; using System.Net; using System.Net.Mail; namespace NetMailSend { class Programe { /// <summary> /// C#发送邮件需要使用命名空间System.Net.Mail /// 同时会使用到MailMessage和SmtpClient两个类 /// </summary> /// <param name="args"></param> public static void Main(string[] args) { //mail message //实例化MailMessage,调用有参构造,传入发送者和接受者的邮箱地址,发送者不是是gmail邮箱 MailMessage myMail = new MailMessage("zxx1978@gmail.com","479224006@qq.com"); myMail.Subject = "Test"; //邮件标题 myMail.Body = "You are<br> successed."; //邮件内容 //设置邮件的优先级别(必写) myMail.Priority = MailPriority.High; //smtp client SmtpClient sender = new SmtpClient(); sender.Host = "smtp.gmail.com"; //设置主机名称或IP地址 sender.Port = 587; //设置SMTP端口 //设置发件人的邮箱和密码 sender.Credentials = new NetworkCredential("zxx1978@gmail.com", "zhao1978"); sender.EnableSsl = true; //设置是否套用加密连接(必写) try { sender.Send(myMail); //发送邮件 Console.WriteLine("Success"); } catch (Exception e) { Console.WriteLine("Failed.Exception:{0}", e.Message); } Console.Write("Press any key to quit..."); Console.ReadKey(); } } }
上面创建的是控制台应用程序!上面部分可以做如下修改:
MailMessage myMail = new MailMessage(); myMail.From = new MailAddress("2384130562@qq.com", "淘宝");//发件人信息 myMail.To.Add("479224006@qq.com");//收件人信息
说明:
1.“淘宝”二字表示发件人的名称,将显示到邮件发件人位置
2.收件人可以添加多个