Click here to Skip to main content
15,881,715 members
Articles / Programming Languages / C#

Set the Time for E-Mail Sending from your .NET Application

Rate me:
Please Sign up or sign in to vote.
4.40/5 (5 votes)
3 Apr 2010GPL32 min read 40.1K   1.4K   29   3
Time triggered mailing which can help you to win over first in first come first serve situation
mailsystem.GIF

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:

C#
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:

C#
private string TimeStamp, CBody;
private int count;
private bool MailSent;
//Email variable
private string SMTPserver, IDstr, FromEmail, FromName, ToList, Subject, Body, Password;
private int Port;
private bool bSSL;

Taking inputs to message format:

C#
// MessageBox mymessage;
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:

C#
if (CBSSL.Checked)
   bSSL = true;
else
   bSSL = false;

Add Time Stamp in the bottom part of the message:

C#
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.

C#
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:

C#
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();
            //change body into html format
            Body = Body.Replace("\n", "\n<br>");
            //set message sender
            Msg.From = new MailAddress(FromEmail, FromName);
            //sets the mail recipients             
            //person(s) who will receive an email
            //(in this case 2 person will be emailed)
            Msg.To.Add(new MailAddress(ToList));
            //set to true if you want to use html in the body
            Msg.IsBodyHtml = true;
            Msg.Subject = Subject;
            //body of the email message with html tag
            Msg.Body = Body;
            //Allows applications to send e-mail
            // host server :  smtp.gmail.com
            //port number : 587
            SmtpClient objMail = new SmtpClient(SMTPserver, Port);
            //Some SMTP servers require you to authenticate first
            //gmail uses SSL      
            //info object contains the gmail
            //username, and password
            NetworkCredential info = new NetworkCredential(IDstr, Password);
            objMail.DeliveryMethod = SmtpDeliveryMethod.Network;
            objMail.Credentials = info;
            objMail.EnableSsl = bSSL;
            try
            {
                //final step send email
                objMail.Send(Msg);
                return true;
            }
            catch
            {
                return false;
            }
        }

Following is every second it checks time and sends when the time is correct.

C#
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()))    //alarm on
        {                   
            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.

mailsystem2.GIF

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

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Korea (Republic of) Korea (Republic of)
embedded software programmer

Comments and Discussions

 
Questionupdates Pin
kiquenet.com31-Dec-13 2:43
professionalkiquenet.com31-Dec-13 2:43 
GeneralCould Add the Design As weel in the Source Pin
RAKESH CHAUBEY14-Aug-12 8:00
RAKESH CHAUBEY14-Aug-12 8:00 
Generalahh Pin
chen hongli27-May-10 16:03
chen hongli27-May-10 16:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.