Is there a way to insert an existing htm page into body of a mailmessage?

M

Mark Sandfox

I have created a newsletter that I want to send to a list of 200 users (yes
they all have requested it). I know this can be created line for line using
htmlTextWriter but that is cumbersome at best. Is there a way to simply
insert the page via referencing it into the body of the email. I tried the
below but it did not work.

<Script runat="Server">
Sub Submit_Click ( s as Object, e as EventArgs)
if page.isvalid = True then
SmtpMail.SmtpServer = "mail.****.com"
dim objMailMessage as MailMessage

objMailMessage = New MailMessage
objMailMessage.From = ***@****.com
objMailMessage.To = ***@****.com
objMailMessage.Subject = "Newsletter"
objMailMessage.Body = http://www.****.com/Newsletter/CurrentNewsletter.htm
objMailMessage.BodyFormat = MailFormat.HTML

SmtpMail.Send( objMailMessage )
Response.Redirect( "thank_you_cu.htm" )
end if
End Sub
</script>
 
G

Guest

No. The MailMessage Body property is a string. Your code should not even compile unless you set this property to a string.

If you can try readin the file content as text into a string and assign it to the body property.

Also, look into the Attachments property of MailMessage. You should be able to attach the HTML file. Not sure if the client's email software will show the attachment or will force the user to save/open the attachment.

HTH,
Suresh.
 
M

Mark Sandfox

Actually you pointed me in the right direction. USing a nice little bit of
code i found called readHTMLPage the following code works perfectly. The
only thin to add now is the database connectivity for the email list, which
is a no brainer. Thank you for your help.

<script language="VB" runat="server">

Dim strNewsletter as string

Sub Submit_Click ( s as Object, e as EventArgs)
if page.isvalid = True then
SmtpMail.SmtpServer = "mail.****.com"
dim objMailMessage as MailMessage
dim swHtmlBody as StringWriter
dim twTextWriter as HtmlTextWriter

swHtmlBody = New StringWriter
twTextWriter = New HtmlTextWriter(swHtmlBody)


objMailMessage = New MailMessage
objMailMessage.From = "mailto:****@****.com"
objMailMessage.To = "mailto:****@****.com"
objMailMessage.Subject = "Great Ideas Newsletter from AP Temps"
objMailMessage.Body = strNewsletter
objMailMessage.BodyFormat = MailFormat.HTML
objMailMessage.UrlContentLocation = "http://www.****.com"

SmtpMail.Send( objMailMessage )
Response.Redirect( "thank_you_cu.htm" )
end if
End Sub


Sub Page_Load(Src As Object, E As EventArgs)
strNewsletter = readHtmlPage("http://www.*****.com/current_newsletter.htm")
End Sub

Function readHtmlPage(url As String) As String
Dim objResponse As WebResponse
Dim objRequest As WebRequest
Dim result As String

objRequest = System.Net.HttpWebRequest.Create(url)
objResponse = objRequest.GetResponse()
Dim sr As New StreamReader(objResponse.GetResponseStream())
result = sr.ReadToEnd()

'clean up StreamReader
sr.Close()

return result
End Function

</script>


Suresh said:
No. The MailMessage Body property is a string. Your code should not even
compile unless you set this property to a string.
If you can try readin the file content as text into a string and assign it to the body property.

Also, look into the Attachments property of MailMessage. You should be
able to attach the HTML file. Not sure if the client's email software will
show the attachment or will force the user to save/open the attachment.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top