Esempio n. 1
0
        public static bool email_send(string filepath, string ErrorFilePath, string SystemMachineName, string[] EmailCollection, string projectName)
        {
            ExcelReport.ReadData Readdata = new ExcelReport.ReadData();
            try
            {
                //Console.WriteLine("In email_send method");
                List <String> to = new List <string>(); List <String> cc = new List <string>(); List <String> bcc = new List <string>(); List <String> Attachment = new List <string>();

                //Set Email To path, It will take path from App.config if available, otherwise default loction will be Input file path
                To = string.Concat((ConfigurationManager.AppSettings["ToEmail"].ToString() == "") ? Readdata.ProcessOnCollection(EmailCollection, "EmailTo") : ConfigurationManager.AppSettings["ToEmail"].ToString());

                //Set Email CC path, It will take path from App.config if available, otherwise default loction will be Input file path
                Cc = string.Concat((ConfigurationManager.AppSettings["CcEmail"].ToString() == "") ? Readdata.ProcessOnCollection(EmailCollection, "EmailCc") : ConfigurationManager.AppSettings["CcEmail"].ToString());

                //Set Email Subject path, It will take path from App.config if available, otherwise default loction will be Input file path
                Subj = string.Concat((ConfigurationManager.AppSettings["SubjectEmail"].ToString() == "") ? Readdata.ProcessOnCollection(EmailCollection, "EmailSubject") : ConfigurationManager.AppSettings["SubjectEmail"].ToString());

                //string To = ConfigurationManager.AppSettings["EmailTo"].ToString();

                //To = Readdata.ProcessOnCollection(EmailCollection, "EmailTo");

                //string Cc = ConfigurationManager.AppSettings["EmailCc"];
                // Cc = Readdata.ProcessOnCollection(EmailCollection, "EmailCC");



                //string Subj = ConfigurationManager.AppSettings["EmailSubject"];
                // Subj = Readdata.ProcessOnCollection(EmailCollection, "EmailSubject");

                string Message = string.Empty;

                //= "Hello," + Environment.NewLine + "Please find attached excel sheet to view automation test results. " + Environment.NewLine
                //      + Environment.NewLine + "You can find more details -  " + SystemMachineName + " in " + Environment.CurrentDirectory
                //     + Environment.NewLine + Environment.NewLine + "Thanks." + Environment.NewLine + "ProCare Transportation and Language Services";

                System.Text.StringBuilder body = new System.Text.StringBuilder();
                body.Clear();
                body.Length = 0;
                string bodyText = PopulateBody(projectName, TestSuit.pass.ToString(), TestSuit.fail.ToString(), TestSuit.skip.ToString(), TestSuit.error.ToString(), (TestSuit.pass + TestSuit.fail + TestSuit.skip + TestSuit.error).ToString());
                body.Append(bodyText);

                //body.Append("<table><tr><td colspan='3' class='Content'> <h3 align='center' id='reportHeader'> ${ReportHeader} </h3></td></tr>");
                // body.Append("");

                /*body.Append("<table border='0' class='subtable' cellpadding='0' cellspacing='0' style='background-color: White; font-family:Calibri;'>");
                 * body.Append("<tr><td colspan='12'>Hello,</td></tr>" + "\r\n");
                 * body.Append("<tr style='height: 6px'><td colspan='12'><br></td></tr>" + "\r\n");
                 * body.Append("<tr><td colspan='12'>Please find attached excel sheet to view automation test results.</td></tr>" + "\r\n");
                 * body.Append("<tr><td colspan='12'>You can find more details -  " + SystemMachineName + " in " + Environment.CurrentDirectory + "</td></tr>" + "\r\n");
                 * body.Append("<tr style='height: 6px'><td colspan='12'><br></td></tr>" + "\r\n");
                 * body.Append("<tr><td colspan='12'>Thanks, </td></tr>" + "\r\n");
                 * body.Append("<tr style='height: 6px'><td colspan='12'><br></td></tr>" + "\r\n");
                 * body.Append("<tr><td colspan='12' style='color:#D50000; font-size:14px; font-style:italic; font-family: Times New Roman;'>\"Tailors Mark\"</td></tr></table>" + "\r\n");*/

                Message = body.ToString();



                if (!string.IsNullOrEmpty(To))
                {
                    string[] ToMuliId = To.Split(';');
                    foreach (string ToEMailId in ToMuliId)
                    {
                        if (ToEMailId != "")
                        {
                            to.Add(ToEMailId);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(Cc))
                {
                    string[] CCId = Cc.Split(';');
                    foreach (string CCEmail in CCId)
                    {
                        if (CCEmail != "")
                        {
                            cc.Add(CCEmail);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(filepath))
                {
                    string[] mailAttachments = filepath.Split(';');
                    foreach (string mailAttach in mailAttachments)
                    {
                        if (mailAttach != "")
                        {
                            Attachment.Add(mailAttach);
                        }
                    }
                }

                if (!string.IsNullOrEmpty(ErrorFilePath))
                {
                    string[] mailAttachments = ErrorFilePath.Split(';');
                    foreach (string mailAttach in mailAttachments)
                    {
                        if (mailAttach != "")
                        {
                            Attachment.Add(mailAttach);
                        }
                    }
                }

                bool   success;
                string ErrMsg = "";
                try
                {
                    MailMessage mailMessage = new MailMessage();
                    mailMessage            = SetMailParameters(to, ConfigurationManager.AppSettings["FromEmail"].ToString(), Subj, Message, mailMessage);
                    mailMessage            = SetCopyParameters(cc, bcc, mailMessage);
                    mailMessage            = SetAttachmentPath(Attachment, mailMessage);
                    mailMessage.IsBodyHtml = true;

                    SmtpClient client = new SmtpClient();

                    string UserName = ConfigurationManager.AppSettings["Username"];
                    string Host     = ConfigurationManager.AppSettings["EmailHost"];
                    bool   Ssl      = Convert.ToBoolean(ConfigurationManager.AppSettings["EmailSsl"]);
                    string pwd      = ConfigurationManager.AppSettings["EmailPwd"];
                    string Port     = ConfigurationManager.AppSettings["EmailPort"];

                    client = AuthenticateCredentials(Host, Convert.ToInt16(Port), Ssl, UserName, pwd, client);

                    //  Console.WriteLine("About to send");
                    client.Send(mailMessage);
                    success = true;
                    Console.WriteLine("Sent");
                }
                catch (Exception Ex)
                {
                    TestSuit.TakeScreenShot("Fail");
                    logger.WriteLog(Ex);
                    success = false;
                    ErrMsg  = Ex.Message;
                }
                return(success);
            }
            catch (Exception Ex)
            {
                TestSuit.TakeScreenShot("Fail");
                logger.WriteLog(Ex);
                return(false);
            }
        }