Selected C# Code Snippets :: [ previous - toc - next ]

Mail Service Component

The following small singleton can send mails. One method works in any standalone context, the other method uses configuration data from a regular ASP.NET environment. (C# 3.0, .net 3.5)

using System.Collections.Generic;

using System.Configuration;

using System.Net;

using System.Net.Configuration;

using System.Net.Mail;

using System.Web.Configuration;

 

namespace com.ooit.edu {

 

 

 

    public class MailService {

 

        private static MailService instance;

 

        // get singleton

        public static MailService GetInstance() {

            return instance == null ? instance = new MailService() : instance;

        }

 

        // eg @"~\web.config"

        public static string WEB_CONFIG_PATH { get; set; }

 

        private MailService() { }

 

        // send regular mail

        public void SendMail(string from, IList<string> tos, string subject, string text,

            string smtpHost, string userid, string passwd) {

           

            // create message

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(from);

            foreach (string to in tos) {

                msg.To.Add(new MailAddress(to));

            }

            msg.Subject = subject;

            msg.Body = text;

            msg.IsBodyHtml = true;

            msg.Priority = MailPriority.High;

            // connect to smtp server

            NetworkCredential SMTPUserInfo = new NetworkCredential(userid, passwd);

            SmtpClient c = new SmtpClient(smtpHost);

            c.Credentials = SMTPUserInfo;

            // send

            c.Send(msg);

        }

 

 

        // send mail within the context of ASP.NET

        public void SendMailInASP(IList<string> tos, string subject, string text) {

            // read configuration file usually : @"~\web.config"

            Configuration config = WebConfigurationManager.OpenWebConfiguration(WEB_CONFIG_PATH);

            MailSettingsSectionGroup settings =

                    (MailSettingsSectionGroup)config.GetSectionGroup("system.net/mailSettings");

            MailMessage msg = new MailMessage();

            msg.From = new MailAddress(settings.Smtp.From);

            foreach (string to in tos) {

                msg.To.Add(new MailAddress(to));

            }

            msg.Subject = subject;

            msg.Body = text;

            msg.IsBodyHtml = true;

            msg.Priority = MailPriority.High;

            NetworkCredential SMTPUserInfo = new NetworkCredential(

                settings.Smtp.Network.UserName,

                settings.Smtp.Network.Password);

            SmtpClient c = new SmtpClient(settings.Smtp.Network.Host);

            c.Credentials = SMTPUserInfo;

            c.Send(msg);

        }

 

    }

 

 

    public static class Mail {

 

        public static void Main(params string[] args) {

            MailService.GetInstance().SendMail("mike@1234.ch",

                new string[] { "bob@yahoo.ch" }, "have a good day",

                "<h1>Have a good day !!!</h1>",

                "smtp.1234.ch", "mike007", "Ejsed");

        }

 

    }

 

 

 

}