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






1.79/5 (11 votes)
Nov 7, 2005
1 min read

114916

1398
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..... :)