Click here to Skip to main content
12,954,522 members (56,679 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


29 bookmarked
Posted 3 Apr 2010

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

, 3 Apr 2010 GPL3
Rate this:
Please Sign up or sign in to vote.
Time triggered mailing which can help you to win over first in first come first serve situation


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.


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;
//Email variable
private string SMTPserver, IDstr, FromEmail, FromName, ToList, Subject, Body, Password;
private int Port;
private bool bSSL;

Taking inputs to message format:

// 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:

if (CBSSL.Checked)
   bSSL = true;
   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.

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);
}} 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();
            //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 :
            //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;
                //final step send email
                return true;
                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()))    //alarm on
            button1_Click(sender, e);
        NUDHor.Enabled = true;
        NUDMin.Enabled = true;
        NUDSec.Enabled = true;
    if (CBLSC.Checked)
        TBSMTP.Enabled = false;
        TBID.Enabled = false;
        TBPORT.Enabled = false;
        MBTPASS.Enabled = false;
        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.


  • 3rd April, 2010: Version 1.0 created


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


About the Author

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

You may also be interested in...


Comments and Discussions

GeneralCould Add the Design As weel in the Source Pin
RAKESH CHAUBEY14-Aug-12 8:00
groupRAKESH CHAUBEY14-Aug-12 8:00 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170525.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by JavierJoung
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid