/// <summary> /// Creates a MailMessage for the current MailAttribute instance. /// </summary> protected EmailMessage GenerateProspectiveMailMessage(MailAttributes mail) { //create base message var message = new EmailMessage { from_name = mail.From.DisplayName, from_email = mail.From.Address, to = mail.To.Union(mail.Cc).Select(t => new EmailAddress(t.Address, t.DisplayName)), bcc_address = mail.Bcc.Any() ? mail.Bcc.First().Address : null, subject = mail.Subject, important = mail.Priority == MailPriority.High ? true : false }; // We need to set Reply-To as a custom header if (mail.ReplyTo.Any()) { message.AddHeader("Reply-To", string.Join(" , ", mail.ReplyTo)); } // Adding content to the message foreach (var view in mail.AlternateViews) { var reader = new StreamReader(view.ContentStream, Encoding.UTF8, true, 1024, true); var body = reader.ReadToEnd(); if (view.ContentType.MediaType == MediaTypeNames.Text.Plain) { message.text = body; } if (view.ContentType.MediaType == MediaTypeNames.Text.Html) { message.html = body; } } // Going through headers and adding them to the message mail.Headers.ToList().ForEach(h => message.AddHeader(h.Key, h.Value)); // Adding the attachments var attachments = new List<email_attachment>(); foreach (var mailAttachment in mail.Attachments.Select(attachment => Utils.AttachmentCollection.ModifyAttachmentProperties(attachment.Key, attachment.Value, false))) { using (var stream = new MemoryStream()) { mailAttachment.ContentStream.CopyTo(stream); var base64Data = Convert.ToBase64String(stream.ToArray()); attachments.Add(new email_attachment { content = base64Data, name = mailAttachment.Name, type = mailAttachment.ContentType.MediaType, }); } } message.attachments = attachments; return message; }
//public async Task<List<EmailResult>> SendAsncTask(MailMessage mail) //{ // var message = ToMandrillMessage(mail); // return await _mandrillClient.SendMessageAsync(message); //} /// <summary> /// Converts a .NET MailMessage to the required API proxy class /// </summary> /// <param name="mail"></param> /// <returns></returns> private static EmailMessage ToMandrillMessage(MailMessage mail) { if (mail.CC.Any()) throw new NotSupportedException("The CC field is not supported with the MandrillMailSender"); if (mail.ReplyToList.Any()) throw new NotSupportedException("The ReplyTo field is not supported with the MandrillMailSender"); //create base message var message = new EmailMessage { from_name = mail.From.Address, from_email = mail.From.DisplayName, to = mail.To.Select(t => new EmailAddress(t.Address, t.DisplayName)), bcc_address = mail.Bcc.Any() ? mail.Bcc.First().Address : null, subject = mail.Subject, }; //add headers for (int i = 0; i < mail.Headers.Count; i++) { message.AddHeader(mail.Headers.Keys[i], mail.Headers[i]); } //add content foreach (var view in mail.AlternateViews) { using (var reader = new StreamReader(view.ContentStream)) { var body = reader.ReadToEnd(); if (view.ContentType.MediaType == MediaTypeNames.Text.Plain) message.text = body; if (view.ContentType.MediaType == MediaTypeNames.Text.Html) message.html = body; } } //add attachments var atts = new List<attachment>(); foreach (var attachment in mail.Attachments) { using (var stream = new MemoryStream()) { attachment.ContentStream.CopyTo(stream); var base64Data = Convert.ToBase64String(stream.ToArray()); atts.Add(new attachment { content = base64Data, name = attachment.ContentId, type = attachment.ContentType.MediaType, }); } } message.attachments = atts; return message; }
/// <summary> /// Creates a MailMessage for the current MailAttribute instance. /// </summary> protected EmailMessage GenerateProspectiveMailMessage(MailAttributes mail) { var idnmapping = new IdnMapping(); var emailAddresses = mail.To .Select( t => { var domainSplit = t.Address.Split('@'); return new EmailAddress(domainSplit[0] + "@" + idnmapping.GetAscii(domainSplit[1])) { type = "to" }; }) .Union( mail.Cc.Select( t => { var domainSplit = t.Address.Split('@'); return new EmailAddress(domainSplit[0] + "@" + idnmapping.GetAscii(domainSplit[1])) { type = "cc" }; })) .Union( mail.Bcc.Select( t => { var domainSplit = t.Address.Split('@'); return new EmailAddress(domainSplit[0] + "@" + idnmapping.GetAscii(domainSplit[1])) { type = "bcc" }; })); //create base message var message = new EmailMessage { from_name = mail.From.DisplayName, from_email = mail.From.Address, to = emailAddresses, subject = mail.Subject, important = mail.Priority == MailPriority.High, preserve_recipients = true }; // We need to set Reply-To as a custom header if (mail.ReplyTo.Any()) { message.AddHeader("Reply-To", string.Join(" , ", mail.ReplyTo)); } // Adding content to the message foreach (var view in mail.AlternateViews) { var reader = new StreamReader(view.ContentStream, Encoding.UTF8, true, 1024, true); var body = reader.ReadToEnd(); if (view.ContentType.MediaType == MediaTypeNames.Text.Plain) { message.text = body; } if (view.ContentType.MediaType == MediaTypeNames.Text.Html) { message.html = body; } } // Going through headers and adding them to the message mail.Headers.ToList().ForEach(h => message.AddHeader(h.Key, h.Value)); // Adding the attachments var attachments = new List<email_attachment>(); foreach (var mailAttachment in mail.Attachments.Select(attachment => Utils.AttachmentCollection.ModifyAttachmentProperties(attachment.Key, attachment.Value, false))) { using (var stream = new MemoryStream()) { mailAttachment.ContentStream.CopyTo(stream); var base64Data = Convert.ToBase64String(stream.ToArray()); attachments.Add(new email_attachment { content = base64Data, name = ReplaceGermanCharacters(mailAttachment.Name), type = mailAttachment.ContentType.MediaType, }); } } message.attachments = attachments; return message; }
/// <summary> /// Creates a EmailMessage for the given IMailAttributes instance. /// </summary> public EmailMessage GenerateProspectiveMailMessage() { var mail = this; if (mail.Cc.Any()) throw new NotSupportedException("The CC field is not supported with the MandrillMailSender"); if (mail.ReplyTo.Any()) throw new NotSupportedException("The ReplyTo field is not supported with the MandrillMailSender"); //create base message var message = new EmailMessage { from_name = mail.From.DisplayName, from_email = mail.From.Address, to = mail.To.Select(t => new EmailAddress(t.Address, t.DisplayName)), bcc_address = mail.Bcc.Any() ? mail.Bcc.First().Address : null, subject = mail.Subject }; //add headers foreach (var kvp in mail.Headers) message.AddHeader(kvp.Key, kvp.Value); //add content foreach (AlternateView view in mail.AlternateViews) { using (var reader = new StreamReader(view.ContentStream)) { var body = reader.ReadToEnd(); if (view.ContentType.MediaType == MediaTypeNames.Text.Plain) message.text = body; if (view.ContentType.MediaType == MediaTypeNames.Text.Html) message.html = body; } } //add attachments var atts = new List<attachment>(); foreach (var mailAttachment in mail.Attachments.Select(attachment => AttachmentCollection.ModifyAttachmentProperties(attachment.Key, attachment.Value, false))) { using (var stream = new MemoryStream()) { mailAttachment.ContentStream.CopyTo(stream); string base64Data = Convert.ToBase64String(stream.ToArray()); atts.Add(new attachment { content = base64Data, name = mailAttachment.Name, type = mailAttachment.ContentType.MediaType, }); } } message.attachments = atts; return message; }