Smtp.NET Simple Email Sending
[C#]
public void SendEmail()
{
SmtpServer oSmtpDotNet = new SmtpServer();
// Set the remote server name. If left blank, "localhost" is used.
oSmtpDotNet.ServerAddress = "mail.domain.com"
// Message Addressing
oSmtpDotNet.ToAddress = "to@domain.com";
oSmtpDotNet.FromAddress = "from@domain.com";
// Set the message subject and body
oSmtpDotNet.Body = "This is the message";
oSmtpDotNet.Subject = "Routine email";
// Force the message body to be html
oSmtpDotNet.BodyFormat = (int)BodyFormatTypes.Html;
// ** OR **
// Set the message body to the contents (html) found
// at http://www.ExclamationSoft.com
oSmtpDotNet.BodyUrl = "http://www.ExclamationSoft.com";
// ** OR **
// Set the message body to the contents (html) found
// in the file c:\temp\page.html
oSmtpDotNet.BodyFile = "c:\\temp\\page.html";
// AND, force the message body to be encoded as HTML
oSmtpDotNet.BodyFileFormat = (int)BodyFormatTypes.Html;
int nRC = oSmtpDotNet.Send();
if ( nRC != (int)ReturnCodes.SUCCESS )
{
Console.WriteLine("Error #"+nRC+" occurred.");
Console.WriteLine(oSmtpDotNet.LastError);
return;
}
// Success!
Console.WriteLine("Success!");
}
[VB]
Public Function SendEmail() As Integer
Dim oSmtpDotNet As New SmtpServer()
Dim nRC As Integer
' Set the remote server name. If left blank, "localhost" is used.
oSmtpDotNet.ServerAddress = "mail.domain.com"
' Message Addressing
oSmtpDotNet.FromAddress = "from@domain.com"
oSmtpDotNet.ToAddress = "to@domain.com"
' Set the message subject and body
oSmtpDotNet.Subject = "Routine email"
oSmtpDotNet.Body = "This is a briefing of the financials"
' Force the message body to be html
oSmtpDotNet.BodyFormat = BodyFormatTypes.Html
' ** OR **
' Set the message body to the contents (html) found
' at http://www.ExclamationSoft.com
oSmtpDotNet.BodyUrl = "http://www.ExclamationSoft.com"
' ** OR **
' Set the message body to the contents (html) found
' in the file c:\temp\page.html
oSmtpDotNet.BodyFile = "c:\\temp\\page.html"
' AND, force the message body to be encoded as HTML
oSmtpDotNet.BodyFileFormat = BodyFormatTypes.Html
' Send the message. Get the return code and store it in the variable
' nRC. Then check nRC for success or failure
nRC = oSmtpDotNet.Send()
If (nRC <> ReturnCodes.SUCCESS) Then
' A problem occurred.
' Write out the last error encountered
Console.WriteLine(oSmtpDotNet.LastError)
Exit Function
End If
' Success!
Console.WriteLine("Message Sent!")
End Function
|