コード例 #1
0
ファイル: MailSender.cs プロジェクト: shahadat-sust/alrd
        private void Send(Mail mail)
        {
            SmtpClient client = new SmtpClient();

            //client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory;
            try
            {
                MailMessage message = new MailMessage();

                if (!string.IsNullOrEmpty(mail.FromEmail))
                {
                    message.From = new MailAddress(mail.FromEmail.Trim());
                }

                if (mail.BCCAddress != null)
                {
                    foreach (var item in mail.BCCAddress)
                    {
                        message.Bcc.Add(new MailAddress(item));
                    }
                }

                if (mail.CCAddress != null)
                {
                    foreach (var item in mail.CCAddress)
                    {
                        message.CC.Add(new MailAddress(item));
                    }
                }
                if (mail.ToAddress != null)
                {
                    foreach (var item in mail.ToAddress)
                    {
                        message.To.Add(new MailAddress(item));
                    }
                }
                if (mail.AttachmantFileName != null)
                {
                    foreach (string item in mail.AttachmantFileName)
                    {
                        var memStream = new MemoryStream(File.ReadAllBytes(item));
                        message.Attachments.Add(new Attachment(memStream, item.Split('\\')[item.Split('\\').Length - 1]));
                    }
                }
                message.Subject = mail.Subject;
                message.Body    = mail.Body;
                if (mail.IsHtml)
                {
                    message.IsBodyHtml = true;
                }
                message.BodyEncoding = UTF8Encoding.UTF8;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;



                client.Send(message);
                _MailReport.Add(new MailReport(mail, true));
            }
            catch (SmtpException ex)
            {
                _MailReport.Add(new MailReport(mail, false, ex));
            }
            catch (Exception ex)
            {
                _MailReport.Add(new MailReport(mail, false, ex));
            }
            finally
            {
                //client.Dispose();
                if (_DoDeleteAttacmentFiles)
                {
                    DeleteAttachMentFiles(mail);
                }
            }
        }
コード例 #2
0
ファイル: MailSender.cs プロジェクト: shahadat-sust/alrd
 public MailReport(Mail sendMail, bool isSend, Exception ex = null)
 {
     this.SendMail = sendMail;
     this.IsSend   = isSend;
     this.Ex       = ex;
 }