Pages

Ads 468x60px

Monday, July 23, 2012

C#: Send Email

This program will send email using gmail smtp client


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

namespace TestEmail
{
    public class Email
    {
private void sendMail()
        {
            string from         = "abc@gmail.com";
    string password = "******";
    string to             = "xyz@gmail.com";
    string subject     = "Subject Text";
    string body        = "Body Text";

            System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
            mail.To.Add(to);
            mail.From = new MailAddress(from, "Name" , System.Text.Encoding.UTF8);
            mail.Subject = subject;
            mail.SubjectEncoding = System.Text.Encoding.UTF8;
            mail.Body = body;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true ;
            mail.Priority = MailPriority.High;
            password = password;
            SmtpClient client = new SmtpClient();

            //Add the Creddentials- use your own email id and password

            client.Credentials = new System.Net.NetworkCredential(from, password);

            client.Port = 587; // Gmail works on this port
            client.Host = "smtp.gmail.com";
            client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(mail);
        }
    }
}

No comments:

Post a Comment