Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.13 KB | None | 0 0
  1. public void SendInstance(EmailSchedule emailSchedule)
  2.         {
  3.             var message = new MailMessage();
  4.  
  5.             MemoryStream attachmentStream = null;
  6.             try
  7.             {
  8.                 var mailAddress =
  9.                     new MailAddress(string.IsNullOrEmpty(emailSchedule.EmailAddressFrom)
  10.                         ? ConfigurationManager.AppSettings["DefaultSender"]
  11.                         : emailSchedule.EmailAddressFrom
  12.                         , emailSchedule.FromField);
  13.                 message.From = mailAddress;
  14.                 message.Sender = mailAddress;
  15.  
  16.                 if (!String.IsNullOrEmpty(emailSchedule.ReplyEmailAddress))
  17.                 {
  18.                     mailAddress = new MailAddress(emailSchedule.ReplyEmailAddress, emailSchedule.FromField);
  19.                     message.ReplyToList.Add(mailAddress);
  20.                 }
  21.                 string[] emailAdressesToList = emailSchedule.EmailAddressTo.Split(',');
  22.                 foreach (var item in emailAdressesToList)
  23.                 {
  24.                     if (!string.IsNullOrEmpty(item))
  25.                     {
  26.                         message.To.Add(new MailAddress(item));
  27.                     }
  28.                 }
  29.                 message.Subject = emailSchedule.Subject;
  30.                 message.Body = emailSchedule.EmailBody;
  31.                 message.BodyEncoding = Encoding.UTF8;
  32.                 message.IsBodyHtml = true;
  33.                 if (emailSchedule.AttachmentContent != null && emailSchedule.AttachmentContent.Length > 0)
  34.                 {
  35.                     // Create a memory stream
  36.                     attachmentStream = new MemoryStream();
  37.                     {
  38.                         attachmentStream.Write(emailSchedule.AttachmentContent, 0, emailSchedule.AttachmentContent.Length);
  39.  
  40.                         // Set the position to the beginning of the stream.
  41.                         attachmentStream.Seek(0, SeekOrigin.Begin);
  42.  
  43.                         // Create attachment
  44.                         ContentType contentType = new ContentType();
  45.                         contentType.MediaType = emailSchedule.AttachmentMediaType; // MediaTypeNames.Application.Octet;
  46.                          
  47.                         if (contentType.MediaType == "application/zip")
  48.                         {
  49.                         }
  50.                         contentType.Name = emailSchedule.AttachmentName;
  51.                         Attachment attachment = new Attachment(attachmentStream, contentType);
  52.                        
  53.                         // Add the attachment
  54.                         message.Attachments.Add(attachment);
  55.                     }
  56.                 }
  57.                 message.Send(emailSchedule);
  58.             }
  59.             catch (Exception ex)
  60.             {
  61.                 emailSchedule.EmailSentStatusValue = (short) EmailSentStatusEnumerator.Failed;
  62.                 TimerLog.LogEntry(ex.Message, EventLogEntryType.Error);
  63.             }
  64.             finally
  65.             {
  66.                 if (attachmentStream != null)
  67.                 {
  68.                     attachmentStream.Dispose();
  69.                 }
  70.             }
  71.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement