Introduction
This is just my first trial for C#. This program will let you send e-mail at a certain time (with second). Usually normal email sending timer is with minutes. So I decided to make sending e-mail in an exact time period. I hope this project will be useful for someone.
Background
Syed Moshiur Murshed's article (Send E-Mail from your .NET Application using your GMail Account) inspired me. Also I tried his idea and tried to send the mail on time for a certain application sending. Sometimes in life, you might need to send a certain mail at a certain time. So I created this project based on Syed Moshiur Murshed's article. Thanks~!
Using the Code
The code is self explanatory by looking at the following function.
The main includes are as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.Threading;
using System.Timers;
using System.Security;
Initial variables:
private string TimeStamp, CBody;
private int count;
private bool MailSent;
private string SMTPserver, IDstr, FromEmail, FromName, ToList, Subject, Body, Password;
private int Port;
private bool bSSL;
Taking inputs to message format:
SMTPserver = TBSMTP.Text.ToString();
IDstr =TBID.Text.ToString();
Port = int.Parse(TBPORT.Text);
Password =MBTPASS.Text.ToString();
ToList =TBTO.Text.ToString();
FromEmail= TBFROM.Text.ToString();
FromName =TBAKA.Text.ToString();
Subject =TBSUBJECT.Text.ToString();
Body = RTBBODY.Text.ToString();
Secure Sockets Layer check:
if (CBSSL.Checked)
bSSL = true;
else
bSSL = false;
Add Time Stamp in the bottom part of the message:
TimeStamp = "\n\n\n\nTimestamp->" + "." + TBHour.Text +
"." + TBMinu.Text + "." +TBSeco.Text;
Body += TimeStamp;
Sending trial which may fail in certain situations that will try again if there is a crisis.
while(count<5)
{
MailSent = sendMail(bSSL, SMTPserver, IDstr, Port, FromEmail,
FromName, ToList, Subject, Body, Password);
if (MailSent)
{
MessageBox.Show("MAIL SENT! \n\r SUCESS!", "RESULT:",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
break;
}
else
{
count++;
}} if (count >0)
MessageBox.Show("MAIL SENDING \n\r FAILED", "RESULT:",
MessageBoxButtons.OK, MessageBoxIcon.Error);
The following is how to send mail through Gmail:
public bool sendMail(bool bSSL,string SMTPserver, string IDstr, int Port,
string FromEmail, string FromName, string ToList, string Subject,
string Body, string Password)
{
MailMessage Msg = new MailMessage();
Body = Body.Replace("\n", "\n<br>");
Msg.From = new MailAddress(FromEmail, FromName);
Msg.To.Add(new MailAddress(ToList));
Msg.IsBodyHtml = true;
Msg.Subject = Subject;
Msg.Body = Body;
SmtpClient objMail = new SmtpClient(SMTPserver, Port);
NetworkCredential info = new NetworkCredential(IDstr, Password);
objMail.DeliveryMethod = SmtpDeliveryMethod.Network;
objMail.Credentials = info;
objMail.EnableSsl = bSSL;
try
{
objMail.Send(Msg);
return true;
}
catch
{
return false;
}
}
Following is every second it checks time and sends when the time is correct.
private void timer1_Tick(object sender, EventArgs e)
{
string m_CSec = DateTime.Now.Second.ToString();
string m_CMin = DateTime.Now.Minute.ToString();
string m_CHor = DateTime.Now.Hour.ToString();
TBHour.Text = m_CHor;
TBMinu.Text = m_CMin;
TBSeco.Text = m_CSec;
if (AlarmTog.Checked)
{
NUDHor.Enabled = false;
NUDMin.Enabled = false;
NUDSec.Enabled = false;
if ((m_CSec == NUDSec.Value.ToString()) &&
(m_CMin == NUDMin.Value.ToString()) &&
(m_CHor == NUDHor.Value.ToString()))
{
button1_Click(sender, e);
timer1.Stop();
}
}
else
{
NUDHor.Enabled = true;
NUDMin.Enabled = true;
NUDSec.Enabled = true;
}
if (CBLSC.Checked)
{
TBSMTP.Enabled = false;
TBID.Enabled = false;
TBPORT.Enabled = false;
MBTPASS.Enabled = false;
}
else
{
TBSMTP.Enabled = true;
TBID.Enabled = true;
TBPORT.Enabled = true;
MBTPASS.Enabled = true;
}
}
Form --> time check --> send is a very simple way and anyone can write this code.
Points of Interest
Even though this is not hard to do, I found that there is a good use of it. In my Outlook, I can only set hh:mm instead of hh:mm:ss. This project enables you to send out at the time you wanted it to be sent (with second!!). you can check several e-mails for yourself and check how fast it comes to your e-mail server.
I learned that there is a delay time in my e-mail server + the email process delay time in their (?) server so there is a time delay. You have to calculate this by yourself. However, the e-mail prints out a time stamp when the e-mail was sent, so you can control this bit easier.
History
- 3rd April, 2010: Version 1.0 created