Click here to Skip to main content
11,434,695 members (55,796 online)
Click here to Skip to main content

How to send a simple text email message

, 14 Nov 2004
Rate this:
Please Sign up or sign in to vote.
How to send a simple text email message.

Sample Image - IndySMTP.gif

Introduction

This article will provide a very basic overview of how to send a plain text mail message with the open source Indy sockets library for .NET.

Constructing the Message

The following code is the basic structure for constructing a message:

C#
Indy.Sockets.Message LMsg = new Indy.Sockets.Message();
LMsg.From.Text = textFrom.Text.Trim();
LMsg.Recipients.Add().Text = textTo.Text.Trim();
LMsg.Subject = textSubject.Text.Trim();
LMsg.Body.Text = textMsg.Text;

Visual Basic

Dim LMsg As New Indy.Sockets.Message
LMsg.From.Text = textFrom.Text.Trim
LMsg.Recipients.Add.Text = textTo.Text.Trim
LMsg.Subject = textSubject.Text.Trim
LMsg.Body.Text = textMsg.Text

A call to the Clear method is necessary only if a single Message is being reused. Clear resets the message content and other fields to their default or empty states.

It is actually legal to send messages without From, Subject, and Body. However, such a message is not very useful and many servers will reject it either as faulty, or probable spam. Thus, these properties should be considered the minimum requirements for sending a message.

For more advanced messages, the Message class also has properties for CC, BCC, Attachments, HTML and more.

Sending the Message

Once a message has been constructed, it must be delivered to an SMTP server. This is done using the SMTP class. The following code is the basic form for sending a message:

C#

SMTP LSMTP = new SMTP();
LSMTP.Host = textSMTPServer.Text.Trim();
LSMTP.Connect();
try {
  LSMTP.Send(LMsg);
  Status("Completed");
}
finally {
  LSMTP.Disconnect();
}

Visual Basic

Dim LSMTP As New SMTP
LSMTP.Host = textSMTPServer.Text.Trim
LSMTP.Connect()
Try
  LSMTP.Send(LMsg)
  'Status("Completed")
Finally
  LSMTP.Disconnect()
End Try

The host must be set so that the SMTP class knows where to send the messages to. In the case of SMTP, the host can be thought of as the address of the local post office.

The Send method accepts one argument which specifies the Message class to send.

In this example, only one message is sent. However, the SMTP protocol allows for multiple messages, so it is not necessary to connect and disconnect if multiple messages are to be sent. To send multiple messages, simply make additional calls to the Send method while connected. Multiple Message instances can be used, or an existing one can be modified between calls to Send.

Send Mail Demo

A very basic demo of sending a simple mail message is available as SendMail.

In addition to the basics covered in this article, the demo also makes use of the OnStatus event. The OnStatus event is a core event of Indy but implemented differently by each protocol. OnStatus is fired at various times during calls to Connect, Send, and Disconnect. Each time, a text message is passed that explains what is occurring. OnStatus is designed to provide user interface updates, or logging. It's not designed for pragmatic interpretation of state. In the SendMail demo, the messages from OnStatus are displayed into a ListBox.

In this demo, the OnStatus event has been defined as follows:

C#

private void Status(string AMessage) {
  lboxStatus.Items.Add(AMessage);
  // Allow the listbox to repaint
  Application.DoEvents();
  Application.DoEvents();
  Application.DoEvents();
}

private void SMTPStatus(object aSender, Status aStatus, string aText) {
  Status(aText);
}

Visual Basic

Private Sub Status(ByVal aMessage As String)
  lboxStatus.Items.Add(aMessage)
  ' Allow the listbox to repaint
  Application.DoEvents()
  Application.DoEvents()
  Application.DoEvents()
End Sub

Private Sub SMTPStatus(ByVal aSender As Object, ByVal aStatus As Status, 
  ByVal aText As String)
  Status(aText)
End Sub

When the Send Mail button is pressed, the following code is executed to send the message:

C#

private void butnSendMail_Click(object sender, System.EventArgs e)  {
  butnSendMail.Enabled = false; 
  try {
    Indy.Sockets.Message LMsg = new Indy.Sockets.Message();
    LMsg.From.Text = textFrom.Text.Trim();
    LMsg.Recipients.Add().Text = textTo.Text.Trim();
    LMsg.Subject = textSubject.Text.Trim();
    LMsg.Body.Text = textMsg.Text;

    // Attachment example
    // new AttachmentFile(LMsg.MessageParts, @"c:\temp\Hydroponics.txt");

    SMTP LSMTP = new SMTP();
    LSMTP.OnStatus += new Indy.Sockets.TIdStatusEvent(SMTPStatus);
    LSMTP.Host = textSMTPServer.Text.Trim();
    LSMTP.Connect();
    try {
      LSMTP.Send(LMsg);
      Status("Completed");
    }
    finally {
      LSMTP.Disconnect();
    }
  }
  finally {
    butnSendMail.Enabled = true;
  }
}

Visual Basic

