Exemplo n.º 1
0
        private MandrillEmailMessage LoadMandrillMessageFromMailMessage(MailMessage message)
        {
            MandrillEmailMessage mandrillMessage = new MandrillEmailMessage();

            mandrillMessage.subject = message.Subject;
            if (message.IsBodyHtml)
            {
                mandrillMessage.html = message.Body;
            }
            else
            {
                mandrillMessage.text = message.Body;
            }
            mandrillMessage.from_email = message.From.Address;
            mandrillMessage.from_name  = message.From.DisplayName;
            foreach (var recipient in message.To)
            {
                mandrillMessage.to.Add(new MandrillRecipient {
                    type = "to", email = recipient.Address, name = recipient.DisplayName
                });
            }
            foreach (var cc in message.CC)
            {
                mandrillMessage.to.Add(new MandrillRecipient {
                    type = "to", email = cc.Address, name = cc.DisplayName
                });
            }
            mandrillMessage.bcc_address = message.Bcc.Select(b => b.Address).FirstOrDefault();
            mandrillMessage.important   = message.Priority == MailPriority.High;
            mandrillMessage.track_opens = true;
            foreach (var attachment in message.Attachments)
            {
                var mandrillAttachment = new MandrillAttachment();
                mandrillAttachment.name    = attachment.Name;
                mandrillAttachment.type    = attachment.ContentType.MediaType;
                mandrillAttachment.content = Convert.ToBase64String(Utilities.ToByteArray(attachment.ContentStream));

                mandrillMessage.attachments.Add(mandrillAttachment);
            }

            return(mandrillMessage);
        }
 /// <summary>
 /// Send a email message using Madrill Api.
 /// </summary>
 /// <param name="message">A Mandrill message type.</param>
 public MandrillMessageStatus Send(MandrillEmailMessage message)
 {
     if (!TestConnectionWithServer())
     {
         throw new Exception("Without connection with Mandrill Server.");
     }
     try
     {
         MandrillSenderData senderData = new MandrillSenderData();
         senderData.key     = _key;
         senderData.message = message;
         senderData.async   = false;
         senderData.ip_pool = "Main Pool";
         senderData.send_at = DateTime.Now;
         return(Send(senderData));
     }
     catch (Exception ex)
     {
         MandrillMessageStatus messageStatus = new MandrillMessageStatus();
         messageStatus.MessageSent  = false;
         messageStatus.MessageError = ex.Message;
         return(messageStatus);
     }
 }