/// <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> /// 添加建立安全套接字层 (SSL) 连接的证书 /// </summary> public SmtpHelper AddClientCertificate(X509Certificate certificate) { MailValidatorHelper.ValideArgumentNull <X509Certificate>(certificate, "certificate"); SmtpClient.EnableSsl = true; SmtpClient.ClientCertificates.Add(certificate); return(this); }
/// <summary> /// 创建 SmtpHelper 实例 /// </summary> /// <param name="type">Email类型</param> /// <param name="enableSsl">指定 SmtpClient 是否使用安全套接字层 (SSL) 加密连接。</param> /// <param name="credential">设置用于验证发件人身份的凭据。</param> public SmtpHelper(EmailType type, bool enableSsl, NetworkCredential credential) { MailValidatorHelper.ValideArgumentNull <NetworkCredential>(credential, "credential"); this.EmailTypeConfig(type, 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="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> /// 添加一个电子邮件不同格式的副本。 /// </summary> /// <param name="alternateView">电子邮件视图</param> public void AddAlternateView(AlternateView alternateView) { MailValidatorHelper.ValideArgumentNull <AlternateView>(alternateView, "alternateView"); AlternateViews.Add(alternateView); }
/// <summary> /// 添加一个电子邮件不同格式的副本。 /// </summary> /// <param name="contentStream">电子邮件内容流</param> public void AddAlterViewStream(Stream contentStream) { MailValidatorHelper.ValideArgumentNull <Stream>(contentStream, "contentStream"); AlternateViews.Add(new AlternateView(contentStream)); }
/// <summary> /// 添加单个附件 /// </summary> /// <param name="attachment">Attachment附件实例</param> public void AddAttachment(Attachment attachment) { MailValidatorHelper.ValideArgumentNull <Attachment>(attachment, "attachment"); Attachments.Add(attachment); }