I'm looking for input, advice, criticism. This is my first helper class. While use of mailto URLs should most often be trivial, I have a real world need to send to multiple recipients and with a body of text something more than just a one liner. Doing so gets sticky and there are some pitfalls with reserved characters, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mailto
{
public class MailtoURL
{
public string Subject { get; set; }
private string _uri = "mailto:";
private string _toField = "?to=";
private string _ccField = "cc=";
private string _bccField = "bcc=";
private string _subjectField = "subject=";
private string _bodyField = "body=";
private string _seperator = "&";
private string _recipientDelimeter = ",";
private string _linebreak = "%0D%0A";
private string _replacePercent = "%25";
private string _replaceAmpersand = "%26";
private string _replaceSpace = "%20";
private string _replaceQuestionMark = "%3F";
private List<Recipient> Recipients;
private List<string> BodyText;
public MailtoURL()
{
Recipients = new List<Recipient>();
BodyText = new List<string>();
}
public string getMailtoUrl()
{
StringBuilder mailtoUrl = new StringBuilder();
mailtoUrl.Append(_uri)
.Append(RecipientString())
.Append(_seperator)
.Append(_subjectField)
.Append(Subject.Replace("%", _replacePercent).Replace("&", _replaceAmpersand).Replace(" ", _replaceSpace))
.Append(_seperator)
.Append(_bodyField)
.Append(BodyTextString().Replace(" ", _replaceSpace).Replace("&", _replaceAmpersand));
return mailtoUrl.ToString();
}
///<summary>Add an empty string for a new blank line</summary>
public void AddBodyText(string bodyText)
{
BodyText.Add(bodyText.Replace("%", _replacePercent) + _linebreak);
}
public void AddRecipient(string recipientAddress, RecipientTypes recipientType)
{
Recipient recipient = new Recipient();
recipient.RecipientAddress = recipientAddress.Replace("?", _replaceQuestionMark)
.Replace("%", _replacePercent);
recipient.RecipientType = recipientType;
Recipients.Add(recipient);
}
private string BodyTextString()
{
var bodytext = string.Empty;
foreach (var body in BodyText)
{
bodytext += body;
}
return bodytext;
}
private string RecipientString()
{
var recipientString = string.Empty;
if (Recipients.Any(r => r.RecipientType == RecipientTypes.To))
{
foreach (var recipient in from r in Recipients where r.RecipientType == RecipientTypes.To select r)
{
recipientString = _toField += recipient.RecipientAddress + _recipientDelimeter;
}
}
if (Recipients.Any(r => r.RecipientType == RecipientTypes.CC))
{
foreach (var recipient in from r in Recipients where r.RecipientType == RecipientTypes.CC select r)
{
recipientString = recipientString += _seperator + _ccField + recipient.RecipientAddress + _recipientDelimeter;
}
}
if (Recipients.Any(r => r.RecipientType == RecipientTypes.Bcc))
{
foreach (var recipient in from r in Recipients where r.RecipientType == RecipientTypes.Bcc select r)
{
recipientString = recipientString += _seperator + _bccField + recipient.RecipientAddress;
}
}
return recipientString;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mailto
{
public class Recipient
{
public string RecipientAddress { get; set; }
public RecipientTypes RecipientType { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace mailto
{
public enum RecipientTypes
{
To,
CC,
Bcc
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace mailto
{
class Program
{
static void Main(string[] args)
{
var mailtoUrl = new MailtoURL();
mailtoUrl.Subject = "This is my excellent &%? subject% & here is more text!";
mailtoUrl.AddRecipient("r%[email protected]", RecipientTypes.CC);
mailtoUrl.AddRecipient("[email protected]", RecipientTypes.To);
mailtoUrl.AddRecipient("[email protected]", RecipientTypes.To);
mailtoUrl.AddRecipient("[email protected]", RecipientTypes.To);
mailtoUrl.AddRecipient("[email protected]", RecipientTypes.CC);
mailtoUrl.AddRecipient("[email protected]", RecipientTypes.Bcc);
mailtoUrl.AddBodyText("This is my body of text & here we are.");
mailtoUrl.AddBodyText("");
mailtoUrl.AddBodyText("And here is yet more text. 100%!");
Process.Start(mailtoUrl.getMailtoUrl());
}
}
}