Private Sub butnSendMail_Click(ByVal sender As System.Object, 
  ByVal e As System.EventArgs) Handles butnSendMail.Click
  butnSendMail.Enabled = False
  Try
    Dim LMsg As New Indy.Sockets.Message
    LMsg.From.Text = textFrom.Text.Trim
    LMsg.Recipients.Add.Text = textTo.Text.Trim
    LMsg.Subject = textSubject.Text.Trim
    LMsg.Body.Text = textMsg.Text

    ' Attachment example
    ' Dim xAttachment As New AttachmentFile(
    '    LMsg.MessageParts, "c:\temp\Hydroponics.txt")

    Dim LSMTP As New SMTP
    AddHandler LSMTP.OnStatus, AddressOf SMTPStatus
    LSMTP.Host = textSMTPServer.Text.Trim
    LSMTP.Connect()
    Try
      LSMTP.Send(LMsg)
      'Status("Completed")
    Finally
      LSMTP.Disconnect()
    End Try
  Finally
    butnSendMail.Enabled = True
  End Try
End Sub

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Chad Z. Hower, a.k.a. Kudzu
"Programming is an art form that fights back"
www.KudzuWorld.com

Formerly the Regional Developer Adviser (DPE) for Microsoft Middle East and Africa, he was responsible for 85 countries spanning 4 continents and 10 time zones. Now Chad is a Microsoft MVP.

Chad is the chair of several popular open source projects including Indy and Cosmos (C# Open Source Managed Operating System).

Chad is the author of the book Indy in Depth and has contributed to several other books on network communications and general programming.

Chad has lived in Canada, Cyprus, Switzerland, France, Jordan, Russia, Turkey, and the United States. Chad has visited more than 60 countries, visiting most of them several times.

Comments and Discussions

 
QuestionI got this Exception at this line : - Indy.Sockets.Message LMsg = new Indy.Sockets.Message(); Pin
Member 1033383527-Mar-14 9:40
memberMember 1033383527-Mar-14 9:40 
General[Message Deleted] Pin
it.ragester2-Apr-09 22:50
memberit.ragester2-Apr-09 22:50 
QuestionPlease Help me Pin
polash_p3-May-08 19:38
memberpolash_p3-May-08 19:38 
AnswerRe: Please Help me Pin
Chad Z. Hower aka Kudzu4-May-08 7:00
memberChad Z. Hower aka Kudzu4-May-08 7:00 
GeneralReply Pin
Anjay11-Apr-08 19:18
memberAnjay11-Apr-08 19:18 
QuestionNice example - how about SMTPRelay? Pin
g18c12-Dec-07 22:17
memberg18c12-Dec-07 22:17 
Generalhi Pin
jer marshall12-Dec-07 9:02
memberjer marshall12-Dec-07 9:02 
Questionan i add Pin
Hesham yassin7-Jun-07 3:30
memberHesham yassin7-Jun-07 3:30 
GeneralPlz help me(mail msg) Pin
Ahmed El-Badry31-Jan-07 4:38
memberAhmed El-Badry31-Jan-07 4:38 
GeneralError using a web service...... Pin
cransaxbeth27-Dec-06 3:44
membercransaxbeth27-Dec-06 3:44 
GeneralAddress Rejected Error Message [modified] Pin
cransaxbeth6-Dec-06 7:24
membercransaxbeth6-Dec-06 7:24 
GeneralRe: Address Rejected Error Message Pin
Chad Z. Hower aka Kudzu6-Dec-06 7:31
memberChad Z. Hower aka Kudzu6-Dec-06 7:31 
GeneralRe: Address Rejected Error Message Pin
cransaxbeth12-Dec-06 3:17
membercransaxbeth12-Dec-06 3:17 
QuestionCan't loop through messageParts Pin
Andrej Kocevar11-Oct-06 4:35
memberAndrej Kocevar11-Oct-06 4:35 
GeneralCannot build the solution Pin
sanu204-Oct-06 1:51
membersanu204-Oct-06 1:51 
QuestionRe: Cannot build the solution Pin
Matthijs ter Woord5-Oct-06 22:04
memberMatthijs ter Woord5-Oct-06 22:04 
QuestionSMTP server? Pin
Bryan Llanes17-Mar-06 19:55
memberBryan Llanes17-Mar-06 19:55 
AnswerRe: SMTP server? Pin
Chad Z. Hower aka Kudzu19-Mar-06 0:37
memberChad Z. Hower aka Kudzu19-Mar-06 0:37 
QuestionHow can I embedded an image in the Mail using VB.net? Pin
george bush 3315-Mar-06 15:39
membergeorge bush 3315-Mar-06 15:39 
AnswerRe: How can I embedded an image in the Mail using VB.net? Pin
Chad Z. Hower aka Kudzu17-Mar-06 2:29
memberChad Z. Hower aka Kudzu17-Mar-06 2:29 
QuestionHow to send accented characters in the subject line? Pin
Lairton Ballin3-Mar-06 3:44
memberLairton Ballin3-Mar-06 3:44 
AnswerRe: How to send accented characters in the subject line? Pin
Chad Z. Hower aka Kudzu4-Mar-06 8:27
memberChad Z. Hower aka Kudzu4-Mar-06 8:27 
QuestionCan't send mail!? Pin
fanaticus5-Oct-05 7:33
memberfanaticus5-Oct-05 7:33 
GeneralSubject in email header Pin
purse0017-Jul-05 21:51
memberpurse0017-Jul-05 21:51 
GeneralRe: Subject in email header Pin
Chad Z. Hower aka Kudzu8-Jul-05 4:38
memberChad Z. Hower aka Kudzu8-Jul-05 4:38 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 15 Nov 2004
Article Copyright 2004 by Chad Z. Hower aka Kudzu
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid