Words on my addictions: .NET, Philosophy, Music

10 September 2006

SMTP Class MailFactory

Filed under: Uncategorized — stefanprodan @ 8:55 pm

Helper class written in C#.NET 2.0 using System.Net.Mail. Supports html embedded images, SMTP authentication and binary attachments. Usage example will follow soon.

I am using this class in my Mass Emailer development.

public sealed class MailFactory
{
    private MailMessage message;
    private SmtpClient smtp;
    private AlternateView htmlView;

    private string error = string.Empty;
    public string ErrorMessage
    {
        get { return this.error; }
    }

    public MailFactory(string smtpServer, int port, Encoding encoding)
    {
        message = new MailMessage();
        message.SubjectEncoding = encoding;
        message.BodyEncoding = encoding;
        message.Headers.Add("X-Mailer", "AlephSmtpCore v1.0");

        smtp = new SmtpClient(smtpServer, port);
        smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
    }

    public void SetFrom(string fromAddress, string fromName)
    {
        message.From = new MailAddress(fromAddress, fromName);
    }

    public void SetExtraInfo(string replyAddress, string errorAddress, string organization, string location)
    {
        message.Headers.Add("Reply-To", replyAddress);
        message.Headers.Add("Errors-To", errorAddress);
        message.Headers.Add("Organization", organization);
        message.Headers.Add("X-Location", location);
    }

    public void AddTo(string address)
    {
        message.To.Add(new MailAddress(address));
    }

    public void RemoveToAddresses()
    {
        message.To.Clear();
    }

    public void CreateMail(string subject, string content)
    {
        message.Subject = subject;
        htmlView = AlternateView.CreateAlternateViewFromString(content, null, "text/html");
    }

    public void EmbedImage(Stream imageStream, string cid)
    {

        LinkedResource img = new LinkedResource(imageStream);
        img.ContentId = cid;
        htmlView.LinkedResources.Add(img);
    }

    public void AddAttachment(Stream fileStream, string name)
    {
        message.Attachments.Add(new Attachment(fileStream, name));
    }

    public void SetCredentials(string username, string password, bool useSsl)
    {
        smtp.Credentials = new NetworkCredential(username, password);
        smtp.EnableSsl = useSsl;
    }

    public bool Send()
    {
        try
        {
            smtp.Send(this.message);
            return true;
        }
        catch (SmtpFailedRecipientsException faildex)
        {
            this.error = "Recipient failed: " + faildex.Message;
        }
        catch (SmtpException smtpex)
        {
            this.error = "Server error: " + smtpex.Message;
        }
        catch (Exception ex)
        {
            this.error = "Unknown error: " + ex.Message;
        }

        return false;
    }
}

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: