Welcome to Strong Coders Community | | Help

Blog ( Of Ryan Olshan)

News

  • About Ryan Olshan

    Join StrongTypes - Free Generics Help By Email
    Join StrongTypes

    Community Credit Winner

    View my MVP profile

    The posts on this weblog are provided "as is" with no warranties and confer no rights. The opinions expressed herin are my own personal opinions and do not represent my employer's view in any way.

    Subscribe to Rolog via My AOL
    Subscribe to Rolog via Bloglines


    Subscribe to Rolog via Feedburner

    Furl Rolog




    Subscribe to Rolog via Newsburst


    Subscribe to Rolog via Pluck

    Add Rolog to Technorati Favorites




    Thycotic Secret Server

.NET Websites

  • => StrongTypes.com <=
  • Community Credit
  • DotNetCharity.com
  • DotNetSlackers
  • EasyListBox
  • SystemNetMail.com
  • SystemWebMail.com

Blogroll

  • Alex Campbell
  • Carlos Magalhaes
  • Chris Taylor
  • Darren Neimke
  • David Betz
  • Eric Smith
  • Rob Caron
  • Rob Howard
  • Sam Gentile
  • Scott Guthrie
  • Scott Mitchell
  • Tess
  • Wesner Moise
  • Jason Alexander
  • Bill Ryan
  • Steve Smith
  • Sahil Malik
  • David Silverlight

Other Websites

  • 911 Pigeon Alert
  • Pacific Wildlife Project

Strong Coder Bloggers

  • Dean Fiala
  • Kevin Jones

Thycotic Bloggers

  • Bryant Smith
  • John Morales
  • Jonathan Cogley

Yahoo! Groups

  • => StrongTypes <=
  • AspNet2
  • AspNet20GridView
  • PayPalDevDotNet
  • RubyOnRails
  • VSTS

Sending an email using System.Net.Mail

Below is a C# and VB.NET class that demonstrates using System.Net.Mail to send an email.

Download C# System.Net.Mail Helper
Download VB.NET System.Net.Mail Helper

Calling the function from code

MailHelper .SendMailMessage( "" , "" , "" , "" , "Sample Subject" , "Sample body of text for mail message" )

MailHelper.cs

using System.Net.Mail;

 

public class MailHelper

{

   ///

   /// Sends an mail message

   ///

   /// Sender address

   /// Recepient address

   /// Bcc recepient

   /// Cc recepient

   /// Subject of mail message

   /// Body of mail message

   public static void SendMailMessage( string from, string to, string bcc, string cc, string subject, string body)

   {

      // Instantiate a new instance of MailMessage

      MailMessage mailMessage = new MailMessage ();

 

      // Set the sender address of the mail message

      mailMessage.From = new MailAddress (from);

      // Set the recepient address of the mail message

      mailMessage.To.Add( new MailAddress (to));

 

      // Check if the bcc value is null or an empty string

      if (! string .IsNullOrEmpty(bcc))

      {

        // Set the Bcc address of the mail message

        mailMessage.Bcc.Add( new MailAddress (bcc));

      }

 

      // Check if the cc value is null or an empty value

      if (! string .IsNullOrEmpty(cc))

      {

        // Set the CC address of the mail message

        mailMessage.CC.Add( new MailAddress (cc));

      }

 

      // Set the subject of the mail message

      mailMessage.Subject = subject;

      // Set the body of the mail message

      mailMessage.Body = body;

 

      // Set the format of the mail message body as HTML

      mailMessage.IsBodyHtml = true ;

      // Set the priority of the mail message to normal

      mailMessage.Priority = MailPriority .Normal;

 

      // Instantiate a new instance of SmtpClient

      SmtpClient smtpClient = new SmtpClient ();

      // Send the mail message

      smtpClient.Send(mailMessage);

   }

}

MailHelper.vb

Imports System.Net.Mail

 

Public Class MailHelper

    '''

    ''' Sends an mail message

    '''

    ''' Sender address

    ''' Recepient address

    ''' Bcc recepient

    ''' Cc recepient

    ''' Subject of mail message

    ''' Body of mail message

    Public Shared Sub SendMailMessage( ByVal from As String , ByVal recepient As String , ByVal bcc As String , ByVal cc As String , ByVal subject As String , ByVal body As String )

        ' Instantiate a new instance of MailMessage

        Dim mailMessage As New MailMessage()

 

        ' Set the sender address of the mail message

        mailMessage.From = New MailAddress(from)

        ' Set the recepient address of the mail message

        mailMessage.To.Add( New MailAddress(recepient))

        ' Check if the bcc value is nothing or an empty string

 

        If Not String .IsNullOrEmpty(bcc) Then

            ' Set the Bcc address of the mail message

            mailMessage.Bcc.Add( New MailAddress(bcc))

        End If

 

        ' Check if the cc value is nothing or an empty value

        If Not String .IsNullOrEmpty(cc) Then

            ' Set the CC address of the mail message

            mailMessage.CC.Add( New MailAddress(cc))

        End If

 

        ' Set the subject of the mail message

        mailMessage.Subject = subject

        ' Set the body of the mail message

        mailMessage.Body = body

 

        ' Set the format of the mail message body as HTML

        mailMessage.IsBodyHtml = True

        ' Set the priority of the mail message to normal

        mailMessage.Priority = MailPriority.Normal

 

        ' Instantiate a new instance of SmtpClient

        Dim smtpClient As New SmtpClient()

        ' Send the mail message

        smtpClient.Send(mailMessage)

    End Sub

End Class

Web.config

xml version = " 1.0 " ?>

< configuration >

  < system.net >

    < mailSettings >

      < smtp from = " " >

        < network host = " smtp.yourdomain.com " port = " 25 " userName = " yourUserName " password = " yourPassword " />

      smtp >

    mailSettings >

  system.net >

configuration >

Share this post: | | | | |
Posted: Monday, March 13, 2006 11:20 PMby Ryan Olshan
Filed under: ,

Comments

Dom said:

I just wish MS would support VB fans as much as they do c#. On the MailMessage class, there is no code sample in VB. I am pleased you provided one!

March 19, 2006 5:54 AM
New Comments to this post are disabled