Пример #1
0
        /// <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);
                    }
                }
            }
        }
Пример #2
0
        /// <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);
        }
Пример #3
0
        /// <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);
                    }
                }
            }
        }
Пример #4
0
        /// <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);
            }
        }
Пример #5
0
        /// <summary>
        /// 添加建立安全套接字层 (SSL) 连接的证书
        /// </summary>
        public SmtpHelper AddClientCertificate(X509Certificate certificate)
        {
            MailValidatorHelper.ValideArgumentNull <X509Certificate>(certificate, "certificate");

            SmtpClient.EnableSsl = true;
            SmtpClient.ClientCertificates.Add(certificate);

            return(this);
        }
Пример #6
0
        /// <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;
        }
Пример #7
0
        // 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;
        }
Пример #8
0
        /// <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;
        }
Пример #9
0
        /// <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;
        }
Пример #10
0
        /// <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;
        }
Пример #11
0
 /// <summary>
 /// 添加一个电子邮件不同格式的副本。
 /// </summary>
 /// <param name="alternateView">电子邮件视图</param>
 public void AddAlternateView(AlternateView alternateView)
 {
     MailValidatorHelper.ValideArgumentNull <AlternateView>(alternateView, "alternateView");
     AlternateViews.Add(alternateView);
 }
Пример #12
0
 /// <summary>
 /// 添加一个电子邮件不同格式的副本。
 /// </summary>
 /// <param name="contentStream">电子邮件内容流</param>
 public void AddAlterViewStream(Stream contentStream)
 {
     MailValidatorHelper.ValideArgumentNull <Stream>(contentStream, "contentStream");
     AlternateViews.Add(new AlternateView(contentStream));
 }
Пример #13
0
 /// <summary>
 /// 添加一个电子邮件不同格式的副本。
 /// </summary>
 /// <param name="mailContent">电子邮件内容</param>
 public void AddAlterViewContent(string mailContent)
 {
     MailValidatorHelper.ValideStrNullOrEmpty(mailContent, "mailContent");
     AlternateViews.Add(AlternateView.CreateAlternateViewFromString(mailContent));
 }
Пример #14
0
        // 指定一个电子邮件不同格式的副本。
        //(eg:发送HTML格式的邮件,可能希望同时提供邮件的纯文本格式,以防止一些收件人使用的电子邮件阅读程序无法显示html内容)

        /// <summary>
        /// 添加一个电子邮件不同格式的副本。
        /// </summary>
        /// <param name="filePath">包含电子邮件内容的文件路径</param>
        public void AddAlterViewPath(string filePath)
        {
            MailValidatorHelper.ValideStrNullOrEmpty(filePath, "filePath");
            AlternateViews.Add(new AlternateView(filePath));
        }
Пример #15
0
        /// <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);
        }
Пример #16
0
 /// <summary>
 /// 添加单个附件
 /// </summary>
 /// <param name="attachment">Attachment附件实例</param>
 public void AddAttachment(Attachment attachment)
 {
     MailValidatorHelper.ValideArgumentNull <Attachment>(attachment, "attachment");
     Attachments.Add(attachment);
 }
Пример #17
0
        /// <summary>
        /// 检查 发件人、收件人、抄送人、密送人 邮箱地址
        /// </summary>
        /// <param name="infoBuilder">StringBuilder实例</param>
        /// <param name="dicMsg">将检查信息收集到此集合</param>
        /// <param name="type">接收邮件地址类型</param>
        private void InnerCheckAddress(StringBuilder infoBuilder, Dictionary <MailInfoType, string> dicMsg, EmailAddrType type)
        {
            Dictionary <string, string> dic = null;
            MailInfoType addressFormat      = MailInfoType.None;
            MailInfoType addressEmpty       = MailInfoType.None;
            bool         allowEmpty         = true;
            // 只有 发件人 是单个地址,特别进行处理
            bool hasHandle = false;

            switch (type)
            {
            case EmailAddrType.From:
            {
                // 标识为已处理
                hasHandle = true;

                allowEmpty = false;
                if (From.Length == 0)
                {
                    dicMsg.Add(MailInfoType.FromEmpty, MailInfoHelper.GetMailInfoStr(MailInfoType.FromEmpty));
                }
                else if (!MailValidatorHelper.IsEmail(From))
                {
                    string strTemp = infoBuilder.AppendFormat(MailInfoHelper.GetMailInfoStr(MailInfoType.FromFormat), FromDisplayName, From).ToString();
                    dicMsg.Add(MailInfoType.FromFormat, strTemp);
                    infoBuilder.Length = 0;
                }
            }
            break;

            case EmailAddrType.To:
            {
                dic          = m_DicTo;
                addressEmpty = MailInfoType.ToEmpty;

                allowEmpty    = false;
                addressFormat = MailInfoType.ToFormat;
            }
            break;

            case EmailAddrType.CC:
            {
                dic           = m_DicCC;
                addressFormat = MailInfoType.CCFormat;

                allowEmpty   = true;
                addressEmpty = MailInfoType.None;
            }
            break;

            case EmailAddrType.Bcc:
            {
                dic           = m_DicBcc;
                addressFormat = MailInfoType.BccFormat;

                allowEmpty   = true;
                addressEmpty = MailInfoType.None;
            }
            break;
            }


            #region 处理 收件人、抄送人、密送人

            if (!hasHandle)
            {
                if (dic == null)
                {
                    if (!allowEmpty)
                    {
                        // 地址为空
                        dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                    }
                }
                else
                {
                    if (dic.Count > 0)
                    {
                        string strTemp = String.Empty;
                        // 邮件地址格式
                        foreach (KeyValuePair <string, string> keyValue in dic)
                        {
                            if (keyValue.Key.Length == 0)
                            {
                                if (!allowEmpty)
                                {
                                    // 地址为空
                                    dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                                }
                            }
                            else if (!MailValidatorHelper.IsEmail(keyValue.Key))
                            {
                                if (strTemp.Length == 0)
                                {
                                    strTemp = MailInfoHelper.GetMailInfoStr(addressFormat);
                                }
                                if (infoBuilder.Length > 0)
                                {
                                    infoBuilder.AppendLine();
                                }
                                infoBuilder.AppendFormat(strTemp, keyValue.Value, keyValue.Key);
                            }
                        }
                        if (infoBuilder.Length > 0)
                        {
                            dicMsg.Add(addressFormat, infoBuilder.ToString());
                            infoBuilder.Length = 0;
                        }
                    }
                    else if (!allowEmpty)
                    {
                        // 地址为空
                        dicMsg.Add(addressEmpty, MailInfoHelper.GetMailInfoStr(addressEmpty));
                    }
                }
            }

            #endregion
        }