/// <summary> /// Adds a file attachment with an inline image. /// </summary> /// <param name = "path">The full path to the file to attach</param> /// <param name = "contentType">The content type.</param> /// <param name = "contentId">ContentID for inline images</param> public void AddAttachment(string path, string contentType, string contentId) { ValidateAttachmentPath(path); using (var stream = File.OpenRead(path)) { var content = ReadStream(stream, 8067); ValidateAttachmentLength(Convert.ToBase64String(content)); var attachment = new PostmarkMessageAttachment { Name = new FileInfo(path).Name, ContentType = contentType, #if !WINDOWS_PHONE Content = Convert.ToBase64String(content, Base64FormattingOptions.InsertLineBreaks), #else Content = Convert.ToBase64String(content), #endif ContentId = contentId }; Attachments.Add(attachment); } }
/// <summary> /// Adds a file attachment. /// </summary> /// <param name = "path">The full path to the file to attach</param> /// <param name = "contentType">The content type.</param> public void AddAttachment(string path, string contentType) { ValidateAttachment(path); var stream = File.OpenRead(path); var content = ReadStream(stream, 8067); var attachment = new PostmarkMessageAttachment { Name = new FileInfo(path).Name, ContentType = contentType, Content = Convert.ToBase64String(content) }; Attachments.Add(attachment); }
/// <summary> /// Adds a file attachment using a byte[] array with inline support /// </summary> /// <param name = "path">The full path to the file to attach</param> /// <param name = "contentType">The content type.</param> /// <param name = "contentId">The ContentId for inline images.</param> public void AddAttachment(byte[] content, string contentType, string attachmentName, string contentId) { ValidateAttachmentLength(Convert.ToBase64String(content)); var attachment = new PostmarkMessageAttachment { Name = attachmentName, ContentType = contentType, #if !WINDOWS_PHONE Content = Convert.ToBase64String(content, Base64FormattingOptions.InsertLineBreaks), #else Content = Convert.ToBase64String(content), #endif ContentId = contentId }; Attachments.Add(attachment); }
/// <summary> /// Adds a file attachment stream with inline support. /// </summary> /// <param name = "contentStream">An opened stream of the file attachments.</param> /// <param name="attachmentName">The file name assocatiated with the attachment.</param> /// <param name = "contentType">The content type.</param> /// <param name="contentId">The ContentId for inlined images.</param> public void AddAttachment(Stream contentStream, string attachmentName, string contentType = "application/octet-stream", string contentId = null) { var content = ReadStream(contentStream, 8067); var payload = Convert.ToBase64String(content); var attachment = new PostmarkMessageAttachment { Name = attachmentName, ContentType = contentType, //TODO: split on 76 character bounds. Content = payload }; if (!String.IsNullOrWhiteSpace(contentId)) { attachment.ContentId = contentId; } Attachments.Add(attachment); }
/// <summary> /// Adds a file attachment. /// </summary> /// <param name = "path">The full path to the file to attach</param> /// <param name = "contentType">The content type.</param> public void AddAttachment(string path, string contentType) { ValidateAttachment(path); using(var stream = File.OpenRead(path)) { var content = ReadStream(stream, 8067); var attachment = new PostmarkMessageAttachment { Name = new FileInfo(path).Name, ContentType = contentType, Content = Convert.ToBase64String(content) }; Attachments.Add(attachment); } }
/// <summary> /// Adds a file attachment using a byte[] array /// </summary> /// <param name = "path">The full path to the file to attach</param> /// <param name = "contentType">The content type.</param> public void AddAttachment(byte[] content, string contentType, string attachmentName) { ValidateAttachmentLength(Convert.ToBase64String(content)); var attachment = new PostmarkMessageAttachment { Name = attachmentName, ContentType = contentType, Content = Convert.ToBase64String(content) }; Attachments.Add(attachment); }
public static PostmarkResponse SendEmail(string toAddress, string fromAddress, string subject, string body, string attachmentName = "", params File[] files) { string attachmentContent = null; if (files.Any()) { try { attachmentContent = ZipFiles(files); } catch (Exception ex) { ErrorSignal.FromCurrentContext().Raise(new ApplicationException("Failed to zip email attachments.", ex)); } } var message = new PostmarkMessage { From = ConfigurationManager.AppSettings["PostmarkSignature"], ReplyTo = fromAddress, To = toAddress, Subject = subject, TextBody = body }; if (attachmentContent != null) { var attachment = new PostmarkMessageAttachment { Name = attachmentName + ".zip", ContentType = "application/zip", Content = attachmentContent }; message.Attachments.Add(attachment); } var client = new PostmarkClient(ConfigurationManager.AppSettings["PostmarkApiKey"]); var result = client.SendMessage(message); if (result.Status != PostmarkStatus.Success) { ErrorSignal.FromCurrentContext().Raise(new ApplicationException(String.Format("Failed to send email to {0}.\nReason: {1}", toAddress, result.Message))); } return result; }