Sending Email using HTML file as Emailbody using Net.Mail.MailMessage... Please help


Hi all
I can send emails with text or html emailbody using asp.net, but cannot find any method or example so far on the web to show how to use an html file as an emailbody. I have tried everything. I know how to do it with the old asp. I have created a html document for example newsletter.html. I want this document to be in the email body when the client opens the email. It must not load from another url. I suppose what i want to do is embed it into the email.
Any help or examples will be appreciated as this is quite urgent for me to implement it for my client. 
 
This is the code im using currently for html mails:
Dim mySmtp As Net.Mail.SmtpClient
Dim msg As New Net.Mail.MailMessage()
msg.from = "me@me.com"
msg.to.Add("you@you.com)
msg.subject ="What ever"
msgBody=<Head><Body> This is html </Body></Head>
msg.IsBodyHtml = True
mySmtp.Send(msg) 
 
 
Thanks 
 
0
wstevens
8/19/2007 10:53:04 PM
📁 asp.net.getting-started
📃 91979 articles.
⭐ 4 followers.

💬 12 Replies
👁️‍🗨️ 2318 Views



See:
http://www.systemnetmail.com/faq/3.1.2.aspx
Jeff

Please:  Don't forget to click "Mark as Answer" on the post that helped you.  That way future readers will know which post solved your issue.
0
jeff
8/20/2007 12:08:12 AM

Hi There,
You could try this ?
http://www.systemnetmail.com/faq/4.8.aspx

DC517
Don't forget to click "Mark as Answer" on the post that helped you.
This credits that member, earns you a point and marks your thread as Resolved.
0
d4dennis
8/20/2007 12:12:33 AM

Hi,
Following examples may help you,
http://www.codeproject.com/useritems/SendMailSMTPWebConfig.asp
http://aspnet.4guysfromrolla.com/articles/080206-1.aspx 

Mehedi Hasan


Mark as answer if the post meets your requirement!
0
M
8/20/2007 5:00:35 AM

Hi!Im using this (it's working fine):
Imports System.Net.Mail
...
Dim MailFrom As MailAddress = New MailAddress("myEmail@mail.com")
Dim MailTo As MailAddress = New MailAddress("yourEmail@mail.com")
Dim Email As MailMessage = New MailMessage(MailFrom, MailTo)
Dim SmtpClient As New SmtpClient 
Email.IsBodyHtml = True
Email.Subject = "test subject"
Email.Body = "test body"
SmtpClient.Port = 25
SmtpClient.Host = "here enter your SMTPHost"
SmtpClient.UseDefaultCredentials = True
SmtpClient.Send(Email)

Greetings Stefan
0
Stefan
8/20/2007 10:14:58 AM

 Hi thanks for the info. It worked well as a stand alone file. My application is configured for authentication. The file that has the code below "emailsender.aspx" is in the same folder as the file "Emailface.aspx" that im html scrapping from. When i run my application i have no errors, however when i check the email sent it has not used the emailface.aspx file but has used the default login page.
I think what is happening is because access to the folder requires user authentication even though im logged in . When the htm scrapping code goes to fetch the file the authentication redirects it to the login page. So my email has a emailbody of the authentication page. If i put code breaks in the emailface.aspx file it does not even get to that point on execution.
If I put the emailface.aspx file in the root directory of the application it stops at the code break point, however I cannot pickup the session variables set in emailsender.asp and pass them to the emailface.aspx page. 
 
Dim mySmtp As New Net.Mail.SmtpClient
        Dim html As String = ScreenScrapeHtml("http://localhost:4395/stanlib.co.za/stanlib/emailface.aspx")
        Dim strFrom As MailAddress = New MailAddress("guestlist@myguestlist.co.za")
        Dim strSubject = EventDescription & " " & FormatDateTime(EventDate, 4) & " " & City
        Dim msg As New Net.Mail.MailMessage()
        msg.From = strFrom
        msg.To.Add(strTo)
        msg.Subject = "html"
        msg.Body = html
        msg.IsBodyHtml = True
        msg.Priority = Net.Mail.MailPriority.High
        msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
        mySmtp.Send(msg)
 Public Function ScreenScrapeHtml(ByVal url As String) As String
        'Response.Redirect("~/stanlib/emailface.aspx")
        Dim objRequest As WebRequest = System.Net.FileWebRequest.Create(url)
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()

        sr.Close()
        Return result
    End Function 'ScreenScrapeHtml
 
0
wstevens
8/20/2007 10:30:33 AM

What is the content from your emailface.aspx file? Must it be a *.aspx file?
I had a similar problem that I solved with "HTML-Templates".
First I grab the Code from the file and then I replace my "placeholders" (like ##NAME##) with the databased text.
It's working fine!
0
Stefan
8/20/2007 10:56:12 AM

 
Hi Stefan
 
The .aspx file has dynamic dat on it . eg detailsview's > unfortunatly it must be aspx.
 
Thanks for your help
 
 
0
wstevens
8/20/2007 11:13:05 AM

hi Stefan can u send me an example or see if you can see the fault in my code below
 Sub
Dim mySmtp As New Net.Mail.SmtpClient
        Dim html As String = ScreenScrapeHtml("http://localhost:4395/stanlib.co.za/stanlib/emailface.aspx")
        Dim strFrom As MailAddress = New MailAddress("guestlist@myguestlist.co.za")
        Dim strSubject = EventDescription & " " & FormatDateTime(EventDate, 4) & " " & City
        Dim msg As New Net.Mail.MailMessage()
        msg.From = strFrom
        msg.To.Add(strTo)
        msg.Subject = "html"
        msg.Body = html
        msg.IsBodyHtml = True
        msg.Priority = Net.Mail.MailPriority.High
        msg.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
        mySmtp.Send(msg)
end 
 Public Function ScreenScrapeHtml(ByVal url As String) As String
        'Response.Redirect("~/stanlib/emailface.aspx")
        Dim objRequest As WebRequest = System.Net.FileWebRequest.Create(url)
        Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
        Dim result As String = sr.ReadToEnd()
        sr.Close()
        Return result
    End Function 'ScreenScrapeHtml
0
wstevens
8/20/2007 12:10:59 PM

Is the Email sent?
Or do you have the problem with the grabbing?
0
Stefan
8/20/2007 12:52:51 PM

 Hi Stefan
The page is grabbed and the email is snet. I canopen it up in my iis queued folder. except it grabs the login.aspx instead of the emailface.aspx. It is not authenticating when it tries to access emailface.aspx thats why it being redirected to the login.aspx
 
0
wstevens
8/20/2007 1:10:40 PM


 Sorry, I dont know why you are redirected. Are you using sessions for authentification?
 Maybe you try another way of grabbing:
Imports System.Web.Security
'STEP 1: Create a WebClient instance
Dim objWebClient As New WebClient()
'STEP 2: Call the DownloadedData method
Const strURL As String = http://localhost:4395/stanlib.co.za/stanlib/emailface.aspx
Dim aRequestedHTML() As Byte
aRequestedHTML = objWebClient.DownloadData(strURL)
'STEP 3: Convert the Byte array into a String
Dim objUTF8 As New UTF8Encoding()
Dim strRequestedHTML As String
strRequestedHTML = objUTF8.GetString(aRequestedHTML)
return strRequestedHTML
Much luck!!
0
Stefan
8/20/2007 1:26:23 PM


Hope this helps,


 

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net.Mail" %>