private void AttachFiles(MimeMessage mimeMessage, Guid[] attachIds) { if (attachIds != null && attachIds.Any()) { try { foreach (Guid id in attachIds) { var file = _tempFilesService.GetFile(id); if (file != null) { var tempFileMemoryStream = new MemoryStream(file.Content); // create an image attachment for the file located at path var attachment = new MimePart(file.MimeType) { Content = new MimeContent(tempFileMemoryStream, ContentEncoding.Default), ContentDisposition = new MimeKit.ContentDisposition(MimeKit.ContentDisposition.Attachment), ContentTransferEncoding = ContentEncoding.Default, // <= was base64 FileName = file.FileName }; // now create the multipart/mixed container to hold the message text and the // image attachment var multipart = new Multipart("mixed"); multipart.Add(mimeMessage.Body); multipart.Add(attachment); // now set the multipart/mixed as the message body mimeMessage.Body = multipart; _tempFilesService.RemoveFile(id); } } } catch (Exception e) { _logService.LogError(e); } } }
public MailService(ITempFilesService tempFilesService, IRazorEngineService razorEngineService, ILogService logService, ISettingsService settingsService) { _tempFilesService = tempFilesService; _razorEngineService = razorEngineService; _logService = logService; _settingsService = settingsService; Mapper.CreateMap <RoCMS.Web.Contract.Models.MailMsg, RoCMS.Data.Models.Mail>() .ForMember(x => x.MailId, x => x.Ignore()) .ForMember(x => x.CreationDate, x => x.Ignore()) .ForMember(x => x.ErrorMessage, x => x.Ignore()) .ForMember(x => x.Sent, x => x.Ignore()) .ForMember(x => x.Attaches, x => x.ResolveUsing(s => { string result = ""; var attachIds = s.AttachIds; if (attachIds != null && attachIds.Any()) { foreach (Guid id in attachIds) { try { var file = _tempFilesService.GetFile(id); if (file != null) { if (result.Length > 0) { result += ","; } result += file.FileName; } } catch (Exception e) { _logService.LogError(e); } } } return(result); })); Mapper.AssertConfigurationIsValid(); }