public void Enqueue(MailMessage mail) { var from = new MailAddress(mail.Sender); var to = mail.Recipients.Select(r => new MailAddress(r)).ToArray(); var content = new MailContent(mail.Subject, from, mail.Html, mail.Text); foreach (var recipient in to) { content.AddRecipient(recipient); } var parsed = new Mail(from, to, content.ToString()); var sendableMail = new SendableMail(parsed, mail.Connector); _mailQueue.Enqueue(sendableMail, TimeSpan.Zero); }
public void TestToString() { var subject = "Test Subject with Ümläutß? and =_="; var from = new MailAddress("*****@*****.**", "Fu Bar äöü"); var html = "<p>This is the <b>HTML</b> content.</p>"; var text = "This is the plain text content. Mit Umlauten: äöüß! = _! ="; var mc = new MailContent(subject, from, html, text); mc.BodyEncoding = Encoding.GetEncoding("ISO-8859-1"); // Encoding.UTF8; mc.SubjectEncoding = Encoding.UTF8; mc.HeaderEncoding = Encoding.UTF8; for (int i = 0; i < 10; i++) { mc.AddRecipient("user" + i + "@test.de", "User " + i); } mc.AddAttachment("fu.txt", "Test Anhang", Encoding.ASCII, MediaTypeNames.Text.Plain); var s = mc.ToString(); Assert.Contains("Test Subject", s); }
private MailContent CreateContent(MailAddress from, MailAddress[] to, bool sendEicar) { var template = _mailTemplates.Get(_timeTable.MailTemplateId); var html = ReplaceTokens(template.Html, template.Title); var text = ReplaceTokens(template.Text, template.Title); var mc = new MailContent(template.Subject, from, html, text) { HeaderEncoding = GetEncoding(template.HeaderEncoding), SubjectEncoding = GetEncoding(template.HeaderEncoding), BodyEncoding = GetEncoding(template.BodyEncoding) }; if (to.Length > 0) { mc.AddRecipient(to.First()); if (to.Length > 1) { foreach (var cc in to.Skip(1)) { mc.AddCc(cc); } } } IAttachment attachment = null; if (_timeTable.AttachmentType == AttachmentType.Fixed) { attachment = _attachments.Get(_timeTable.AttachmentName); } else if (_timeTable.AttachmentType == AttachmentType.Random) { var attachments = _attachments.All().ToArray(); attachment = attachments[_random.Next(attachments.Length)]; } if (attachment != null) { mc.AddAttachment(attachment.Name, attachment.Content); } if (sendEicar) { if (_timeTable.ProtocolLevel == ProtocolLevel.Verbose) { Logger.InfoFormat("Timetable '{0}': next mail will be sent with an EICAR file.", _timeTable.Name); } mc.AddAttachment("eicar.html", EICAR, Encoding.ASCII, MediaTypeNames.Text.Html); } if (EmlFolder != null) { if (!File.Exists(EmlFolder)) { Directory.CreateDirectory(EmlFolder); } var name = _timeTable.Name + " " + DateTime.Now.ToString("s"); var safeName = Regex.Replace(name, "\\W+", "-"); using (var file = File.OpenWrite(Path.Combine(EmlFolder, safeName + ".eml"))) using (var writer = new StreamWriter(file)) { writer.Write(mc.ToString()); } } return(mc); }