Click here to Skip to main content
Licence GPL3
First Posted 3 Apr 2010
Views 5,880
Downloads 268
Bookmarked 27 times

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

By | 3 Apr 2010 | Article
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:

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;
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();
            //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.

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)

About the Author

JavierJoung



Korea (Republic Of) Korea (Republic Of)

Member

embedded software programmer

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalahh Pinmemberchen hongli16:03 27 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web01 | 2.5.120517.1 | Last Updated 3 Apr 2010
Article Copyright 2010 by JavierJoung
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid