Archive for the ‘Dot Net’ Category

Sending Emails from C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            string SmtpServer = ConfigurationManager.AppSettings[“App_Mail_Server”]; // Provide the SMTP server name
            string strFrom = “abc@domain.com”;
            string strTo = “cde@domain.com“;  // If no mail id required, give blank
            string strCC = “afc@domain.com“;  // If no mail id required, give blank
            string strBCC = “sdf@domain.com“;     // If no mail id required, give blank
            string strPeriority = “High”;
            string strAttachment = @”C:\Test.txt,D:\Test1.txt”;
            string strSubject = “Test Mail with Multiple Attachment”;
            string strBody = “Test Mail with Multiple Attachment”;
            bool IsBodyIsInHTML = false;

            try
            {
                SmtpClient smtp = new SmtpClient(SmtpServer);
                MailMessage mssg = new MailMessage();
                mssg.From = new MailAddress(strFrom);
                mssg.Subject = strSubject;
                mssg.Body = strBody;

                //Adding multiple To Addresses
                if (strTo.Trim().Length > 0)
                {
                    foreach (string sTo in strTo.Split(“,”.ToCharArray()))
                        mssg.To.Add(sTo);
                }

                //Adding multiple CC Addresses
                if (strCC.Trim().Length > 0)
                {
                    foreach (string sCC in strCC.Split(“,”.ToCharArray()))
                        mssg.CC.Add(sCC);
                }

                //Adding multiple BCC Addresses
                if (strBCC.Trim().Length > 0)
                {
                    foreach (string sBCC in strBCC.Split(“,”.ToCharArray()))
                        mssg.Bcc.Add(sBCC);
                }

                //Adding multiple BCC Addresses
                if (strAttachment.Trim().Length > 0)
                {
                    foreach (string sAttachment in strAttachment.Split(“,”.ToCharArray()))
                    {
                        Attachment attachment = new Attachment(sAttachment);
                        mssg.Attachments.Add(attachment);
                    }
                }

                MailPriority mailPeriority = MailPriority.Normal;
                switch (strPeriority)
                {
                    case “High”:
                        mailPeriority = MailPriority.High;
                        break;
                    case “Low”:
                        mailPeriority = MailPriority.Low;
                        break;
                    case “Normal”:
                        mailPeriority = MailPriority.Normal;
                        break;
                }
                mssg.Priority = mailPeriority;

                mssg.BodyEncoding = System.Text.Encoding.Default;
                mssg.SubjectEncoding = System.Text.Encoding.Default;
                mssg.IsBodyHtml = IsBodyIsInHTML;
                smtp.Send(mssg);
            }
            catch (Exception ex)
            {
                //trach the exception and write in the log. might be Main Addresses contains non mail id formats.
            }

        }
    }
}