|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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 MessageThe 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 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 Sending the MessageOnce a message has been constructed, it must be delivered to an SMTP server. This is done using the 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 The 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 Mail DemoA 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 In this demo, the 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
|
||||||||||||||||||||||