using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using Swarmops.Logic.Support;
using Swarmops.Logic.Swarm;
namespace Swarmops.Utility.Mail
{
public class NewsletterTransmitter2
{
private static QuotedPrintable qpUTF8 = new QuotedPrintable (Encoding.UTF8);
private static QuotedPrintable qp8859 = new QuotedPrintable (Encoding.GetEncoding ("ISO-8859-1"));
private readonly string htmlTemplateFile;
private readonly People sendList;
private readonly string textTemplateFile;
private readonly string title;
public NewsletterTransmitter2 (string title, string htmlTemplateFile, string textTemplateFile, People sendList)
{
this.title = title;
this.htmlTemplateFile = htmlTemplateFile;
this.textTemplateFile = textTemplateFile;
this.sendList = sendList;
}
public NewsletterTransmitter2()
{
// This constructor will basically make a nonfunctioning instance, since the fields are readonly
}
public void Send()
{
int indexLastBackslash = this.htmlTemplateFile.LastIndexOf ('\\');
string directory = this.htmlTemplateFile.Substring (0, indexLastBackslash);
string nakedFileName = this.htmlTemplateFile.Substring (indexLastBackslash + 1);
string htmlTemplate = "Failed to read HTML mail template.";
string textTemplate = "Failed to read plaintext mail template.";
string intro = "Failed to read intro.";
string mailBody = "Failed to read mail body.";
using (StreamReader reader = new StreamReader (this.htmlTemplateFile, Encoding.Default))
{
htmlTemplate = reader.ReadToEnd();
}
using (StreamReader reader = new StreamReader (this.textTemplateFile, Encoding.Default))
{
textTemplate = reader.ReadToEnd();
}
using (StreamReader reader = new StreamReader (directory + "\\" + "intro.txt", Encoding.Default))
{
intro = reader.ReadToEnd();
}
using (StreamReader reader = new StreamReader (directory + "\\" + "body.txt", Encoding.Default))
{
mailBody = reader.ReadToEnd();
}
// PREPARE HTML VIEW:
// Write in the title, intro, and body
htmlTemplate = htmlTemplate.
Replace ("%TITLE%", HttpUtility.HtmlEncode (this.title.ToUpper())).
Replace ("%title%", HttpUtility.HtmlEncode (this.title)).
Replace ("%intro%", HtmlEncode (intro)).
Replace ("%body%", HtmlEncode (mailBody));
// Embed any inline images directly into the message
string newsletterIdentifier = DateTime.Now.ToString ("yyyyMMddhhMMssfff") + "@piratpartiet.se";
htmlTemplate = htmlTemplate.Replace ("Header-PP-Logo.png", "cid:pplogo" + newsletterIdentifier);
/*
htmlTemplate = htmlTemplate.Replace("TopRight.png", "cid:topright" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("MiddleRight.png", "cid:middleright" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("BottomRight.png", "cid:bottomright" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("TopLeft.png", "cid:topleft" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("MiddleLeft.png", "cid:middleleft" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("BottomLeft.png", "cid:bottomleft" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("TopMiddle.png", "cid:topmiddle" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("BottomMiddle.png", "cid:bottommiddle" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("PPShield.png", "cid:ppshield" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("Rick.png", "cid:rick" + newsletterIdentifier);
htmlTemplate = htmlTemplate.Replace("RFe-signature.gif", "cid:rick-signature" + newsletterIdentifier);*/
MemoryStream memStream = new MemoryStream();
AlternateView htmlView = AlternateView.CreateAlternateViewFromString (htmlTemplate,
new ContentType (
MediaTypeNames.Text.Html));
LinkedResource image = new LinkedResource (directory + "\\" + "Header-PP-Logo.png");
image.ContentId = "pplogo" + newsletterIdentifier;
htmlView.LinkedResources.Add (image);
/*
image = new LinkedResource(directory + "\\" + "MiddleRight.png");
image.ContentId = "middleright" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "BottomRight.png");
image.ContentId = "bottomright" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "TopLeft.png");
image.ContentId = "topleft" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "MiddleLeft.png");
image.ContentId = "middleleft" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "BottomLeft.png");
image.ContentId = "bottomleft" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "TopMiddle.png");
image.ContentId = "topmiddle" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "BottomMiddle.png");
image.ContentId = "bottommiddle" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "PPShield.png");
image.ContentId = "ppshield" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "Rick.png");
image.ContentId = "rick" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);
image = new LinkedResource(directory + "\\" + "RFe-Signature.gif");
image.ContentId = "rick-signature" + newsletterIdentifier;
htmlView.LinkedResources.Add(image);*/
// PREPARE FORUM FILE:
string forumTemplate = textTemplate.
Replace ("%TITLE%", "[h1]" + this.title.ToUpper() + "[/h1]").
Replace ("%title%", "[h1]" + this.title + "[/h1]").
Replace ("%intro%", intro).
Replace ("%body%", mailBody);
// Replace "Text" with "Text (http://link)"
Regex regexLinksForum =
new Regex ("(?s)\\[a\\s+href=\\\"(?[^\\\"]+)\\\"\\](?
"). Replace ("[", "<"). Replace ("]", ">"). Replace (""", "\""). Replace ("&", "&"); } } }