Live with Dot Net Just another Programming weblog

How to send mail using smtpclient

Posted on June 25, 2009

You can create a Mail Message dynamically and send the mail to recepient list through smtp client. You need to use System.Net.Mail to use the smtp client class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System.Configuration;
using System.Net.Mail;
 
        public static bool SendMailNotification()
        {
            MailMessage msg = new MailMessage();
            try
            {
                StringBuilder strBody = new StringBuilder("");
                strBody.Append("<span style='font-size: 9pt; font-family: Arial'>" + team + " Message body goes here<b>" + DateTime.Now.ToString("MMMM") + "</b><br /> ");
                strBody.Append("<br/><span style='font-size: 16pt; font-family: Arial'>Message body goes here<b>" + "</b></span><br /> <br />");
 
                string[] lsMailList = "Get mail receiptent list from database";
 
                foreach (string mail in lsMailList)
                {
                    msg.To.Add(mail);
                }
                msg.From = new MailAddress("from@domain.com");
                msg.Subject = "any Subject to the mail";
                AlternateView Body = AlternateView.CreateAlternateViewFromString(strBody.ToString(), null, "text/html");
                msg.AlternateViews.Add(Body);
                SendMail(msg);
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
 
        public static void SendMail(MailMessage msg)
        {
            try
            {
                SmtpClient smtp = new SmtpClient();
                smtp.Host =  ConfigurationSettings.AppSettings["MailServer"].ToString(); //relay server name
                smtp.Send(msg);
            }
            catch { }
        }

About admin

No description. Please complete your profile.
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.