Exemple #1
0
        /// <summary>
        /// SendMail 發送郵件
        /// </summary>
        /// <param name="mailToArray">對方郵件地址</param>
        /// <param name="fromMail">來源地址</param>
        /// <param name="fromDisplayName">來源地址別名</param>
        /// <param name="subject">主旨</param>
        /// <param name="body">內容</param>
        /// <returns></returns>
        public static bool SendMail(string[] mailToArray, string fromMail, string fromDisplayName, string subject, string body)
        {
            SmtpClient s = SimpleSmtp.CreationSmtpClient();

            //取得或設定帳密-----------------add by bruce 20150119
            string user_name = SimpleSmtp.MailUserName;
            string password  = SimpleSmtp.MailPassword;

            if (!string.IsNullOrEmpty(user_name) && !string.IsNullOrEmpty(password))
            {
                s.Credentials = new System.Net.NetworkCredential(user_name, password);
            }
            //取得或設定帳密-----------------add by bruce 20150119

            MailMessage smail  = SimpleSmtp.CreationMailMessage();
            bool        resilt = false;

            if (fromDisplayName == "" && fromMail == "")
            {
                smail.From = new MailAddress(SimpleSmtp.ServiceEmailAddress, SimpleSmtp.ServiceMailDiaplayName);
            }
            else if (fromDisplayName != "" && fromMail == "")
            {
                smail.From = new MailAddress(SimpleSmtp.ServiceEmailAddress, fromDisplayName);
            }
            else if (fromDisplayName == "" && fromMail != "")
            {
                smail.From = new MailAddress(fromMail, SimpleSmtp.ServiceMailDiaplayName);
            }
            else if (fromDisplayName != "" && fromMail != "")
            {
                smail.From = new MailAddress(fromMail, fromDisplayName);
            }

            try
            {
                if (CheckMailTo(mailToArray))
                {
                    throw MailToAddressException();
                }
                smail.Subject = subject.Trim();
                smail.Body    = body.Trim();
                foreach (string address in mailToArray)
                {
                    smail.To.Add(new MailAddress(address.Trim()));
                }
                s.Send(smail);
                resilt = true;
            }
            catch (System.FormatException ex)
            {
                throw ex;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                throw ex;
            }
            finally { smail.Dispose(); }
            return(resilt);
        }
Exemple #2
0
        /// <summary>
        /// SendMail 發送郵件
        /// </summary>
        /// <param name="mailToArray">對方郵件地址</param>
        /// <param name="mailToDisplayNameArray">對方地址別名</param>
        /// <param name="subject">主旨</param>
        /// <param name="body">內容</param>
        /// <returns></returns>
        public static bool SendMail(string[] mailToArray, string[] mailToDisplayNameArray, string subject, string body)
        {
            SmtpClient s = SimpleSmtp.CreationSmtpClient();

            //取得或設定帳密-----------------add by bruce 20150119
            string user_name = SimpleSmtp.MailUserName;
            string password  = SimpleSmtp.MailPassword;

            if (!string.IsNullOrEmpty(user_name) && !string.IsNullOrEmpty(password))
            {
                s.Credentials = new System.Net.NetworkCredential(user_name, password);
            }
            //取得或設定帳密-----------------add by bruce 20150119

            MailMessage smail  = SimpleSmtp.CreationMailMessage();
            bool        resilt = false;

            try
            {
                if (CheckMailTo(mailToArray))
                {
                    throw MailToAddressException();
                }
                smail.Subject = subject.Trim();
                smail.Body    = body.Trim();
                for (int i = 0; i < mailToArray.Length; i++)
                {
                    if (mailToDisplayNameArray.Length == mailToArray.Length)
                    {
                        smail.To.Add(new MailAddress(mailToArray[i].Trim(), mailToDisplayNameArray[i].Trim()));
                    }
                    else
                    {
                        smail.To.Add(new MailAddress(mailToArray[i].Trim()));
                    }
                }
                s.Send(smail);
                resilt = true;
            }
            catch (System.FormatException ex)
            {
                throw ex;
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                throw ex;
            }
            finally { smail.Dispose(); }
            return(resilt);
        }