1
Answer

How To Call Methods In Thread For Sending Bulk Mail

sagar Bhosale

sagar Bhosale

11y
1.2k
1
Hi Friends,

HoW To Call  Below Methods Call In Threads 

Becouse I get Time Out Error while Sending Mail

So I want To Call This methods in Thread



public void clientMail(string client_id, string FileName, string TimeFrame, string MailId, string Title, string Body, string reportPath)
        {
            try
            {
                string strFromDis = string.Empty;
                string strSub = FileName + " of " + Client_Name + " " + FormatFunctions.C_Format.M_FormatDate(DateTime.Now.ToString(), "dd MMM,yyyy").ToString();
                string strBody = string.Empty;

                strFromDis = "Team";


                string[] ArrayEmail;
                MailId = MailId.Replace(';', ',');
                MailId = MailId.Replace(',', ',');
                ArrayEmail = MailId.Split(',');


                strBody = "<html><head><META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=us-ascii'></head><body> <div>";
                strBody += "<table cellspacing='0' cellpadding='0' border='0' width='100%'>";
                strBody += "<tr><td>";
                strBody += Body;
                strBody += "</td></tr>";
                strBody += "</table>";
                strBody += "</div></body></html>";


                for (int x = 0; x < ArrayEmail.Length; x++)
                {


                    try
                    {
                        MailMessage msg = new MailMessage();
                        msg.From = new MailAddress(strFrom, strFromName);
                        msg.IsBodyHtml = true;

                        msg.Subject = strSub;
                        msg.Body = strBody;
                        msg.Attachments.Add(new Attachment(@reportPath));
                        FileInfo finfo = new FileInfo(@reportPath);
                        long FileInBytes = finfo.Length;
                        long FileInKB = finfo.Length / 1024;
                        long FileInMB = FileInKB / 1024;


                        if (FileInMB < 12288)
                        {
                            if (OAttSend == "True")
                            {
                                msg.Attachments.Add(new Attachment(OtherAttachment));
                            }

                            
                            System.Net.NetworkCredential mailAuthentication = new System.Net.NetworkCredential(strAuthEmail, strMailAuthPass);
                            System.Net.Mail.SmtpClient mailClient = new System.Net.Mail.SmtpClient(strSMTP, Int32.Parse(strPort));
                            mailClient.Credentials = mailAuthentication;
                            FirstExecution = "F";
                            msg.To.Add(new MailAddress(ArrayEmail[x], ""));
                            //ThreadPool.QueueUserWorkItem(delegate { mailClient.Send(msg); });
                            mailClient.Send(msg);
                            mailClient.UseDefaultCredentials = false;
                            mailClient.Credentials = mailAuthentication;
                            mailClient.Timeout = 999999;
                            Process_Error.WriteSuccessLogMail(client_id, "Mail Sent Successfully", TimeFrame, FileName, ArrayEmail[x], FirstExecution);
                        }

                        else
                        {
                            Process_Error.WriteErrorLogMail(client_id, "Porfolio Report exceeds 12 Mb", TimeFrame, FileName, ArrayEmail[x], FirstExecution);
                        }
                    }
                    catch (Exception ex)
                    {

                        Process_Error.WriteErrorLogMail(client_id, ex.Message.ToString(), TimeFrame, FileName, ArrayEmail[x], FirstExecution);
                    }
                }

            }
            catch (Exception ex)
            {
                ;
            }
        }
Answers (1)