MailMessage Constructor (String, String)
.NET Framework 4
Initializes a new instance of the MailMessage class by using the specified String class objects.
Assembly: System (in System.dll)
Parameters
- from
- Type: System.String
A String that contains the address of the sender of the e-mail message.
- to
- Type: System.String
A String that contains the addresses of the recipients of the e-mail message.
| Exception | Condition |
|---|---|
| ArgumentNullException |
from is null. -or- to is null. |
| ArgumentException |
from is Empty (""). -or- to is Empty (""). |
| FormatException |
from or to is malformed. |
The following code example demonstrates calling this constructor.
public static void CreateTestMessage2(string server) { string to = "jane@contoso.com"; string from = "ben@contoso.com"; MailMessage message = new MailMessage(from, to); message.Subject = "Using the new SMTP client."; message.Body = @"Using this new feature, you can send an e-mail message from an application very easily."; SmtpClient client = new SmtpClient(server); // Credentials are necessary if the server requires the client // to authenticate before it will send e-mail on the client's behalf. client.UseDefaultCredentials = true; try { client.Send(message); } catch (Exception ex) { Console.WriteLine("Exception caught in CreateTestMessage2(): {0}", ex.ToString() ); } }
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Sample using PowerShell
<#
.SYNOPSIS
This script creates and sends an email using SMTP.
.DESCRIPTION
This script firt creates a mail message, populates it
with subject and body, creates an SMTP client that
sends the email message. Any errors are captured. If
everythign works there is no output
.NOTES
File Name : get-autohelp.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted to:
http://msdn.microsoft.com/en-us/library/14k9fb7t.aspx
.EXAMPLE
Left as an exercise to the reader
#>
$to = "jane@contoso.com"
$from = "ben@contoso.com"
$message = new-object system.Net.Mail.MailMessage $from, $to
$message.Subject = "Using the new SMTP client.";
$message.Body = "Using this new feature, you can send an e-mail message from an application very easily."
$server = "Cookham8"
$client = New-Object System.Net.Mail.SmtpClient $server
$client.UseDefaultCredentials = $true
try {
$client.Send($message);
}
catch {
"Exception caught in New-TestMessage2: {0}" -f $Error[0]
}
- 9/24/2010
- Thomas Lee
Sample Using PowerShell
<#
.SYNOPSIS
This script creates and sends an email using SMTP.
.DESCRIPTION
This script firt creates a mail message, populates it
with subject and body, creates an SMTP client that
sends the email message. Any errors are captured. If
everythign works there is no output
.NOTES
File Name : get-autohelp.ps1
Author : Thomas Lee - tfl@psp.co.uk
Requires : PowerShell Version 2.0
.LINK
This script posted to:
http://www.pshscripts.blogspot.com
MSDN sample posted to:
http://msdn.microsoft.com/en-us/library/14k9fb7t.aspx
.EXAMPLE
Left as an exercise to the reader
#>
$to = "jane@contoso.com"
$from = "ben@contoso.com"
$message = new-object system.Net.Mail.MailMessage $from, $to
$message.Subject = "Using the new SMTP client.";
$message.Body = "Using this new feature, you can send an e-mail message from an application very easily."
$server = "Cookham8"
$client = New-Object System.Net.Mail.SmtpClient $server
$client.UseDefaultCredentials = $true
try {
$client.Send($message);
}
catch {
"Exception caught in New-TestMessage2: {0}" -f $Error[0]
}
- 9/23/2010
- Thomas Lee
Addresses in "to" parameter must be separated by commas
If we separate the addresses in the to parameter with semi-colons (like we have to do in Outlook), we get:
The specified string is not in the form required for an e-mail address.
[tfl 23 01 2010]
This is correct behaviour and by design - the address strings are delimited by commas not semi-colons.
The specified string is not in the form required for an e-mail address.
[tfl 23 01 2010]
This is correct behaviour and by design - the address strings are delimited by commas not semi-colons.
- 8/25/2010
- Marc Schluper
- 9/23/2010
- Thomas Lee