SmtpClient.BeginSendRawMail Method

Sends email message with original raw content asynchronously.

[Visual Basic]
Public Function BeginSendMail( _
    server As SmtpServer, _
    mail() As Byte, _
    from As MailAddress, _
    recipients As AddressCollection, _
    callback As AsyncCallback, _
    state As Object _
) As SmtpClientAsyncResult
public SmtpClientAsyncResult BeginSendMail(
    SmtpServer server,
    byte[] mail,
    MailAddress from,
    AddressCollection recipients,    
    AsyncCallback callback,
    Object state
public: SmtpClientAsyncResult^ BeginSendMail(
    SmtpServer^ server,
    array<unsigned char>^ mail,
    MailAddress^ from,
    AddressCollection^ recipients,
    AsyncCallback^ callback,
    Object^ state
public function BeginSendMail( 
    server : SmtpServer, 
    mail : Byte[],
    from : MailAddress,
    recipients : AddressCollection,
    callback : AsyncCallback, 
    state As Object 
) : SmtpClientAsyncResult;


A SmtpServer instances used to send email.
A byte array instance which contains the email content to send.
A MailAddress instance represents the email sender.
A AddressCollection instance represents the email recipients.
The AsyncCallback delegate.
An object containing state information for this request.

Return Value

An SmtpClientAsyncResult that references the asynchronous send.


This method is used to send the customized email content. For example, you can use this method to forward an email without changing anything in the email. If from or recipients is null, EASendMail uses the from, recipient in orignal email file automatically.


[Visual Basic, C#] To get the full samples of EASendMail, please refer to Samples section.

[VB - Forward Email]
Imports EASendMail
Imports System.IO

Sub SendMail()
    Dim oSmtp As SmtpClient = New SmtpClient
    Dim ar As SmtpClientAsyncResult = Nothing

        Dim from As New MailAddress("")
        Dim recipients As New AddressColleciton("")

        Dim fs As New FileStream( "c:\test.eml", FileMode.Open, FileAccess.Read, FileShare.Read)
        Dim mail(fs.Length-1) As Byte
        fs.Read(mail, 0, fs.Length)

        Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
        ar = oSmtp.BeginSendRawMail(oServer, mail, from, recipients, Nothing, Nothing)
        Do While Not ar.AsyncWaitHandle.WaitOne(5, False)
            'do other thing


        Console.WriteLine("the message was sent to {0} successfully", _
    Catch exp As SmtpTerminatedException
    Catch exp As SmtpServerException
        Console.WriteLine("Exception: Server Respond: {0}", exp.ErrorMessage)
    Catch exp As System.Net.Sockets.SocketException
        Console.WriteLine("Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.ComponentModel.Win32Exception
        Console.WriteLine("Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message)
    Catch exp As System.Exception
        Console.WriteLine("Exception: Common: {0}", exp.Message)
    End Try

    Console.WriteLine("SMTP LOG:" & vbCrLf)
End Sub

[C# - Forward Email]
using System;
using EASendMail;
using System.IO;

void SendMail()
    SmtpClient oSmtp = new SmtpClient();
    SmtpClientAsyncResult ar = null;
        MailAddress from = new MailAddress("");
        AddressColleciton recipients = new AddressColleciton("");

        FileStream fs = new FileStream( "c:\\test.eml", FileMode.Open, FileAccess.Read, FileShare.Read);
        byte[] mail = new byte[fs.Length];
        fs.Read(mail, 0, fs.Length);
        SmtpServer oServer = new SmtpServer("myserveraddress");

        ar = oSmtp.BeginSendRawMail( oServer, mail, from, recipients, null, null );
        while(!ar.AsyncWaitHandle.WaitOne(5, false ))
            //do other thing

        oSmtp.EndSendMail( ar );

        Console.WriteLine( "the message was sent to {0} successfully",
            ar.SmtpClientInstance.CurrentSmtpServer.Server );
    catch( SmtpTerminatedException exp )
        Console.WriteLine( exp.Message );
    catch( SmtpServerException exp )
        Console.WriteLine( "Exception: Server Respond: {0}", exp.ErrorMessage );
    catch( System.Net.Sockets.SocketException exp )
        Console.WriteLine( "Exception: Networking Error: {0} {1}", exp.ErrorCode, exp.Message );
    catch( System.ComponentModel.Win32Exception exp )
        Console.WriteLine( "Exception: System Error: {0} {1}", exp.ErrorCode, exp.Message );            
    catch( System.Exception exp )
        Console.WriteLine( "Exception: Common: {0}", exp.Message );         

    Console.WriteLine( "SMTP LOG:\r\n" );
    Console.WriteLine( ar.SmtpClientInstance.SmtpConversation );

See Also

SmtpClient.BeginSendMail Method
Work with EASendMail Service (Email Queuing)
SmtpClient.SendMailToQueue Method
SmtpClient.SendMailToQueueEx Method

Online Tutorials

Send Email in VB 6.0 - Tutorial
Send Email in C# - Tutorial
Send Email in VB.NET - Tutorial
Send Email in Visual C++ - Tutorial
Send Email in Managed C++/CLI - Tutorial
Send Email in Delphi - Tutorial
Send Email in MS SQL stored procedure - Tutorial