Export (0) Print
Expand All

SmtpMail Class

Provides properties and methods for sending messages using the Collaboration Data Objects for Windows 2000 (CDOSYS) message component.

For a list of all members of this type, see SmtpMail Members.

System.Object
   System.Web.Mail.SmtpMail

[Visual Basic]
Public Class SmtpMail
[C#]
public class SmtpMail
[C++]
public __gc class SmtpMail
[JScript]
public class SmtpMail

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

The mail message can be delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server. Types in the System.Web.Mail namespace can be used from ASP.NET or from any managed application.

If the SmtpServer Property is not set, mail is by default queued on a Windows 2000 system, ensuring that the calling program does not block network traffic. If the SmtpServer property is set, the mail is delivered directly to the specified server.

Example

[Visual Basic, C#, C++] The following example can be compiled to a console application that is used to send email from a command line. If you compile the example to a file named MailMessage.exe, use the executable file to send email as follows:

[Visual Basic, C#, C++] MailMessage to@contoso.com from@contoso.com test hello

[Visual Basic] 
Imports System
Imports System.Web.Mail
 
Namespace SendMail
   Public Class usage
      Public Sub DisplayUsage()
         ' Display usage instructions in case of error.
         Console.WriteLine("Usage SendMail.exe <to> <from> <subject> <body>")
         Console.WriteLine("<to> the addresses of the email recipients")
         Console.WriteLine("<from> your email address")
         Console.WriteLine("<subject> subject of your email")
         Console.WriteLine("<body> the text of the email")
         Console.WriteLine("Example:")
         Console.WriteLine("SendMail.exe SomeOne@contoso.com;SomeOther@contoso.com Me@contoso.com Hi hello")
     End Sub
   End Class

   Public Class Start
      '  The main entry point for the application.
      Public Shared Sub Main(ByVal args As String())
         Try
            Try
               Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage()
               Message.To = args(0)
               Message.From = args(1)
               Message.Subject = args(2)
               Message.Body = args(3)
               Try
                  SmtpMail.SmtpServer = "your mail server name goes here"
                  SmtpMail.Send(Message)
               Catch ehttp As System.Web.HttpException
                  Console.WriteLine("0", ehttp.Message)
                  Console.WriteLine("Here is the full error message")
                  Console.Write("0", ehttp.ToString())
               End Try
            Catch e As IndexOutOfRangeException
               ' Display usage instructions if error in arguments.
               Dim use As usage = New usage()
               use.DisplayUsage()
            End Try
         Catch e As System.Exception
            ' Display text of unknown error.
            Console.WriteLine("Unknown Exception occurred 0", e.Message)
            Console.WriteLine("Here is the Full Error Message")
            Console.WriteLine("0", e.ToString())
         End Try
      End Sub
   End Class
End Namespace

[C#] 
using System;
using System.Web.Mail;
 
namespace SendMail
{
   class usage
   {
      public void DisplayUsage()
      {
         Console.WriteLine("Usage SendMail.exe <to> <from> <subject> <body>");
         Console.WriteLine("<to> the addresses of the email recipients");
         Console.WriteLine("<from> your email address");
         Console.WriteLine("<subject> subject of your email");
         Console.WriteLine("<body> the text of the email");
         Console.WriteLine("Example:");
         Console.WriteLine("SendMail.exe SomeOne@Contoso.com;SomeOther@Contoso.com Me@contoso.com Hi hello");
      }
   }
 

   class Start
   {
      // The main entry point for the application.
      [STAThread]
      static void Main(string[] args)
      {
         try
         {
            try
            {
               MailMessage Message = new MailMessage();
               Message.To = args[0];
               Message.From = args[1];
               Message.Subject = args[2];
               Message.Body = args[3];

               try
               {
                  SmtpMail.SmtpServer = "your mail server name goes here";
                  SmtpMail.Send(Message);
               }
               catch(System.Web.HttpException ehttp)
               {
                  Console.WriteLine("{0}", ehttp.Message);
                  Console.WriteLine("Here is the full error message output");
                  Console.Write("{0}", ehttp.ToString());
               }
            }
            catch(IndexOutOfRangeException)
            {
               usage use = new usage();
               use.DisplayUsage();
            }
         }
         catch(System.Exception e)
         {
            Console.WriteLine("Unknown Exception occurred {0}", e.Message);
            Console.WriteLine("Here is the Full Message output");
            Console.WriteLine("{0}", e.ToString());
         }
      }
   }
}

[C++] 
#using <mscorlib.dll>
#using <System.dll>
#using <System.Web.dll>
using namespace System;
using namespace System::Web::Mail;

void DisplayUsage() 
{
   Console::WriteLine(S"Usage SendMail.exe <to> <from> <subject> <body>");
   Console::WriteLine(S"<to> the addresses of the email recipients");
   Console::WriteLine(S"<from> your email address");
   Console::WriteLine(S"<subject> subject of your email");
   Console::WriteLine(S"<body> the text of the email");
   Console::WriteLine(S"Example:");
   Console::WriteLine(S"SendMail.exe SomeOne@Contoso.com;SomeOther@Contoso.com Me@contoso.com Hi hello");
}

int main() 
{
   String* args[] = Environment::GetCommandLineArgs();
   try 
   {
      try 
      {
         MailMessage* Message = new MailMessage();
         Message->To = args[1];
         Message->From = args[2];
         Message->Subject = args[3];
         Message->Body = args[4];

         try 
         {
            SmtpMail::SmtpServer = S"your mail server name goes here";
            SmtpMail::Send(Message);
         } 
         catch (System::Web::HttpException* ehttp) 
         {
            Console::WriteLine(S" {0}", ehttp->Message);
            Console::WriteLine(S"Here is the full error message output");
            Console::Write(S" {0}", ehttp);
         }
      } 
      catch (IndexOutOfRangeException*) 
      {
         DisplayUsage();
      }
   } 
   catch (System::Exception* e) 
   {
      Console::WriteLine(S"Unknown Exception occurred {0}", e->Message);
      Console::WriteLine(S"Here is the Full Message output");
      Console::WriteLine(S" {0}", e);
   }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Web.Mail

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

Assembly: System.Web (in System.Web.dll)

See Also

SmtpMail Members | System.Web.Mail Namespace

Show:
© 2014 Microsoft