Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

Page History: Sending Email - .Net Framework

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Wed, Oct 14, 2009, 1:28 PM


The following code demonstrates how to send email. You can convert code between C# and VB.NET at this website.

See also the MailServer Class.

Using a Mail Server

MailMessage message = new MailMessage(sender, recipient, subject, msg);
SmtpClient mySmtpClient = new SmtpClient(mailServer);
mySmtpClient.UseDefaultCredentials = true;
mySmtpClient.Send(message);

Via the local SMTP service hosted within IIS

SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.UseDefaultCredentials = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smtp.Port = 25;
MailMessage msg = new MailMessage("somebody@emailhost.com",
	"somebody@emailhost.com", "My Email Subject", 
	"This is a test of my SMTP service.");

try
{
    smtp.Send(msg);
	MessageBox.Show("Message sent!");
}

The following code shows a slightly different way to do it, this time in Visual Basic.

Imports System.Net
Imports System.Net.Mail

...

Public Sub SendEmail(ByVal recipient As String, ByVal sender As String, ByVal subject As _
String, ByVal messageBody As String, ByVal smtpServer As String)

    Dim smtpClient As New SmtpClient
    smtpClient.Host = smtpServer
    smtpClient.Port = 25
    smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials

    Dim mailMessage As New MailMessage(sender, recipient, subject, messageBody)
    mailMessage.IsBodyHtml = True
    smtpClient.Send(mailMessage)

End Sub

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.