Monday, 20 February 2012

Email Sending Codes for C#

using System;
using System.Net;
using System.Net.Mail;
using System.Collections;
public class MailSender
{
    public static bool SendEmail(
        string pGmailEmail,
        string pGmailPassword,
        string pTo,
        string pSubject,
        string pBody,

        ArrayList pAttachmentPath)
    {
        try
        {
            NetworkCredential loginInfo = new NetworkCredential(pGmailEmail, pGmailPassword);
            MailMessage msg = new MailMessage();
            msg.From = new MailAddress(pGmailEmail);
            msg.To.Add(new MailAddress(pTo));
            //msg.Bcc.Add(new MailAddress("ashok.prasad@akshamaala.com"));
            msg.Subject = pSubject;
            msg.Body = pBody;
            msg.IsBodyHtml = true;
            System.Net.Mail.Attachment attachment;
            for (int i = 0; i < pAttachmentPath.Count; i++)
            {
                attachment = new System.Net.Mail.Attachment(Convert.ToString(pAttachmentPath[i]));
                msg.Attachments.Add(attachment);
            }
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.UseDefaultCredentials = false;
            client.Credentials = loginInfo;
            client.Send(msg);
            msg.Dispose();
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }

No comments:

Post a Comment