.NET Framework Namespace
- System.Net.Mail
.NET Framework Classes
MailMessage
MailAddress, MailAddressCollection
SmtpClient, SmtpPermission, SmtpPermissionAttribute
Attachment, AttachmentBase, AttachmentCollection
SmtpException, SmtpFailedRecipientException, SmtpFailedRecipientsException
SendCompletedEventHandler (delegate)
LinkedResource, LinkedResourceCollection
AlternateView, AlternateViewCollection
Sample Code
Here is some code demonstrating the use of these classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
namespace Mail
{
class Program
{
static void Main(string[] args)
{
try
{
MailMessage mailMessage = new MailMessage();
MailAddress myAddress = new MailAddress("cmedcoff@charlesmedcoff.com");
// note the To property is a MailAddressCollection because
// you can send a mail message to several parties
mailMessage.To.Add(myAddress);
// No collection here, only one from
mailMessage.From = myAddress;
mailMessage.Subject = "testing 1, 2, 3";
mailMessage.Body = "this is the body of my mail message";
// or if this needs to be HTML
/// create an HTML based view (including attached image)
AlternateView avHtml =
AlternateView.CreateAlternateViewFromString(
@"<html><body><h1>My Message</h1><br/>
This is the body of my mail message
with an image <img src=\""cid:myimage\"">",
null,
MediaTypeNames.Text.Html);
LinkedResource pic =
new LinkedResource("pic.jpg", MediaTypeNames.Image.Jpeg);
pic.ContentId = "mypiccontentid";
avHtml.LinkedResources.Add(pic);
mailMessage.AlternateViews.Add(avHtml);
// create a plain text view for clients that can't handle HTML
AlternateView avText =
AlternateView.CreateAlternateViewFromString(
"This is the body of of my mail message",
null, MediaTypeNames.Text.Plain);
mailMessage.AlternateViews.Add(avText);
// if we needed to send an attachment as well...
FileStream fs = new FileStream("c:\boot.ini", FileMode.Open, FileAccess.Read);
Attachment myAttachment = new Attachment(fs, "boot.ini", MediaTypeNames.Application.Octet);
// note! the content type is available as a read only prop myAttachment.ContentType;
mailMessage.Attachments.Add(myAttachment);
// send
SmtpClient mailClient = new SmtpClient("mail.charlesmedcoff.com");
// dont' use System.Web.Mail.SmtpMail!!!
// add security
mailClient.EnableSsl = true;
mailClient.Send(mailMessage);
// or to send asynch
mailClient.SendCompleted += new SendCompletedEventHandler(mailClient_SendCompleted);
mailClient.SendAsync(mailMessage);
}
catch (SmtpFailedRecipientsException e)
{
/*
* The exception that is thrown when e-mail is sent using an SmtpClient and
* cannot be delivered to all recipients.
*/
}
catch (SmtpFailedRecipientException e)
{
/*
* Represents the exception that is thrown when the SmtpClient is
* not able to complete a Send or SendAsync operation to a particular recipient.
*/
}
catch (SmtpException e)
{
// Represents the exception that is thrown when the SmtpClient
// is not able to complete a Send or SendAsync operation
}
catch (Exception e)
{
// When all else fails
}
}
static void mailClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
// completed
}
}
}