Ejemplo n.º 1
0
        private IMessageAttachmentsCollectionPage GetAttachments(MList <EmailAttachmentEmbedded> attachments)
        {
            var result = new MessageAttachmentsCollectionPage();

            foreach (var a in attachments)
            {
                result.Add(new FileAttachment
                {
                    ContentId    = a.ContentId,
                    Name         = a.File.FileName,
                    ContentType  = MimeMapping.GetMimeType(a.File.FileName),
                    ContentBytes = a.File.GetByteArray(),
                });
            }
            return(result);
        }
        protected virtual MailMessage CreateMailMessage(EmailMessageEntity email)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage()
            {
                From       = email.From.ToMailAddress(),
                Subject    = email.Subject,
                IsBodyHtml = email.IsBodyHtml,
            };

            System.Net.Mail.AlternateView view = System.Net.Mail.AlternateView.CreateAlternateViewFromString(email.Body.Text !, null, email.IsBodyHtml ? "text/html" : "text/plain");
            view.LinkedResources.AddRange(email.Attachments
                                          .Where(a => a.Type == EmailAttachmentType.LinkedResource)
                                          .Select(a => new System.Net.Mail.LinkedResource(a.File.OpenRead(), MimeMapping.GetMimeType(a.File.FileName))
            {
                ContentId = a.ContentId,
            }));
            message.AlternateViews.Add(view);

            message.Attachments.AddRange(email.Attachments
                                         .Where(a => a.Type == EmailAttachmentType.Attachment)
                                         .Select(a => new System.Net.Mail.Attachment(a.File.OpenRead(), MimeMapping.GetMimeType(a.File.FileName))
            {
                ContentId = a.ContentId,
                Name      = a.File.FileName,
            }));


            message.To.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.To).Select(r => r.ToMailAddress()).ToList());
            message.CC.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Cc).Select(r => r.ToMailAddress()).ToList());
            message.Bcc.AddRange(email.Recipients.Where(r => r.Kind == EmailRecipientKind.Bcc).Select(r => r.ToMailAddress()).ToList());

            return(message);
        }