Click here to Skip to main content
Click here to Skip to main content

(ASP.NET)Sending mail using SMTP in HTML format using IP settings in web.config file

, 20 Feb 2006
Rate this:
Please Sign up or sign in to vote.
This article briefs you about sending mail using SMTP in HTML Format, which takes the default SMTP settings from web.config file

Introduction

This articles educates you to send mails using SMTP mail server in HTML format. It makes use of default settings from the web.config for sending e-mail.

Source Code


How to set the value in web.config
open the web.config fine either in a VS.NET editor or using anyother text editor.

Add the below code between the <appSettings> Add the below with your IP  </appSettings> tags. Your resultant code looks like this

<appSettings>  

<add key="SmtpServer" value="<Your SMTP IP>"/>

</appSettings>

 

Adding code to the form
Open an application in ASP.NET. Add a button to the web form with the name SendClick

Paste the below code on the button click event

Private Sub SendClick()
        Dim mailMsg As New MailMessage
        Try
            mailMsg.To = "<tomailid>"
            mailMsg.From = "<frommailid>"
            'mailMsg.BodyFormat = MailFormat.Text 'optional for sending text in body
            mailMsg.BodyFormat = MailFormat.Html
            mailMsg.Subject = "Statistics Report"
            mailMsg.Body = "<html><body><Table><tr><td>Hi,</td></tr><tr><td>Details of the Statistics :</td></tr></Table></body></html><html><body>" & "sometext" & _
            "</body></html><html><body><Table><tr><td> </td></tr><tr><td>NOTE: This is an automated mail. Please, do not reply.</td></tr>" & _
            "<tr><td>*Green coloured rows indicates temporary demos</td></tr>" & _
            "<tr><td>**All statistics are based on the page naming conventions Eg., 22_10_2005_</td></tr>" & _
            "<tr><td> </td></tr><tr><td>Regards,</td></tr><tr><td>some text,</td></tr><tr><td>some text,</td></tr>" & _
            "<tr><td> Some text </td></tr></table></body></html>" 
            SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer")
            SmtpMail.Send(mailMsg)
            'xm.InnerHtml = "Your message has been sent"
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

In the above code 'mailMsg' is the name of the object of type MailMessage. We are using this object to set the 'To' address, 'From' address, 'BodyFormat' etc.

We are setting the body format tag to 'MailFormat.HTML' as the body format is of type HTML. We can use 'MailFormat.Text' for the text format. 'Subject' for setting the subject as well as for 'CC' and 'BCC'.

SmtpMail.SmtpServer = ConfigurationSettings.AppSettings("SmtpServer")


In the above line we are setting the ip to the object.

Configurations.AppSettings("SmtpServer") will read the value from the web.config and assigns it dynamically.

SmtpMail.Send(mailMsg)
The above line of code will send the mail......

Happy Mailing..... Smile | :)

 

 

 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

creativesri
Web Developer
United States United States
No Biography provided

Comments and Discussions

 
Questionhow to create mail address box? Pinmemberduraie20-Mar-08 0:33 
AnswerRe: how to create mail address box? PinmemberMember 45300647-May-11 0:59 
GeneralImage Pinmemberalevandro3-Aug-06 4:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 21 Feb 2006
Article Copyright 2005 by creativesri
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid