星期二, 9月 12, 2017

C# SendMail

 public void SendMail(string strMailServer, string strMailSubject, string strMailFrom, string strMailTo, string strMailCc, string strMailBody, string strRPTPATH)
        {

            string strERRORMSG = "";
            try
            {
                //create the mail message
                MailMessage Mailstr = new MailMessage();

                //set the addresses
                Mailstr.From = new MailAddress(strMailFrom);
                Mailstr.To.Add(strMailTo);
                if (strMailCc != "")
                    Mailstr.CC.Add(strMailCc);
                // set the content
                Mailstr.Subject = strMailSubject;
                Mailstr.Body = strMailBody + "<br><br><br>";
                Mailstr.IsBodyHtml = true;
                if (strRPTPATH != "")
                {
                    string file = @strRPTPATH;
                    Attachment data = new Attachment(file);
                    Mailstr.Attachments.Add(data);
                }
                //Send the message.
                SmtpClient client = new SmtpClient(strMailServer);
                client.Send(Mailstr);

            }
            catch (System.Exception Ex)
            {
                strERRORMSG = "ERROR MESSAGE:" + Ex.ToString();
                SendMail(mailserver, "JOB失敗", ADMIN_EMAIL, ADMIN_EMAIL, "", "更新 ERROR MESSAGE:" + Ex.ToString(), "");
            }

        }

沒有留言: