SmtpClient.SendMail Method


Sends an e-mail message.

[Visual Basic]
Public Sub SendMail( _
    server As SmtpServer, _
    mail As SmtpMail _
)

Public Sub SendMail( _
    mail As SmtpMail _
)
[C#]
public void SendMail(
    SmtpServer server,
    SmtpMail mail
);

public void SendMail(
    SmtpMail mail
);
[C++]
public: void SendMail(
    SmtpServer^ server,
    SmtpMail^ mail
);

public: void SendMail(
    SmtpMail^ mail
);
[JScript]
public function SendMail( 
    server : SmtpServer, 
    mail : SmtpMail
);

public function SendMail( 
    mail : SmtpMail
);

Parameters

server
A SmtpServer instances used to send email.
mail
A SmtpMail instance to send.

Remarks

The combination of Connect, Quit, Reset methods and SmtpClient.SendMail( SmtpMail mail ) methods sends multiple emails in one SMTP connection session. DO NOT use Connect, Quit and Reset methods with SmtpClient.SendMail( SmtpServer server, SmtpMail mail ) method. SmtpClient.SendMail( SmtpServer server, SmtpMail mail ) method connects and disconnects smtp server automatically.

Example

[Visual Basic, C#, C++] The following example demonstrates how to send email with SmtpClient class. To get the full samples of EASendMail, please refer to Samples section.

[VB - Send multiple emails in one SMTP connection session]

Imports EASendMail

Sub SendTwoMailsInOneConnection()

    Try
        Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
        ' SMTP user authentication
        oServer.User = "myusername"
        oServer.Password = "mypassword"

        ' Most mordern SMTP servers require SSL/TLS connection now.
        ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
        oServer.ConnectType = SmtpConnectType.ConnectTryTLS

        Dim oSmtp As SmtpClient = New SmtpClient
        oSmtp.Connect(oServer)

        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("to@adminsystem.com"))

        oMail.Subject = "firset test email"
        oMail.TextBody = "test body"

        oSmtp.SendMail(oMail)
        Console.WriteLine("first sent")

        oSmtp.Reset()
        oMail.Reset()

        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("another@adminsystem.com"))

        oMail.Subject = "second test email"
        oMail.TextBody = "another body"

        oSmtp.SendMail(oMail)
        Console.WriteLine("second sent")
        oSmtp.Quit()

    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub

Sub SendMail()

    Try
        Dim oServer As SmtpServer = New SmtpServer("myserveraddress")
        ' SMTP user authentication
        oServer.User = "myusername"
        oServer.Password = "mypassword"

        ' Most mordern SMTP servers require SSL/TLS connection now.
        ' ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
        oServer.ConnectType = SmtpConnectType.ConnectTryTLS

        Dim oMail As SmtpMail = New SmtpMail("TryIt")
        oMail.From = New MailAddress("from@adminsystem.com")
        oMail.To.Add(New MailAddress("to@adminsystem.com"))

        oMail.Subject = "test subject"
        oMail.TextBody = "test body"

        Dim oSmtp As SmtpClient = New SmtpClient()
        oSmtp.SendMail(oServer, oMail)
        Console.WriteLine("message was sent")
    Catch exp As Exception
        Console.WriteLine("Exception: {0}", exp.Message)
    End Try

End Sub


[C# - Send multiple emails in one SMTP connection session] using System; using EASendMail; void SendTwoMailsInOneConnection() { try { SmtpServer oServer = new SmtpServer("myserveraddress"); // SMTP user authentication oServer.User = "myusername"; oServer.Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = SmtpConnectType.ConnectTryTLS; SmtpClient oSmtp = new SmtpClient(); oSmtp.Connect(oServer); SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("to@adminsystem.com")); oMail.Subject = "first test email"; oMail.TextBody = "test body"; oSmtp.SendMail(oMail); Console.WriteLine("first sent"); oSmtp.Reset(); oMail.Reset(); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("another@adminsystem.com")); oMail.Subject = "second test email"; oMail.TextBody = "another body"; oSmtp.SendMail(oMail); Console.WriteLine("second sent"); oSmtp.Quit(); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } } void SendMail() { try { SmtpServer oServer = new SmtpServer("myserveraddress"); // SMTP user authentication oServer.User = "myusername"; oServer.Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer.ConnectType = SmtpConnectType.ConnectTryTLS; SmtpMail oMail = new SmtpMail("TryIt"); oMail.From = new MailAddress("from@adminsystem.com"); oMail.To.Add(new MailAddress("to@adminsystem.com")); oMail.Subject = "test subject"; oMail.TextBody = "test body"; SmtpClient oSmtp = new SmtpClient(); oSmtp.SendMail(oServer, oMail); Console.WriteLine("message was sent"); } catch (Exception exp) { Console.WriteLine("Exception: {0}", exp.Message); } }
[C++/CLI - Send multiple emails in one SMTP connection session] using namespace System; using namespace EASendMail; void SendTwoMailsInOneConnection() { try { SmtpServer ^oServer = gcnew SmtpServer("myserveraddres"); // SMTP user authentication oServer->User = "myusername"; oServer->Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = SmtpConnectType::ConnectTryTLS; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->Connect(oServer); SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew MailAddress("from@adminsystem.com"); oMail->To->Add(gcnew MailAddress("to@adminsystem.com")); oMail->Subject = "first test email"; oMail->TextBody = "test body"; oSmtp->SendMail(oMail); Console::WriteLine("first sent"); oSmtp->Reset(); oMail->Reset(); oMail->From = gcnew MailAddress("from@adminsystem.com"); oMail->To->Add(gcnew MailAddress("another@adminsystem.com")); oMail->Subject = "second test email"; oMail->TextBody = "another body"; oSmtp->SendMail(oMail); Console::WriteLine("second sent"); oSmtp->Quit(); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } } void SendMail() { try { SmtpServer ^oServer = gcnew SmtpServer("myserveraddres"); // SMTP user authentication oServer->User = "myusername"; oServer->Password = "mypassword"; // Most mordern SMTP servers require SSL/TLS connection now. // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically. oServer->ConnectType = SmtpConnectType::ConnectTryTLS; SmtpMail ^oMail = gcnew SmtpMail("TryIt"); oMail->From = gcnew MailAddress("from@adminsystem.com"); oMail->To->Add(gcnew MailAddress("to@adminsystem.com")); oMail->Subject = "test subject"; oMail->TextBody = "test body"; SmtpClient ^oSmtp = gcnew SmtpClient(); oSmtp->SendMail(oServer, oMail); Console::WriteLine("message was sent"); } catch (Exception ^exp) { Console::WriteLine("Exception: {0}", exp->Message); } }

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