public partial class Form1 : Form { public Form1() { InitializeComponent(); } public static void SendMail(string strSmtpServer, string UserName, string Password, string strFrom, string strTo, string strSubject, string strBody, string strFileName) { //生成一个 使用SMTP发送邮件的客户端对象 System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer); //表示以当前登录用户的默认凭据进行身份验证,不写依然能发送 //client.UseDefaultCredentials = true; //包含用户名和密码 client.Credentials = new System.Net.NetworkCredential(UserName, Password); //指定如何发送电子邮件。 //Network 电子邮件通过网络发送到 SMTP 服务器。 //PickupDirectoryFromIis 将电子邮件复制到挑选目录,然后通过本地 Internet 信息服务 (IIS) 传送。 //SpecifiedPickupDirectory 将电子邮件复制到 SmtpClient.PickupDirectoryLocation 属性指定的目录,然后由外部应用程序传送。 client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network; //建立邮件对象 System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strTo, strSubject, strBody); //定义邮件正文,主题的编码方式 message.BodyEncoding = System.Text.Encoding.UTF8; message.SubjectEncoding = System.Text.Encoding.UTF8; //获取或设置一个值,该值指示电子邮件正文是否为 HTML。 message.IsBodyHtml = true; //指定邮件优先级 message.Priority = System.Net.Mail.MailPriority.Normal; //添加附件 //发件人身份验证,否则163 发不了 client.Credentials = new System.Net.NetworkCredential(strFrom, Password); //发送 client.Send(message); } private void button1_Click(object sender, EventArgs e) { System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(); client.Host = "smtp.163.com";//邮件服务器 client.Port = 25;//smtp主机上的端口号,默认是25. client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;//邮件发送方式:通过网络发送到SMTP服务器 client.Credentials = new System.Net.NetworkCredential("beyonder_man", "oepfkrkrbgfotemy");//凭证,发件人登录邮箱的用户名和密码 //client.UseDefaultCredentials = false; //启用身份认证 //电子邮件信息类 System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("[email protected]", "xiaoming");//发件人Email,在邮箱是这样显示的,[发件人:小明<[email protected]>;] System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("[email protected]", "小红");//收件人Email,在邮箱是这样显示的, [收件人:小红<[email protected]>;] System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage(fromAddress, toAddress);//创建一个电子邮件类 mailMessage.Subject = "邮件的主题"; string filePath = "E:\\One_Day.lrc";//邮件的内容可以是一个html文本. System.IO.StreamReader read = new System.IO.StreamReader(filePath, System.Text.Encoding.GetEncoding("GB2312")); string mailBody = read.ReadToEnd(); read.Close(); mailMessage.Body = mailBody ;//可为html格式文本 //mailMessage.Body = "邮件的内容";//可为html格式文本 mailMessage.SubjectEncoding = System.Text.Encoding.UTF8;//邮件主题编码 mailMessage.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312");//邮件内容编码 mailMessage.IsBodyHtml = true;//邮件内容是否为html格式 mailMessage.Priority = System.Net.Mail.MailPriority.High;//邮件的优先级,有三个值:高(在邮件主题前有一个红色感叹号,表示紧急),低(在邮件主题前有一个蓝色向下箭头,表示缓慢),正常(无显示). mailMessage.Attachments.Add(new System.Net.Mail.Attachment("E://Red.lrc")); client.Send(mailMessage);//发送邮件 //client.SendAsync(mailMessage, "ojb");//异步方法发送邮件,不会阻塞线程. } private void button2_Click(object sender, EventArgs e) { SendMail("smtp.163.com", "你的邮箱用户名", "你的授权码", "你的邮箱", "要发送给的邮箱", "主题", "内容", ""); } }
源代码奉上,参考了别人的,都可以用