Click here to Skip to main content
15,884,177 members
Articles / Web Development / ASP.NET
Article

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

Rate me:
Please Sign up or sign in to vote.
1.79/5 (12 votes)
20 Feb 20061 min read 114.3K   1.4K   24   3
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..... :)

 

 

 

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to create mail address box? Pin
duraie19-Mar-08 23:33
duraie19-Mar-08 23:33 
AnswerRe: how to create mail address box? Pin
Abhijat Chauhan6-May-11 23:59
Abhijat Chauhan6-May-11 23:59 
GeneralImage Pin
alevandro3-Aug-06 3:25
alevandro3-Aug-06 3:25 
Very good...
But I like insert to image in this HTML mail, it's possible??

Thank's

Geraldo Analista de Sistemas

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

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