Auto Send Birthday Wish C# code by using Console Application mail id get from Database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mime;
namespace BdayEmail
{
class Program
{
static void Main(string[] args)
{
try
{
string connStr = ConfigurationManager.ConnectionStrings["EmpDBConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(connStr);
con.Open();
SqlCommand cmd;
cmd = new SqlCommand("SELECT empemail,EmpDOB,empname,Photo,BdayMessage,CC FROM EmpDetails where datepart(day,EmpDOB)= datepart(day,sysdatetime()) and datepart(month,EmpDOB)=datepart(month,sysdatetime())", con);
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
string todaydate = DateTime.Now.ToString("dd/MM");
string Bday = Convert.ToDateTime(reader["EmpDOB"]).ToString("dd/MM");
int diff = DateTime.Compare(DateTime.Today, Convert.ToDateTime(Bday));
if (diff == 0)
{
string Email = reader["EmpEmail"].ToString();
string Name = reader["EmpName"].ToString();
string Photo = reader["Photo"].ToString();
string cc = reader["CC"].ToString();
string Bdaymessage = reader["BdayMessage"].ToString();
string body = "<br><b><i><font Size=4 face=Comic Sans MS color= #FF2B9E> Dear</br><br><font Size=4 face=Comic Sans MS color=#FF2B9E>" + Name + "</i></b>" + "<br>" + "<br><img src=" + Photo + "></body></html></body></html>" + "<br><b><font Size=4 face=Comic Sans MS color=#FF2B9E><i>" + Bdaymessage + "</i></b></br> " + "<br><br><br>";
AlternateView av = AlternateView.CreateAlternateViewFromString(body, null, System.Net.Mime.MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource(Photo, MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
av.LinkedResources.Add(inline);
System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.AlternateViews.Add(av);
msg.To.Add(Email);
msg.CC.Add(cc);
msg.Subject = " WIsH YoU MaNy MaNY HaPPy Birthday";
msg.From = new System.Net.Mail.MailAddress("sendermailid@gmail.com");
SmtpClient mailClient = new SmtpClient("smtp.gmail.com", 587);
NetworkCredential NetCrd = new NetworkCredential("sendermailid@gmail.com", "password");
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = true;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
reader.Close();
con.Close();
}
else
{
Console.Write("its a earlier date");
}
}
}
catch (Exception ex)
{
Console.Write("There is No Birthday" + ex);
}
}
}
}
No comments:
Post a Comment