Hi democloud,
Refer the below code.
C#
using System;
using System.Data;
using System.Net.Mail;
/// <summary>
/// Summary description for ScheduleMail
/// </summary>
public class ScheduleMail
{
public ScheduleMail()
{
//
// TODO: Add constructor logic here
//
}
public void SendScheduleMail()
{
// Fetch record from Database.
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Email", typeof(string)),
new DataColumn("Date",typeof(DateTime)) });
dt.Rows.Add(1, "john@gmail.com", "2018/11/06");
dt.Rows.Add(2, "mudassar@gmail.com", "2018/11/25");
dt.Rows.Add(3, "suzanne@gmail.com", "2018/11/30");
dt.Rows.Add(4, "robert@gmail.com", "2018/11/07");
for (int i = 0; i < dt.Rows.Count; i++)
{
// Checking condition of Current Date with Database Date.
if (Convert.ToDateTime(dt.Rows[i]["Date"]).AddDays(-7).ToShortDateString() == DateTime.Now.ToShortDateString())
{
using (MailMessage mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress("sender@gmail.com");
mailMessage.Subject = "Test Subject";
mailMessage.Body = "Test Body";
mailMessage.IsBodyHtml = true;
mailMessage.To.Add(new MailAddress(dt.Rows[i]["Email"].ToString()));
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "sender@gmail.com";
NetworkCred.Password = "Password";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = 587;
smtp.Send(mailMessage);
}
}
}
}
}
VB.Net
Imports System
Imports System.Data
Imports System.Net.Mail
Public Class ScheduleMail
Public Sub New()
End Sub
Public Sub SendScheduleMail()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("Id", GetType(Integer)), New DataColumn("Email", GetType(String)), New DataColumn("Date", GetType(DateTime))})
dt.Rows.Add(1, "john@gmail.com", "2018/11/06")
dt.Rows.Add(2, "mudassar@gmail.com", "2018/11/25")
dt.Rows.Add(3, "suzanne@gmail.com", "2018/11/30")
dt.Rows.Add(4, "robert@gmail.com", "2018/11/07")
For i As Integer = 0 To dt.Rows.Count - 1
If Convert.ToDateTime(dt.Rows(i)("Date")).AddDays(-7).ToShortDateString() = DateTime.Now.ToShortDateString() Then
Using mailMessage As MailMessage = New MailMessage()
mailMessage.From = New MailAddress("sender@gmail.com")
mailMessage.Subject = "Test Subject"
mailMessage.Body = "Test Body"
mailMessage.IsBodyHtml = True
mailMessage.[To].Add(New MailAddress(dt.Rows(i)("Email").ToString()))
Dim smtp As SmtpClient = New SmtpClient()
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential()
NetworkCred.UserName = "sender@gmail.com"
NetworkCred.Password = "Password"
smtp.UseDefaultCredentials = True
smtp.Credentials = NetworkCred
smtp.Port = 587
smtp.Send(mailMessage)
End Using
End If
Next
End Sub
End Class
Set timer interval to 1 day. So that every day the SendScheduleMail function gets called and checked with database date. Once the database date match as per condition then email will be is send.