Esempio n. 1
0
        public void SendMailSmtp(string subject, string body, bool isBodyHtml,  MailAddress[] mailTo, MailAddress mailFrom)
        {
            if (mailTo == null || !mailTo.Any()) throw new Exception("Не указаны получатели письма!");

            if (String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            //if (!String.IsNullOrEmpty(settings.Login))
            //{
            //    client.Credentials = new NetworkCredential(settings.Login, settings.Password);
            //    mail.From = new MailAddress(settings.Login);
            //}
            //else
            //{
            mail.From = mailFrom;
            //}

            client.EnableSsl = false;//settings.EnableSsl;

            foreach (MailAddress mailAddress in mailTo)
            {
                mail.To.Add(mailAddress);
            }

            //Шлем копию письма если надо
            //if (!String.IsNullOrEmpty(settings.MailCopyTo))
            //{
            //    mail.CC.Add(new MailAddress(settings.MailCopyTo));
            //}
            //else
            //{
            //    mail.CC.Clear();
            //}

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;

            client.Host = "ums-1";

            client.Send(mail);
        }
Esempio n. 2
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo, MailAddress mailFrom, bool isTest = false)
        {
            if ((mailTo == null || !mailTo.Any()) && (hiddenMailTo == null || !hiddenMailTo.Any())) throw new Exception("Не указаны получатели письма!");

            if (mailFrom == null || String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            mail.From = mailFrom;

            client.EnableSsl = false;

            if (mailTo != null)
            {
                foreach (MailAddress mailAddress in mailTo)
                {
                    mail.To.Add(mailAddress);
                }
            }
            if (hiddenMailTo != null)
            {
                foreach (MailAddress mailAddress in hiddenMailTo)
                {
                    mail.Bcc.Add(mailAddress);
                }
            }

            if (isTest)
            {
                mail.To.Clear();
                mail.CC.Clear();
                mail.Bcc.Clear();
                mail.Bcc.Add(new MailAddress("*****@*****.**"));
                //mail.Bcc.Add(new MailAddress("*****@*****.**"));
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;
            client.Host = "ums-1";
            client.Send(mail);
        }
Esempio n. 3
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo = null, MailAddress mailFrom = null, AttachmentFile file = null, bool isTest = false)
        {
            if (!mailTo.Any() && (hiddenMailTo == null || !hiddenMailTo.Any())) throw new Exception("Не указаны получатели письма!");

            if (mailFrom == null || String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            mail.From = mailFrom;

            client.EnableSsl = false;

            if (ConfigurationManager.AppSettings["Environment"].Equals("Production") && !isTest)
            {
                if (mailTo != null)
                {
                    foreach (MailAddress mailAddress in mailTo)
                    {
                        if (String.IsNullOrEmpty(mailAddress.Address)) continue;
                        mail.To.Add(mailAddress);
                    }
                }
                if (hiddenMailTo != null)
                {
                    foreach (MailAddress mailAddress in hiddenMailTo)
                    {
                        if (String.IsNullOrEmpty(mailAddress.Address)) continue;
                        mail.CC.Add(mailAddress);
                    }
                }
            }
            else
            {
                string[] testMails = ConfigurationManager.AppSettings["Emails4Test"].Split('|');
                foreach (var email in testMails)
                {
                    if (String.IsNullOrEmpty(email)) continue;
                    mail.To.Add(email);
                }

                body += "\r\n";
                if (mailTo != null)
                {
                    foreach (var mailAddress in mailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
                //Hidden recipients
                if (hiddenMailTo != null)
                {
                    foreach (var mailAddress in hiddenMailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;

            client.Host = "ums-1";

            if (file != null && file.Data.Length > 0)
            {
                MemoryStream stream = new MemoryStream(file.Data);
                Attachment attachment = new Attachment(stream, file.FileName, file.DataMimeType);
                mail.Attachments.Add(attachment);
            }

            try
            {
                client.Send(mail);
            }
            catch (Exception ex)
            {

                throw new Exception(String.Format("Сообщение не было отправлено. Текст ошибки - {0}", ex.Message));
            }
        }
Esempio n. 4
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo, MailAddress mailFrom, AttachmentFile file = null,string login = null, string pass = null, bool isTest = false)
        {
            if (IsTest) isTest = true;

            if ((mailTo == null || !mailTo.Any()) && (hiddenMailTo == null || !hiddenMailTo.Any())) throw new Exception("Не указаны получатели письма!");

            if (mailFrom == null || String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient("smtp.office365.com", 587);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            if (String.IsNullOrEmpty(login))
            {
                login = "******";
                pass = "******";
            }

            client.Credentials = new NetworkCredential(login, pass);

            mail.From = new MailAddress(login);

            if (mailTo != null)
            {
                foreach (MailAddress mailAddress in mailTo)
                {
                    mail.To.Add(mailAddress);
                }
            }
            if (hiddenMailTo != null)
            {
                foreach (MailAddress mailAddress in hiddenMailTo)
                {
                    mail.Bcc.Add(mailAddress);
                }
            }

            if (isTest)
            {
                mail.To.Clear();
                mail.CC.Clear();
                mail.Bcc.Clear();
                //mail.Bcc.Add(new MailAddress("*****@*****.**"));
                string[] testMails = ConfigurationManager.AppSettings["TestMailTo"].Split('|');
                foreach (string testMail in testMails)
                {
                    mail.Bcc.Add(new MailAddress(testMail));
                }

                //mail.Bcc.Add(new MailAddress("*****@*****.**"));
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;

            if (file != null && file.Data.Length > 0)
            {
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    ms.Read(file.Data, 0, file.Data.Length);

                MemoryStream stream = new MemoryStream(file.Data);

                Attachment attachment = new Attachment(stream, file.FileName, file.DataMimeType);
                    mail.Attachments.Add(attachment);

                    //client.Send(mail);
                //}

                //Attachment attachment = new Attachment(file.FileName, MediaTypeNames.Application.Octet);
                //ContentDisposition disposition = attachment.ContentDisposition;
                //disposition.CreationDate = File.GetCreationTime(file.FileName);
                //disposition.ModificationDate = File.GetLastWriteTime(file.FileName);
                //disposition.ReadDate = File.GetLastAccessTime(file.FileName);
                //disposition.FileName = Path.GetFileName(file.FileName);
                //disposition.Size = new FileInfo(file.FileName).Length;
                //disposition.DispositionType = DispositionTypeNames.Attachment;
                //mail.Attachments.Add(attachment);
            }
            //else
            //{
                client.Send(mail);
            //}
        }
Esempio n. 5
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo = null, MailAddress mailFrom = null, AttachmentFile file = null, bool isTest = false)
        {
            if (!mailTo.Any() && (hiddenMailTo == null || !hiddenMailTo.Any())) throw new Exception("Не указаны получатели письма!");

            if (mailFrom == null || String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient("smtp.office365.com",587);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("*****@*****.**", "pRgvD7TL");

            mail.From = new MailAddress("*****@*****.**", String.Empty, System.Text.Encoding.UTF8);

            //client.EnableSsl = false;

            if (ConfigurationManager.AppSettings["Environment"].Equals("Production") && !isTest)
            {
                if (mailTo != null)
                {
                    foreach (MailAddress mailAddress in mailTo)
                    {
                        if (String.IsNullOrEmpty(mailAddress.Address)) continue;
                        mail.To.Add(mailAddress);
                    }
                }
                if (hiddenMailTo != null)
                {
                    foreach (MailAddress mailAddress in hiddenMailTo)
                    {
                        if (String.IsNullOrEmpty(mailAddress.Address)) continue;
                        mail.CC.Add(mailAddress);
                    }
                }
            }
            else
            {
                string[] testMails = ConfigurationManager.AppSettings["Emails4Test"].Split('|');
                foreach (var email in testMails)
                {
                    if (String.IsNullOrEmpty(email)) continue;
                    mail.To.Add(email);
                }

                body += "\r\n";
                if (mailTo != null)
                {
                    foreach (var mailAddress in mailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
                //Hidden recipients
                if (hiddenMailTo != null)
                {
                    foreach (var mailAddress in hiddenMailTo)
                    {
                        body += "\r\n" + mailAddress.Address;
                    }
                }
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;
            client.SendCompleted += new SendCompletedEventHandler(SendCompletedCallback);

            if (file != null && file.Data.Length > 0)
            {
                MemoryStream stream = new MemoryStream(file.Data);
                Attachment attachment = new Attachment(stream, file.FileName, file.DataMimeType);
                mail.Attachments.Add(attachment);
            }

            try
            {
                client.SendAsync(mail, mail);
            }
            catch (Exception ex)
            {

                throw new Exception(String.Format("Сообщение не было отправлено. Текст ошибки - {0}", ex.Message));
            }
        }
Esempio n. 6
0
        public static void SendMailSmtp(string subject, string body, bool isBodyHtml, MailAddress[] mailTo, MailAddress[] hiddenMailTo, MailAddress mailFrom, AttachmentFile file = null, bool isTest = false)
        {
            if ((mailTo == null || !mailTo.Any()) && (hiddenMailTo == null || !hiddenMailTo.Any())) throw new Exception("Не указаны получатели письма!");

            if (mailFrom == null || String.IsNullOrEmpty(mailFrom.Address)) mailFrom = defaultMailFrom;

            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient();
            client.Port = 25;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            mail.From = mailFrom;

            client.EnableSsl = false;

            if (mailTo != null)
            {
                foreach (MailAddress mailAddress in mailTo)
                {
                    mail.To.Add(mailAddress);
                }
            }
            if (hiddenMailTo != null)
            {
                foreach (MailAddress mailAddress in hiddenMailTo)
                {
                    mail.Bcc.Add(mailAddress);
                }
            }

            if (isTest)
            {
                mail.To.Clear();
                mail.CC.Clear();
                mail.Bcc.Clear();
                mail.Bcc.Add(new MailAddress("*****@*****.**"));
                //mail.Bcc.Add(new MailAddress("*****@*****.**"));
            }

            mail.Subject = subject;
            mail.Body = body;
            mail.IsBodyHtml = isBodyHtml;
            client.Host = "ums-1";

            if (file != null && file.Data.Length > 0)
            {
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    ms.Read(file.Data, 0, file.Data.Length);

                MemoryStream stream = new MemoryStream(file.Data);

                Attachment attachment = new Attachment(stream, file.FileName, file.DataMimeType);
                    mail.Attachments.Add(attachment);

                    //client.Send(mail);
                //}

                //Attachment attachment = new Attachment(file.FileName, MediaTypeNames.Application.Octet);
                //ContentDisposition disposition = attachment.ContentDisposition;
                //disposition.CreationDate = File.GetCreationTime(file.FileName);
                //disposition.ModificationDate = File.GetLastWriteTime(file.FileName);
                //disposition.ReadDate = File.GetLastAccessTime(file.FileName);
                //disposition.FileName = Path.GetFileName(file.FileName);
                //disposition.Size = new FileInfo(file.FileName).Length;
                //disposition.DispositionType = DispositionTypeNames.Attachment;
                //mail.Attachments.Add(attachment);
            }
            //else
            //{
                client.Send(mail);
            //}
        }