PowerTCP Mail for .NET
BeginSend(Stream,Object) Method
See Also  Example Send comments on this topic.
Dart.PowerTCP.Mail Namespace > Smtp Class > BeginSend Method : BeginSend(Stream,Object) Method




message
A MessageStream object representing an email message.
state
User state information.
Asynchronously send a MessageStream object that represents an email message.

Syntax

Visual Basic (Declaration) 
Public Overloads Function BeginSend( _
   ByVal message As Stream, _
   ByVal state As Object _
) As IAsyncResult
Visual Basic (Usage)Copy Code
Dim instance As Smtp
Dim message As Stream
Dim state As Object
Dim value As IAsyncResult
 
value = instance.BeginSend(message, state)
C# 
public IAsyncResult BeginSend( 
   Stream message,
   object state
)
Managed Extensions for C++ 
public: IAsyncResult* BeginSend( 
   Stream* message,
   Object* state
) 
C++/CLI 
public:
IAsyncResult^ BeginSend( 
   Stream^ message,
   Object^ state
) 

Parameters

message
A MessageStream object representing an email message.
state
User state information.

Return Value

An IAsyncResult that represents the asynchronous operation, which could still be pending.

Exceptions

ExceptionDescription
Dart.PowerTCP.Mail.ProtocolExceptionBad SMTP protocol response received from server.
System.InvalidOperationException Smtp.BeginSend used without providing an EndSend event handler.
System.Net.Sockets.SocketExceptionThe requested address is not valid in its context.
System.IO.IOExceptionThe filename, directory name, or volume label syntax is incorrect.
System.ArgumentExceptionA required argument contains invalid characters, is empty, or contains only white spaces.

Example

The following example demonstrates using the Smtp.BeginSend method and the Smtp.EndSend event.
Visual BasicCopy Code
' Be sure to import the namespace by putting "Imports Dart.PowerTCP.Mail"
' at the top of your class.

Private Sub BeginSendTest()
   ' Create a message
   Dim msg As New MessageStream()
   msg.To.Add(New MailAddress("you@test.com"))
   msg.From = New MailAddress("me@test.com")
   msg.Subject = "test"
   msg.Text = "This is a test"

   ' Begin to asynchronously send the message
   Smtp1.Server = "mail.test.com"
   Smtp1.BeginSend(msg, Nothing)
End Sub

Private Sub Smtp1_EndSend(ByVal sender As Object, ByVal e As Dart.PowerTCP.Mail.SmtpEventArgs) Handles Smtp1.EndSend
   ' Send operation complete. Check to see if an exception was thrown during the operation.
   If e.Exception Is Nothing Then
      Debug.WriteLine("Send operation complete")
      Dim m As MailAddress
      For Each m In e.Response.Recipients
         Debug.WriteLine("Mail sent to " + m.Address)
      Next
   End If
End Sub
C#Copy Code
// Be sure to import the namespace by putting "using Dart.PowerTCP.Mail;"
// at the top of your class.

private void BeginSendTest()
{
   // Create a message
   MessageStream msg = new MessageStream();
   msg.To.Add(new MailAddress("you@test.com"));
   msg.From = new MailAddress("me@test.com");
   msg.Subject = "test";
   msg.Text = "This is a test";

   // Begin to asynchronously send the message
   smtp1.Server = "mail.test.com";
   smtp1.BeginSend(msg, null);
}

private void smtp1_EndSend(object sender, Dart.PowerTCP.Mail.SmtpEventArgs e)
{
   // Send operation complete. Check to see if an exception was thrown during the operation.
   if(e.Exception == null)
   {
      Debug.WriteLine("Send operation complete");
      foreach(MailAddress m in e.Response.Recipients)
         Debug.WriteLine("Mail sent to " + m.Address); 
   }
}

Remarks

This method begins to send a email message. Upon completion the Smtp.EndSend event will be raised. An SmtpEventArgs object will be passed into this event, containing data about the message sent. For example, the SmtpEventArgs will contain data such as the length of the email message sent. If you are using the Smtp component as a reference, you must "wire up" the event yourself. This involves creating a method that acts as the event handler that implements the SmtpEventHandler delegate. See Using Events When Using A Component As A Reference for more information.

The object state is useful for any kind of state information that you would like accessible in the Smtp.EndGet event. Since the method is "disconnected" from the calling code and returns to the calling thread elsewhere, you may wish to pass an object/variable as state to be retrieved and used in the event handler.

To send an email message synchronously, use Smtp.Send.

Use The Smtp.BeginSend(Stream,Object) Method If...

Use this method if you wish to send an email message transparently with minimal application impact, as execution occurs on another thread.

Requirements

Target Platforms: Microsoft .NET Framework 2.0

See Also

Documentation Version 3.2
© 2010 Dart Communications. All Rights Reserved.