Beispiel #1
0
        /// <summary>
        /// Sending An Email with master mail template
        /// </summary>
        /// <param name="mailFrom">Mail From</param>
        /// <param name="mailTo">Mail To</param>
        /// <param name="mailCC">Mail CC</param>
        /// <param name="mailBCC">Mail BCC</param>
        /// <param name="subject">Mail Subject</param>
        /// <param name="body">Mail Body</param>
        /// <param name="emailType">Email Type</param>
        /// <param name="attachment">Mail Attachment</param>
        /// <param name="attachmentByteList">Attachment byte list for the mail</param>
        /// <param name="attachmentName">Attachment file name for the mail</param>
        /// <returns>return send status</returns>
        public static bool Send(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, EmailType emailType, string attachment, List <byte[]> attachmentByteList = null, string attachmentName = null)
        {
            if (ProjectConfiguration.IsEmailTest)
            {
                mailTo  = ProjectConfiguration.TestEmailToAddress;
                mailCC  = string.Empty;
                mailBCC = string.Empty;
            }

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            if (!string.IsNullOrEmpty(mailTo))
            {
                mailTo = mailTo.Trim(';').Trim(',');
            }

            if (!string.IsNullOrEmpty(mailCC))
            {
                mailCC = mailCC.Trim(';').Trim(',');
            }

            if (!string.IsNullOrEmpty(mailBCC))
            {
                mailBCC = mailBCC.Trim(';').Trim(',');
            }

            if (ValidateEmail(mailFrom, mailTo) && (string.IsNullOrEmpty(mailCC) || ValidateEmail(mailCC)) && (string.IsNullOrEmpty(mailBCC) || ValidateEmail(mailBCC)))
            {
                System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage();
                mailMesg.From = new System.Net.Mail.MailAddress(mailFrom);
                if (!string.IsNullOrEmpty(mailTo))
                {
                    mailTo = mailTo.Replace(";", ",");
                    mailMesg.To.Add(mailTo);
                }

                if (!string.IsNullOrEmpty(mailCC))
                {
                    mailCC = mailCC.Replace(";", ",");
                    mailMesg.CC.Add(mailCC);
                }

                if (!string.IsNullOrEmpty(mailBCC))
                {
                    mailBCC = mailBCC.Replace(";", ",");
                    mailMesg.Bcc.Add(mailBCC);
                }

                if (!string.IsNullOrEmpty(attachment) && string.IsNullOrEmpty(attachmentName))
                {
                    string[] attachmentArray = attachment.Trim(';').Split(';');
                    foreach (string attachFile in attachmentArray)
                    {
                        try
                        {
                            System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(attachFile);
                            mailMesg.Attachments.Add(attach);
                        }
                        catch
                        {
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(attachment) && !string.IsNullOrEmpty(attachmentName))
                {
                    string[] attachmentArray     = attachment.Trim(';').Split(';');
                    string[] attachmentNameArray = attachmentName.Trim(';').Split(';');

                    if (attachmentArray.Length == attachmentNameArray.Length)
                    {
                        for (int cnt = 0; cnt <= attachmentArray.Length - 1; cnt++)
                        {
                            if (System.IO.File.Exists(attachmentArray[cnt]))
                            {
                                try
                                {
                                    string fileName = ConvertTo.String(attachmentName[cnt]);
                                    if (!string.IsNullOrEmpty(fileName))
                                    {
                                        System.IO.FileStream       fs     = new System.IO.FileStream(attachmentArray[cnt], System.IO.FileMode.Open, System.IO.FileAccess.Read);
                                        System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(fs, fileName);
                                        mailMesg.Attachments.Add(attach);
                                    }
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }

                if (attachmentByteList != null && attachmentName != null)
                {
                    string[] attachmentNameArray = attachmentName.Trim(';').Split(';');

                    if (attachmentByteList.Count == attachmentNameArray.Length)
                    {
                        for (int cnt = 0; cnt <= attachmentByteList.Count - 1; cnt++)
                        {
                            string fileName = attachmentNameArray[cnt];
                            if (!string.IsNullOrEmpty(fileName))
                            {
                                try
                                {
                                    MemoryStream ms = new MemoryStream(attachmentByteList[cnt]);
                                    System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, fileName);
                                    mailMesg.Attachments.Add(attach);
                                }
                                catch
                                {
                                }
                            }
                        }
                    }
                }

                mailMesg.Subject = subject;
                mailMesg.AlternateViews.Add(GetMasterBody(body, subject, emailType));
                mailMesg.IsBodyHtml = true;

                System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient();
                try
                {
                    objSMTP.Send(mailMesg);
                    return(true);
                }
                catch (Exception ex)
                {
                    LogWritter.WriteErrorFile(ex);
                }
                finally
                {
                    objSMTP.Dispose();
                    mailMesg.Dispose();
                    mailMesg = null;
                }
            }

            return(false);
        }
Beispiel #2
0
        public static bool SendForWindowService(string mailFrom, string mailTo, string mailCC, string mailBCC, string subject, string body, string path)
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            if (!string.IsNullOrEmpty(mailTo))
            {
                mailTo = mailTo.Trim(';').Trim(',');
            }

            if (!string.IsNullOrEmpty(mailCC))
            {
                mailCC = mailCC.Trim(';').Trim(',');
            }

            if (!string.IsNullOrEmpty(mailBCC))
            {
                mailBCC = mailBCC.Trim(';').Trim(',');
            }

            if (ValidateEmail(mailFrom, mailTo) && (string.IsNullOrEmpty(mailCC) || ValidateEmail(mailCC)) && (string.IsNullOrEmpty(mailBCC) || ValidateEmail(mailBCC)))
            {
                System.Net.Mail.MailMessage mailMesg = new System.Net.Mail.MailMessage();
                mailMesg.From = new System.Net.Mail.MailAddress(mailFrom);
                if (!string.IsNullOrEmpty(mailTo))
                {
                    mailTo = mailTo.Replace(";", ",");
                    mailMesg.To.Add(mailTo);
                }

                if (!string.IsNullOrEmpty(mailCC))
                {
                    mailCC = mailCC.Replace(";", ",");
                    mailMesg.CC.Add(mailCC);
                }

                if (!string.IsNullOrEmpty(mailBCC))
                {
                    mailBCC = mailBCC.Replace(";", ",");
                    mailMesg.Bcc.Add(mailBCC);
                }

                mailMesg.Subject = subject;

                mailMesg.AlternateViews.Add(GetMasterBodyForWindowService(body, subject, path));
                mailMesg.IsBodyHtml = true;

                System.Net.Mail.SmtpClient objSMTP = new System.Net.Mail.SmtpClient();
                try
                {
                    objSMTP.Send(mailMesg);
                    return(true);
                }
                catch (Exception ex)
                {
                    LogWritter.WriteErrorFile(ex);
                }
                finally
                {
                    objSMTP.Dispose();
                    mailMesg.Dispose();
                    mailMesg = null;
                }
            }

            return(false);
        }