function of sending an email

 ///
        /// 發送一封符合條件的郵件
        /// </summary>
        public void SendAnEmail(
            string SmtpServer,
            string SmtpServerUsername,
            string SmtpServerPassword,
            string fromEmail,
            string toEmail,
            string subject,
            string content,
            int sendFailTimes,
            int ResendTimes)
        {
            // 發送郵件
            System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
            System.Web.Mail.SmtpMail.SmtpServer = SmtpServer;
            if (SmtpServerUsername.Trim().Length > 0)
            {
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", SmtpServerUsername.Trim()); //username
                message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", SmtpServerPassword.Trim()); //password
            }
            message.BodyFormat = System.Web.Mail.MailFormat.Html;
            if (fromEmail != string.Empty)
                message.From = fromEmail;
            else
                message.From = "";
            message.To = toEmail;
            if (subject != string.Empty)
                message.Subject = subject;
            else
                message.Subject = "";
            message.Body = content;

            bool bSendSuccess = true;
            try
            {
                System.Web.Mail.SmtpMail.Send(message);
            }
            catch (Exception ex)
            {
                bSendSuccess = false;
            }
            int status = 0;// 0 還沒發走(沒發過或發送失敗),1:正在發送, 2:已經發走
            int isClose = 0;// 1 表示關閉,0 表示沒關閉
            int newSendFailTimes = sendFailTimes;
            if (bSendSuccess)
            {
                status = 2;
                isClose = 1;
            }
            else
            {
                newSendFailTimes += 1;
                if (newSendFailTimes >= ResendTimes)
                    isClose = 1;
            }
            // 更新Email狀態
            Response.Write(status);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章