Beispiel #1
0
        public static void SendItBudget()
        {
            HtmlToPdf converter = new HtmlToPdf();
            string url = String.Format("{0}/Report/ItBudgetView", stuffUrl);
            PdfDocument doc = converter.ConvertUrl(url);
            MemoryStream stream = new MemoryStream();
            doc.Save(stream);

            var file = new AttachmentFile() { Data = stream.ToArray(), FileName = "it-budget.pdf", DataMimeType = MediaTypeNames.Application.Pdf };
            //var recipients = new MailAddress[] {new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**"), new MailAddress("*****@*****.**") };
            var recipientsStr = ConfigurationManager.AppSettings["ItBudgetRecipients"].Split('|');
            //string monthName = new TranslitDate().GetMonthName(DateTime.Now.AddMonths(1).Month);
            var recipients = (from s in recipientsStr where !String.IsNullOrEmpty(s) select new MailAddress(s)).ToArray();
            string monthName = TranslitDate.GetMonthNameImenit(DateTime.Now.AddMonths(-1).Month);
            SendMailSmtp(String.Format("ИТ бюджет за {0} {1}", monthName, DateTime.Now.AddMonths(-1).Year), "Добрый день. ИТ бюджет во вложении", true, null, recipients, null, file);
        }
Beispiel #2
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);
            //}
        }
Beispiel #3
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);
            //}
        }