Ejemplo n.º 1
0
        public Message ToMimeMessage(int tenant, string user, bool load_attachments)
        {
            var mime_message = new Message
            {
                Date = DateTime.Now,
                From = new Address(From, string.IsNullOrEmpty(DisplayName) ? "" : Codec.RFC2047Encode(DisplayName))
            };

            if (Important)
            {
                mime_message.Priority = MessagePriority.High;
            }

            mime_message.To.AddRange(To.ConvertAll(address =>
            {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mime_message.Cc.AddRange(Cc.ConvertAll(address =>
            {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mime_message.Bcc.AddRange(Bcc.ConvertAll(address =>
            {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mime_message.Subject = Codec.RFC2047Encode(Subject);

            // Set correct body
            if (Attachments.Any() || AttachmentsEmbedded.Any())
            {
                foreach (var attachment in Attachments)
                {
                    attachment.user   = user;
                    attachment.tenant = tenant;
                    var attach = CreateAttachment(attachment, load_attachments);
                    if (attach != null)
                    {
                        mime_message.Attachments.Add(attach);
                    }
                }

                foreach (var embedded_attachment in AttachmentsEmbedded)
                {
                    embedded_attachment.user   = user;
                    embedded_attachment.tenant = tenant;
                    var attach = CreateAttachment(embedded_attachment, true);
                    if (attach != null)
                    {
                        mime_message.EmbeddedObjects.Add(attach);
                    }
                }
            }

            mime_message.BodyText.Charset = Encoding.UTF8.HeaderName;
            mime_message.BodyText.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable;
            mime_message.BodyText.Text = "";

            mime_message.BodyHtml.Charset = Encoding.UTF8.HeaderName;
            mime_message.BodyHtml.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable;
            mime_message.BodyHtml.Text = HtmlBody;

            return(mime_message);
        }
Ejemplo n.º 2
0
        public Message CreateMimeMessage(int tennantid, string userid, bool loadAttachments)
        {
            List <MailAttachment> internalAttachments = new List <MailAttachment>();

            PreprocessHtml(tennantid, internalAttachments);

            var mailMessage = new Message()
            {
                Date     = DateTime.Now,
                Priority = this.Important ? ActiveUp.Net.Mail.MessagePriority.High : ActiveUp.Net.Mail.MessagePriority.Normal
            };

            mailMessage.From = new Address(From, string.IsNullOrEmpty(DisplayName) ? "" : Codec.RFC2047Encode(DisplayName));

            mailMessage.To.AddRange(To.ConvertAll(address => {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mailMessage.Cc.AddRange(Cc.ConvertAll(address =>
            {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mailMessage.Bcc.AddRange(Bcc.ConvertAll(address =>
            {
                var addr  = Parser.ParseAddress(address);
                addr.Name = string.IsNullOrEmpty(addr.Name) ? "" : Codec.RFC2047Encode(addr.Name);
                return(new Address(addr.Email, addr.Name));
            }));

            mailMessage.Subject = Codec.RFC2047Encode(Subject);

            // Set correct body
            if (Attachments.Count != 0 || internalAttachments.Count != 0)
            {
                foreach (MailAttachment attachment in Attachments)
                {
                    var attachPath = attachment;

                    var attach = CreateAttachment(tennantid, attachment, loadAttachments);

                    if (attach != null)
                    {
                        mailMessage.Attachments.Add(attach);
                    }
                }

                foreach (MailAttachment internalAttachment in internalAttachments)
                {
                    var attach = CreateAttachment(tennantid, internalAttachment, true);

                    if (attach != null)
                    {
                        mailMessage.EmbeddedObjects.Add(attach);
                    }
                }
            }

            mailMessage.BodyText.Charset = Encoding.UTF8.HeaderName;
            mailMessage.BodyText.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable;
            mailMessage.BodyText.Text = "";

            mailMessage.BodyHtml.Charset = Encoding.UTF8.HeaderName;
            mailMessage.BodyHtml.ContentTransferEncoding = ContentTransferEncoding.QuotedPrintable;
            mailMessage.BodyHtml.Text = HtmlBody;

            return(mailMessage);
        }