Published Monday, July 17, 2006 11:06 PM by Aleph

Send mail via SMTP with System.Web.Mail

I was looking for a way to send reports from an web application and I found a lot of code on the net, but the new System.Net.Mail from .NET 2.0 doesn't work with Gmail... so I've searched more and found a lot of good stuff on CodeProject. So here it is the class that I write based on those articles, simple and easy. The only problem is that it can’t send attachments. If anywone knows a way to send attachments using Gmail please let me know.
public sealed class SmtpMailer
{
    private string senderAddress;
    private string senderName;
    private string server;
    private string userName;
    private string password;
    private bool enableAuth;
    private bool enableSsl;
    private int port;

    private string errorMsg;
    public string ErrorMsg
    {
        get { return this.errorMsg; }
    }

    /// <summary>
    /// For public SMTP servers with no authentication
    /// </summary>
    public SmtpMailer(string senderAddress, string senderName, string server)
        : this(senderAddress, senderName, server, null, null, false, false, 25)
    { }

    /// <summary>
    /// For secure SMTP servers with authentication and SSL
    /// </summary>
    public SmtpMailer(string senderAddress, string senderName, string server, 
        string userName, string password, bool enableAuth, bool enableSsl, int port)
    {
        this.senderAddress = senderAddress;
        this.senderName = senderName;
        this.server = server;
        this.userName = userName;
        this.password = password;
        this.enableAuth = enableAuth;
        this.enableSsl = enableSsl;
        this.port = port;
    }

    public bool Send(string toAddress, string subject, string body, bool isHtml)
    {
        try

        {
            MailMessage mailMsg = new MailMessage();

            mailMsg.To = toAddress;
            mailMsg.Headers.Add("From", string.Format("{0} <{1}>", senderName, senderAddress));
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"] = server;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"] = port;
            mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusing"] = 2;

            if (enableAuth)
            {
                mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"] = 1;
                mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendusername"] = userName;
                mailMsg.Fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"] = password;
            }

            if (enableSsl)
            {
                mailMsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
            }

            if (isHtml)
            {
                mailMsg.BodyFormat = MailFormat.Html;
            }

            mailMsg.BodyEncoding = Encoding.UTF8;
            mailMsg.Subject = subject;
            mailMsg.Body = body;

            SmtpMail.SmtpServer = server;
            SmtpMail.Send(mailMsg);

            return true;
        }
        catch (Exception ex)
        {
            this.errorMsg = ex.Message;
            return false;
        }
    }
}



Filed under ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

No Comments

Leave a Comment

Name  

Comments 
Type the characters you see in the picture on the left.
    



Note: HTML tags in comment messages are not supported.


About Aleph

VB.NET programmer '02
C# programmer '03
Software Analyst '05
Software Architect '06