How to send an email using .NET

Discussion in 'Programming' started by largn81, Jan 26, 2008.

  1. #1
    Many .NET applications will require an email be sent out for various reasons. This article will give a quick examples on how to send an email using VB.NET. The examples given can easily be translated into C#.NET.:)

    Storing Email Credentials:p
    If you are developing a web application the best place to store your email credentials is in your web.config file's <appSettings>. This will allow you to secure your email credentials using encryption and also provide a single location for all of your code to access the credentials.


    Code: ( xml )
    <configuration>
    <appSettings>
    <add key="EmailUserName" value="userName"/>
    <add key="EmailPassword" value="password"/>
    </appSettings>
    </configuration>



    If you are using .NET Framework version 1.1
    You will have to import System.Web.Mail
    This Framework will not easily allow you to supply email credentials that allow you to connect to your mail server. I will not cover how to do this in this article.


    Code: ( vbnet )
    Dim mailMsg As Mail.MailMessage
    Dim body As new String = "This will be the body of my email message"

    mailMsg= New MailMessage
    mailMsg.To= "to@blabla.com"
    mailMsg.From= "from@blabla.com"
    mailMsg.Subject = "The subject of the email"
    mailMsg.BodyFormat = MailFormat.Html
    mailMsg.Body = body

    SmtpMail.SmtpServer="localhost"

    Try
    SmtpMail.Send(mailMsg)
    Catch ex As Exception

    End Try



    If you are using .NET Framework version 2.0
    You will have to import System.Net.Mail.MailMessage and if you need to provide email credentials in order to connect to your mail server System.Net.NetworkCredentials

    Please note that the following code snippet shows how to retrieve your user credentials from the web.config file. If you are not storing your credentials in this file you can simply add strings for the user name and password instead of using ConfigureationManager.AppSettings(...) to supply your credentials.

    Also if you do not have to supply user credentials in order to connect to your mail provider you can ignore anything that has to do with credentials in this example


    Code: ( vbnet )
    Try
    Dim emailTitle As String ="My Email Title"

    Dim emailMessage As Net.Mail.MailMessage

    Dim body As String = "This will appear in the body of my email"

    emailMessage = New Net.Mail.MailMessage("from@emailAddress.com", "to@emailAddress.com", emailTitle, body)

    Dim mailClient As New Net.Mail.SmtpClient("urlOfEmailService.com", 25)
    '25 is the port on which the mail server is listening. If your mail server
    'runs on the default port this number does not need to be provided


    'If you do not need to provide credentials to connect to the mail server you can ignore the next 3 lines
    Dim myCredentials As New System.Net.NetworkCredential(System.Configuration. ConfigurationManager.AppSettings("EmailUserName"), System.Configuration.ConfigurationManager.AppSetti ngs("EmailPassword"))
    mailClient.UseDefaultCredentials = False
    mailClient.Credentials = myCredentials

    mailClient.Send(emailMessage)

    Catch ex As Exception

    End Try


    I hope this has helped you!
    :)
     
    largn81, Jan 26, 2008 IP