![]() |
General Programming »
Internet / Network »
Email
Intermediate
How to send a simple text email messageBy Chad Z. Hower aka KudzuHow to send a simple text email message. |
C#, VB, Windows, .NET 1.1VS.NET2003, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||

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.
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.
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.
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
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 14 Nov 2004 Editor: Nishant Sivakumar |
Copyright 2004 by Chad Z. Hower aka Kudzu Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |