About Me

Barking, Essex, United Kingdom
MCITP DBA ,MCITP BI & Oracle OCP 11G

Monday, March 2, 2015

c# Script APP to send the email in the html format

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Net.Mail;
using System.IO;

namespace script
{
    class Program
    {
        static void Main(string[] args)
        {

            // Storing SSIS variables in .Net variables. You could skip this step and call the SSIS variables in the actual mail code
            // to reduce the number of code lines. Or you could fill these .Net variables with hardcoded values.
            String SendMailFrom = "xxx";
            String SendMailTo = "xxxx";
            String SendMailSubject = "bla";
            String SendMailBody = "ygugi";
            string AttachmentPath = @"xxx";

            // string sEmailUser = Dts.Variables["MailFrom"].Value.ToString();
            string sEmailPassword = "";



            SqlConnection SQLConn = new SqlConnection();
            SQLConn.ConnectionString = @"Data Source=xxxx;Initial Catalog=xxxx;Integrated Security=SSPI";
            SQLConn.Open();
            string strQuery = null;
            strQuery = "select DocumentPath,DocumentName,EntityId,DateCreated,rowNum,name,value " + "from CSDDetails ";
            SqlCommand myCommand = new SqlCommand(strQuery, SQLConn);
            myCommand.CommandTimeout = 0;
            myCommand.ExecuteNonQuery();
            SqlDataReader myReader = default(SqlDataReader);
            myReader = myCommand.ExecuteReader();
            if (myReader.HasRows)
            {
                SendMailBody = "Please refer the attached synopsis template for the below customers.

"
;
                SendMailBody = SendMailBody + "Here it goes..
"
;
                SendMailBody = SendMailBody + ""
;
                SendMailBody = SendMailBody + "
DocumentPath
DocumentName
EntityId
DateCreated
rowNum
name/th>
value
";
                while (myReader.Read())
                {
                    SendMailBody = SendMailBody + "
"
+ myReader.GetValue(0).ToString() + "" + myReader.GetValue(1).ToString() + "" + myReader.GetValue(2).ToString() + "" + myReader.GetValue(3).ToString() + "" + myReader.GetValue(4).ToString() + "" + myReader.GetValue(5).ToString() + "

"
+ myReader.GetValue(6).ToString() + "";
                }
                SendMailBody = SendMailBody + "
";

            }
            myReader.Close();
            SQLConn.Close();
           // Dts.TaskResult = (int)ScriptResults.Success;



           // Get SMTP Server from SMTP Connection Manager. Alternative is to use extra variables or paramters instead:
            // String SmtpServer = Dts.Variables["SmtpServer"].Value.ToString();
            String SmtpServer = "gmail";

            // Create an email and change the format to HTML
            MailMessage myHtmlFormattedMail = new MailMessage(SendMailFrom, SendMailTo, SendMailSubject, SendMailBody);
            myHtmlFormattedMail.IsBodyHtml = true;
            string[] filename = Directory.GetFiles(AttachmentPath);
            foreach (string fname in filename)
                myHtmlFormattedMail.Attachments.Add(new Attachment(fname));
            //myHtmlFormattedMail.Attachments.Add(new Attachment(AttachmentPath));

            // Create a SMTP client to send the email
            SmtpClient mySmtpClient = new SmtpClient(SmtpServer);
            mySmtpClient.Port = 25; // If you want to use a different portnumber instead of the default. Else remove this line.
            //mySmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
            System.Net.NetworkCredential myCredentials = new System.Net.NetworkCredential(SendMailFrom, sEmailPassword);
            mySmtpClient.Credentials = myCredentials;
            mySmtpClient.UseDefaultCredentials = false;




            mySmtpClient.Send(myHtmlFormattedMail);

            // Close Script Task with success
           // Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}



No comments:

Post a Comment