Friday, 25 January 2013

Mail send C# Code


using System;
using System.Data;
using System.Configuration;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


/// <summary>
/// Summary description for SendMail
/// </summary>
public class SendMail
{
    public static bool sendMail(string[] toEmail, string[] ccEmail, string[] bccEmail, string Subject, string MessageBody, string AttachFilePath)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (ccEmail.Length > 0)
                for (int toCount = 0; toCount < ccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (ccEmail.Length > 0)
                for (int ccCount = 0; ccCount < ccEmail.Length; ccCount++)
                    if (!string.IsNullOrEmpty(ccEmail[ccCount]))
                        objMail.CC.Add(ccEmail[ccCount]);

            if (bccEmail.Length > 0)
                for (int bccCount = 0; bccCount < bccEmail.Length; bccCount++)
                    if (!string.IsNullOrEmpty(bccEmail[bccCount]))
                        objMail.Bcc.Add(bccEmail[bccCount]);
            objMail.From = new MailAddress(Membership.GetUser().Email); //Membership.GetUser().Email);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
       
        }
    }

    public static bool sendMailwithoutattachment(string[] toEmail, string[] ccEmail, string Subject, string MessageBody)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (ccEmail.Length > 0)
                for (int toCount = 0; toCount < ccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (ccEmail.Length > 0)
                for (int ccCount = 0; ccCount < ccEmail.Length; ccCount++)
                    if (!string.IsNullOrEmpty(ccEmail[ccCount]))
                        objMail.CC.Add(ccEmail[ccCount]);
            var frommail = "xxx@gmail.com"; //for testing
            objMail.From = new MailAddress(frommail);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
       
        }
    }
    public static bool sendMailBcc(string[] toEmail, string[] bccEmail, string Subject, string MessageBody)
    {
        SmtpClient objSMTP;
        MailMessage objMail;

        objMail = new MailMessage();
        objSMTP = new SmtpClient();
        try
        {
            if (bccEmail.Length > 0)
                for (int toCount = 0; toCount < bccEmail.Length; toCount++)
                    objMail.To.Add(toEmail[toCount]);

            if (bccEmail.Length > 0)
                for (int bccCount = 0; bccCount < bccEmail.Length; bccCount++)
                    if (!string.IsNullOrEmpty(bccEmail[bccCount]))
                        objMail.Bcc.Add(bccEmail[bccCount]);
            var frommail = "xxx@gmail.com."; //for testing
            objMail.From = new MailAddress(frommail);
            objMail.IsBodyHtml = true;
            objMail.Subject = Subject;
            objMail.Body = MessageBody;
            objSMTP.Send(objMail);
            return true;
        }
        catch (Exception ex)
        {
 
        }
    }
}

No comments:

Post a Comment