In this article I am sending Web form as E-mail. Whatever you placed on the form it will be send as a e-mail.
Firstly, create an account in Gmail and fill password and Id in mailAuthentication parameters. I m attaching a receipt images as an example which I have send as E-mail to clients after fetch the record from database.
Hope it will help you.
Imports System.Data
Imports System.IO
Imports System.Data.SqlClient
Partial Class default
Inherits System.Web.UI.Page
Private Sub webformasemail() ‘call this method where u need
‘Declare the following variables
Dim objMM As New MailMessage()
Dim SB As New StringBuilder()
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)
Me.Page.RenderControl(htmlTW) ‘ it will render the default web form page
Dim webform As String = SB.ToString()
Try
Dim mail As New System.Net.Mail.MailMessage(“fromemailid”,”toemailid”, “Subject”,webform )
‘Proper Authentication Details need to be passed when sending email from gmail
Dim mailAuthentication As New System.Net.NetworkCredential(“ursid”, “urpassword”)
‘Smtp Mail server of Gmail is “smpt.gmail.com” and its port no. 587
Dim mailClient As New System.Net.Mail.SmtpClient(“smtp.gmail.com”, 587)
‘Enable SSL
‘Gmail is SSL certify so that why we need to set Enabelssl=true
mailClient.EnableSsl = True
mailClient.UseDefaultCredentials = False
mailClient.Credentials = mailAuthentication
mail.IsBodyHtml = True ‘ It will render the whole page before send the email
mailClient.Send(mail)
Catch ex As Exception
Labe1.text=” Unable to send the message”
End Try
response.redirect(“e_mailsent.aspx”) ‘Or you can use the VerifyRenderingInServerForm to avoid Error.
End class
Public Overloads Overrides Sub VerifyRenderingInServerForm ByVal control As Control)
‘ Verifies that the control is rendered
End Sub
*Note: Error:
[Control 'XXXXXX' of type 'XXXXXX' must be placed inside a form tag with runat=server.]
to avoid above mentioned error use the [VerifyRenderingInServerForm] code.
With Regards
Vik



Recent Comments