《C#发送邮件源码_已测试.doc》由会员分享,可在线阅读,更多相关《C#发送邮件源码_已测试.doc(5页珍藏版)》请在金锄头文库上搜索。
1、C# 发送邮件源码最近有用户提出了一个新的需求,希望公司的网站系统能够自动发邮件到相关人员的邮箱中,通过实践做出了程序,通过下面的代码分享给大家。发送邮件使用VS中的System.Net.Mail类。using System.Net.Mail;1、定义公共变量: #region 邮件 public static string senderServerIp = ; public static string fromMailAddress = ; public static string mailUsername = ; public static string mailPassword =.;
2、public static string mailPort = ; /内容根据实际情况而定 #endregion2、新增类using System;using System.Collections.Generic;using System.Text;using System.IO;using System.Net.Mail;using System.Net.Mime;namespace KRP.ClsCarInfo public class MyEmail private MailMessage mMailMessage; /主要处理发送邮件的内容(如:收发人地址、标题、主体、图片等等) pr
3、ivate SmtpClient mSmtpClient; /主要处理用smtp方式发送此邮件的配置信息(如:邮件服务器、发送端口号、验证方式等等) private int mSenderPort; /发送邮件所用的端口号(htmp协议默认为) private string mSenderServerHost; /发件箱的邮件服务器地址(IP形式或字符串形式均可) private string mSenderPassword; /发件箱的密码 private string mSenderUsername; /发件箱的用户名(即符号前面的字符串,例如:,用户名为:hello) private b
4、ool mEnableSsl; /是否对邮件内容进行socket层加密传输 private bool mEnablePwdAuthentication; /是否对发件人邮箱进行密码验证 / / 构造函数 / / 发件箱的邮件服务器地址 / 收件人地址(可以是多个收件人,程序中是以“;进行区分的) / 发件人地址 / 邮件标题 / 邮件内容(可以以html格式进行设计) / 发件箱的用户名(即符号前面的字符串,例如:,用户名为:hello) / 发件人邮箱密码 / 发送邮件所用的端口号(htmp协议默认为) / true表示对邮件内容进行socket层加密传输,false表示不加密 / true
5、表示对发件人邮箱进行密码验证,false表示不对发件人邮箱进行密码验证 public MyEmail(string server, string toMail, string fromMail, string subject, string emailBody, string username, string password, string port, bool sslEnable, bool pwdCheckEnable) try mMailMessage = new MailMessage(); mMailMessage.To.Add(toMail); mMailMessage.From
6、 = new MailAddress(fromMail); mMailMessage.Subject = subject; mMailMessage.Body = emailBody; mMailMessage.IsBodyHtml = true; mMailMessage.BodyEncoding = System.Text.Encoding.UTF8; mMailMessage.Priority = MailPriority.Normal; this.mSenderServerHost = server; this.mSenderUsername = username; this.mSen
7、derPassword = password; this.mSenderPort = Convert.ToInt32(port); this.mEnableSsl = sslEnable; this.mEnablePwdAuthentication = pwdCheckEnable; catch (Exception ex) Console.WriteLine(ex.ToString(); / / 添加附件 / / 附件的路径集合,以分号分隔 public void AddAttachments(string attachmentsPath) try string path = attachm
8、entsPath.Split(;); /以什么符号分隔可以自定义 Attachment data; ContentDisposition disposition; for (int i = 0; i path.Length; i+) data = new Attachment(pathi, MediaTypeNames.Application.Octet); disposition = data.ContentDisposition; disposition.CreationDate = File.GetCreationTime(pathi); disposition.Modification
9、Date = File.GetLastWriteTime(pathi); disposition.ReadDate = File.GetLastAccessTime(pathi); mMailMessage.Attachments.Add(data); catch (Exception ex) Console.WriteLine(ex.ToString(); / / 邮件的发送 / public void Send() try if (mMailMessage != null) mSmtpClient = new SmtpClient(); /mSmtpClient.Host = smtp. + mMailMessage.From.Host; mSmtpClient.Host = this.mSenderServerHost; mSmtpClient.Port = this.mSenderPort; mSmtpClient.UseDefaultCredentials = false; mSmtpClient.EnableSsl = this.mEnableSsl; if (this.mEnablePwdAuthentication) System.Net.NetworkCredential nc = new System.Net.NetworkCredentia