/// <summary> /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址) /// </summary> /// <param name="type">类型:收件人、抄送人、密送人</param> /// <param name="address">Email地址</param> /// <param name="displayName">显示名称</param> public void AddReceive(EmailAddrType type, string address, string displayName) { MailValidatorHelper.ValideStrNullOrEmpty(address, "address"); Dictionary <string, string> dic = null; switch (type) { case EmailAddrType.To: dic = DicTo; break; case EmailAddrType.CC: dic = DicCC; break; case EmailAddrType.Bcc: dic = DicBcc; break; case EmailAddrType.From: throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR); } if (dic.Count == 0 || !dic.ContainsKey(address)) { dic.Add(address, displayName); } }
/// <summary> /// 添加内嵌资源(eg:图片,mp3等等) /// </summary> /// <param name="fieldPath">内嵌资源的文件路径</param> /// <param name="cidName">设置此附件的 MIME 内容 ID</param> public void AddInlineAttachment(string fieldPath, string cidName) { MailValidatorHelper.ValideStrNullOrEmpty(fieldPath, "fieldPath"); MailValidatorHelper.ValideStrNullOrEmpty(cidName, "cidName"); this.InnerAddAttachment(fieldPath, String.Empty, true, cidName); }
/// <summary> /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址) /// </summary> /// <param name="type">类型:收件人、抄送人、密送人</param> /// <param name="dicAddress">Email地址,显示名称</param> public void AddReceive(EmailAddrType type, Dictionary <string, string> dicAddress) { MailValidatorHelper.ValideArgumentNull <Dictionary <string, string> >(dicAddress, "dicAddress"); if (dicAddress.Count > 0) { Dictionary <string, string> dic = null; switch (type) { case EmailAddrType.To: dic = DicTo; break; case EmailAddrType.CC: dic = DicCC; break; case EmailAddrType.Bcc: dic = DicBcc; break; case EmailAddrType.From: throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR); } foreach (KeyValuePair <string, string> keyValue in dicAddress) { MailValidatorHelper.ValideStrNullOrEmpty(keyValue.Key, "dicAddress", MailValidatorHelper.EMAIL_ADDRESS_DIC_ERROR); if (dic.Count > 0 && !dic.ContainsKey(keyValue.Key)) { dic.Add(keyValue.Key, keyValue.Value); } } } }
/// <summary> /// 添加收件人、抄送人、密送人(每个类型中,若地址有重复,只保留第一个地址) /// </summary> /// <param name="type">类型:收件人、抄送人、密送人</param> /// <param name="addressList">Email地址列表</param> public void AddReceive(EmailAddrType type, IEnumerable <string> addressList) { MailValidatorHelper.ValideArgumentNull <IEnumerable <string> >(addressList, "addressList"); if (addressList.Count() > 0) { Dictionary <string, string> dic = null; switch (type) { case EmailAddrType.To: dic = DicTo; break; case EmailAddrType.CC: dic = DicCC; break; case EmailAddrType.Bcc: dic = DicBcc; break; case EmailAddrType.From: throw new Exception(MailValidatorHelper.EMAIL_ADDRESS_RANGE_ERROR); } foreach (string address in addressList) { MailValidatorHelper.ValideStrNullOrEmpty(address, "addressList", MailValidatorHelper.EMAIL_ADDRESS_LIST_ERROR); if (dic.Count > 0 && !dic.ContainsKey(address)) { dic.Add(address, String.Empty); } } } }
/// <summary> /// 创建 SmtpHelper 实例 /// </summary> /// <param name="type">Email类型</param> /// <param name="enableSsl">指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。</param> /// <param name="userName">用户名</param> /// <param name="password">密码</param> public SmtpHelper(EmailType type, bool enableSsl, string userName, string password) { MailValidatorHelper.ValideStrNullOrEmpty(userName, "userName"); MailValidatorHelper.ValideStrNullOrEmpty(password, "password"); this.EmailTypeConfig(type, enableSsl); SmtpClient.UseDefaultCredentials = false; SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient.Credentials = new NetworkCredential(userName, password); SmtpClient.Timeout = 100000; }
// useDefaultCredentials // false:则连接到服务器时会将 Credentials 属性中设置的值用作凭据。 // 如果UseDefaultCredentials属性设置为 false 并且尚未设置 Credentials 属性,则将邮件以匿名方式发送到服务器。 // 若SMTP 服务器要求在验证客户端的身份则会抛出异常。。 // true:System.Net.CredentialCache.DefaultCredentials (应用程序系统凭证)会随请求一起发送。 /// <summary> /// 创建 SmtpHelper 实例 /// </summary> /// <param name="host">设置 SMTP 主服务器</param> /// <param name="port">端口号</param> /// <param name="enableSsl">指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。</param> /// <param name="useDefaultCredentials">SMTP服务器是否传输系统默认凭证。</param> public SmtpHelper(string host, int port, bool enableSsl, bool useDefaultCredentials) { MailValidatorHelper.ValideStrNullOrEmpty(host, "host"); SmtpClient.Host = host; SmtpClient.Port = port; SmtpClient.EnableSsl = enableSsl; SmtpClient.UseDefaultCredentials = useDefaultCredentials; SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient.Timeout = 100000; }
/// <summary> /// 创建 SmtpHelper 实例 /// </summary> /// <param name="host">设置 SMTP 主服务器</param> /// <param name="port">端口号</param> /// <param name="enableSsl">指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。</param> /// <param name="credential">设置用于验证发件人身份的凭据。</param> public SmtpHelper(string host, int port, bool enableSsl, NetworkCredential credential) { MailValidatorHelper.ValideStrNullOrEmpty(host, "host"); MailValidatorHelper.ValideArgumentNull <NetworkCredential>(credential, "credential"); SmtpClient = new SmtpClient(host, port); SmtpClient.EnableSsl = enableSsl; SmtpClient.UseDefaultCredentials = false; SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient.Credentials = credential; SmtpClient.Timeout = 100000; }
/// <summary> /// 创建 SmtpHelper 实例 /// </summary> /// <param name="host">设置 SMTP 主服务器</param> /// <param name="port">端口号</param> /// <param name="enableSsl">指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。</param> /// <param name="userName">用户名</param> /// <param name="password">密码</param> public SmtpHelper(string host, int port, bool enableSsl, string userName, string password) { MailValidatorHelper.ValideStrNullOrEmpty(host, "host"); MailValidatorHelper.ValideStrNullOrEmpty(userName, "userName"); MailValidatorHelper.ValideStrNullOrEmpty(password, "password"); SmtpClient = new SmtpClient(host, port); SmtpClient.EnableSsl = enableSsl; SmtpClient.UseDefaultCredentials = false; SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; SmtpClient.Credentials = new NetworkCredential(userName, password); SmtpClient.Timeout = 100000; }
/// <summary> /// 添加一个电子邮件不同格式的副本。 /// </summary> /// <param name="mailContent">电子邮件内容</param> public void AddAlterViewContent(string mailContent) { MailValidatorHelper.ValideStrNullOrEmpty(mailContent, "mailContent"); AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailContent)); }
// 指定一个电子邮件不同格式的副本。 //(eg:发送HTML格式的邮件,可能希望同时提供邮件的纯文本格式,以防止一些收件人使用的电子邮件阅读程序无法显示html内容) /// <summary> /// 添加一个电子邮件不同格式的副本。 /// </summary> /// <param name="filePath">包含电子邮件内容的文件路径</param> public void AddAlterViewPath(string filePath) { MailValidatorHelper.ValideStrNullOrEmpty(filePath, "filePath"); AlternateViews.Add(new AlternateView(filePath)); }
/// <summary> /// 添加单个附件 /// </summary> /// <param name="fieldPath">待上传文件路径</param> /// <param name="fileName">文件显示名称(不带后缀)</param> public void AddAttachment(string fieldPath, string fileName = "") { MailValidatorHelper.ValideStrNullOrEmpty(fieldPath, "fieldPath"); this.InnerAddAttachment(fieldPath, fileName, false, String.Empty); }