Thursday, 21 August 2014

Send Email With ASP.NET C# with Formated Text

Send Email With ASP.NET C# with Formated Text

Here i have implemented Email send program with the console application just you need to use your email address and password from where you want to send the email.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Data;
namespace TaskRunner
{
    class Program
    {
        public static string connectionString="ServerName;Initial Catalog=DataBaseName;Persist Security Info=True;User";
   
   
        static void Main(string[] args)
        {
            Program p = new Program();
            p.SendMail();
        }
       
    protected void SendMail()
    {
 
        SqlConnection cn = new SqlConnection(@""+connectionString+"");
        DataSet ds = new DataSet();
        DateTime d = new DateTime();
        string yesterday = DateTime.Today.ToString("yyyy-MM-dd");
        string nextday = DateTime.Today.AddDays(1).ToString("yyyy-MM-dd");
   
        SqlDataAdapter da = new SqlDataAdapter("select * from Table where id=1", cn);
        da.Fill(ds);
        int i=0;
        for (i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            Console.WriteLine("Please wait ...");
            System.Net.Mail.MailMessage ms = new System.Net.Mail.MailMessage();
            var fromAddress = "from@gmail.com";      // Gmail Address from where you send the mail
            MailAddress to = new MailAddress("to@abc.com"); // Address to whome you send the mail
       
            string body = "Hi XYZ, <br>";
            body += "<h2>How are You?</h2><h3>Dear<br> Thank you, <br> From Jayesh Jakhaniya";
       
            ms.From = new MailAddress("from@gmail.com");
            ms.To.Add(to);
            ms.Subject = "Subject";
            ms.IsBodyHtml = true;
            ms.Body = body;
       
            var fromPassword = "password";
       
            var smtp = new System.Net.Mail.SmtpClient();
            {
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.EnableSsl = true;
                smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
                smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
                smtp.Timeout = 20000;
            }
       
            smtp.Send(ms);
        }
   
    }
    }


}



No comments:

Post a Comment