Monday, May 20, 2013

Script to send e-mail by tracking the package status using the Script Task in SSIS

private void Mailsend()
        {
            string fromID = Dts.Variables["User::MailTo"].Value.ToString();
            string toID = Dts.Variables["User::MailTo"].Value.ToString();
            string subject = Dts.Variables["User::MailSubject"].Value.ToString();
            string messageText = Dts.Variables["User::MailMessage"].Value.ToString();
            string exchangeServerName = Dts.Variables["User::ExchangeServerName"].Value.ToString();
            string packagestatus = Dts.Variables["User::PackageStatus"].Value.ToString();
            string restoredata = Dts.Variables["User::RestoreDatabaseStatus"].Value.ToString();

            string sPackageName = Dts.Variables["User::sPackageName"].Value.ToString();
            string sPackageRunStartDate = Dts.Variables["User::sPackageRunStartDate"].Value.ToString();
            string sPackageRunEndDate = Dts.Variables["User::sPackageRunEndDate"].Value.ToString();
            string sPackageStatus = Dts.Variables["User::sPackageStatus"].Value.ToString();


            string sJobName = Dts.Variables["User::JobName"].Value.ToString();
            string sJobLastRunDate = Dts.Variables["User::JobLastRunDate"].Value.ToString();
            string sJobLastRunTime = Dts.Variables["User::JobLastRunTime"].Value.ToString();
            string sJobLastRunDuration = Dts.Variables["User::JobLastRunDuration"].Value.ToString();

            string senderMailID = fromID;
            MailAddress from = new MailAddress(senderMailID);
            string receiverMailID = toID;
            MailAddress to = new MailAddress(receiverMailID);
            MailMessage msg = new MailMessage(from, to);
            msg.Subject = subject;
            StringBuilder mailString = new StringBuilder();
            // mailString.AppendLine(messageText);
            try
            {
                if ((sJobName == null && sPackageName == null) || (sJobName == "" && sPackageName == "" && sPackageStatus != "Completed") )
                {
                    mailString.Append("<html><body><H3>" + messageText + "</h3>" + "</br>" + "LSDM20BackupRestore Status" + "</br>" + "<table border='1' width='100%'> <tr border=2 bgcolor='gray'><td border=2>PackageName</td><td>LastRunStartDate</td><td>LastRunEndDate</td><td>PackageStaus</td></tr> <tr> <td border=2>" + sPackageName + "</td><td>" + sPackageRunStartDate + "</td><td>" + sPackageRunEndDate + "</td><td>" + sPackageStatus + "</td> </tr> </table></body></html>" + "</br>" + "LoadCultivationTrack Job Status" + "</br>" + "<table border='1' width='100%'> <tr border=2 bgcolor='gray'><td border=2>PackageName</td><td>JobLastRunDate</td><td>JobLastRunTime</td><td>JobLastRunDuration</td></tr> <tr> <td border=2>" + sJobName + "</td><td>" + sJobLastRunDate + "</td><td>" + sJobLastRunTime + "</td><td>" + sJobLastRunDuration + "</td> </tr> </table></body></html>");
                    msg.IsBodyHtml = true;
                    msg.Body = mailString.ToString();
                    SmtpClient smtpClient = new SmtpClient(exchangeServerName);
                    smtpClient.Send(msg);
                }
                else if ((sJobName != null && sPackageName == null) ||  (sJobName != "" && sPackageName == "" ))
                {
                    mailString.Append("<html><body><H3>" + messageText + "</h3>" + "</br>" +  "LoadCultivationTrack Job Status" + "</br>" + "<table border='1' width='100%'> <tr border=2 bgcolor='gray'><td border=2>PackageName</td><td>JobLastRunDate</td><td>JobLastRunTime</td><td>JobLastRunDuration</td></tr> <tr> <td border=2>" + sJobName + "</td><td>" + sJobLastRunDate + "</td><td>" + sJobLastRunTime + "</td><td>" + sJobLastRunDuration + "</td> </tr> </table></body></html>");
                    msg.IsBodyHtml = true;
                    msg.Body = mailString.ToString();
                    SmtpClient smtpClient = new SmtpClient(exchangeServerName);
                    smtpClient.Send(msg);
                }
                else if ((sJobName == null && sPackageName != null) ||  (sJobName == "" && sPackageName != "" ))
                {
                    mailString.Append("<html><body><H3>" + messageText + "</h3>" + "</br>" + "LSDM20BackupRestore Status" + "</br>" + "<table border='1' width='100%'> <tr border=2 bgcolor='gray'><td border=2>PackageName</td><td>LastRunStartDate</td><td>LastRunEndDate</td><td>PackageStaus</td></tr> <tr> <td border=2>" + sPackageName + "</td><td>" + sPackageRunStartDate + "</td><td>" + sPackageRunEndDate + "</td><td>" + sPackageStatus + "</td> </tr> </table></body></html>");
                    msg.IsBodyHtml = true;
                    msg.Body = mailString.ToString();
                    SmtpClient smtpClient = new SmtpClient(exchangeServerName);
                    smtpClient.Send(msg);
                }
                
            }
            catch (Exception)
            {
                //Log.Error(String.Format("Resetpassword.ascx.cs: There was an error during sending mail. Details: {0}.\n{1}", ex.Message, ex.StackTrace), new object());
            }
            finally
            {
                msg.Dispose();
            }
        }

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.