Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void SendInstance(EmailSchedule emailSchedule)
- {
- var message = new MailMessage();
- MemoryStream attachmentStream = null;
- try
- {
- var mailAddress =
- new MailAddress(string.IsNullOrEmpty(emailSchedule.EmailAddressFrom)
- ? ConfigurationManager.AppSettings["DefaultSender"]
- : emailSchedule.EmailAddressFrom
- , emailSchedule.FromField);
- message.From = mailAddress;
- message.Sender = mailAddress;
- if (!String.IsNullOrEmpty(emailSchedule.ReplyEmailAddress))
- {
- mailAddress = new MailAddress(emailSchedule.ReplyEmailAddress, emailSchedule.FromField);
- message.ReplyToList.Add(mailAddress);
- }
- string[] emailAdressesToList = emailSchedule.EmailAddressTo.Split(',');
- foreach (var item in emailAdressesToList)
- {
- if (!string.IsNullOrEmpty(item))
- {
- message.To.Add(new MailAddress(item));
- }
- }
- message.Subject = emailSchedule.Subject;
- message.Body = emailSchedule.EmailBody;
- message.BodyEncoding = Encoding.UTF8;
- message.IsBodyHtml = true;
- if (emailSchedule.AttachmentContent != null && emailSchedule.AttachmentContent.Length > 0)
- {
- // Create a memory stream
- attachmentStream = new MemoryStream();
- {
- attachmentStream.Write(emailSchedule.AttachmentContent, 0, emailSchedule.AttachmentContent.Length);
- // Set the position to the beginning of the stream.
- attachmentStream.Seek(0, SeekOrigin.Begin);
- // Create attachment
- ContentType contentType = new ContentType();
- contentType.MediaType = emailSchedule.AttachmentMediaType; // MediaTypeNames.Application.Octet;
- if (contentType.MediaType == "application/zip")
- {
- }
- contentType.Name = emailSchedule.AttachmentName;
- Attachment attachment = new Attachment(attachmentStream, contentType);
- // Add the attachment
- message.Attachments.Add(attachment);
- }
- }
- message.Send(emailSchedule);
- }
- catch (Exception ex)
- {
- emailSchedule.EmailSentStatusValue = (short) EmailSentStatusEnumerator.Failed;
- TimerLog.LogEntry(ex.Message, EventLogEntryType.Error);
- }
- finally
- {
- if (attachmentStream != null)
- {
- attachmentStream.Dispose();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement