Sending Contents Of Local HTML File As Email Body

Last post 04-16-2009 6:17 PM by kwanann. 1 replies.

Sort Posts:

  • Sending Contents Of Local HTML File As Email Body

    04-16-2009, 6:00 PM
    • Member
      158 point Member
    • kevorkian
    • Member since 06-16-2008, 4:03 PM
    • Posts 251

    I am trying to create a mailing list type application where a user will make a selection from a dropdown list and pastes the list od email addresses in a multiline text box. The list selected value contains the name of an html file.  I want a way to be able to open the html file and read its contents to send as the body of an email.  (i need everything from start to bottom as CSS styles in the header tags must be included).

    Here is what I have so far but this isn't working - does anyone know how to read in the contents of an html file?

     

    Dim email_list As String = Replace(txt_EmailAddresses.Text, vbCr, ";")
            If Right(email_list, 1) = ";" Then email_list = Left(email_list, (email_list.Length - 1))
    
            Dim TemplatePath As String = String.Format("templates/{0}/{1}.html", list_template.SelectedValue, list_template.SelectedValue)
    
            Dim objRequest As WebRequest = System.Net.HttpWebRequest.Create(TemplatePath) 'Invalid URI: The format of the URI could not be determined.
            Dim sr As New StreamReader(objRequest.GetResponse().GetResponseStream())
            Dim EmailBody As String = sr.ReadToEnd()
            sr.Close()
    
    Dim MailMSG As New MailMessage()
    
            MailMSG.To.Add(email_list)
            MailMSG.From = New MailAddress("admin@mysite.com")
            MailMSG.Subject = "Test Subject"
            MailMSG.Body = EmailBody 
            MailMSG.IsBodyHtml = True
            Dim smtp As New SmtpClient("127.0.0.1")
            smtp.Send(MailMSG)
     
  • Re: Sending Contents Of Local HTML File As Email Body

    04-16-2009, 6:17 PM
    Answer
    • Contributor
      2,537 point Contributor
    • kwanann
    • Member since 02-05-2009, 7:59 AM
    • SG
    • Posts 497
    • TrustedFriends-MVPs

     Hi,

     Actually the error pretty much says it all, the format of the URL is invalid.

    If you look at the code, the url is just templates/.../....html which cannot be resolved because there is no domain..

    alternatively instead of usnig httpwebrequest, since the file is on the server, why not just use streamreader (note the code is in c#, but it is pretty much the same in vb)?

     (using streamreader sr = new streamreader(server.mappath(string.format("~/templates/{0}/{1}.html", list_template.SelectedValue, list_template.SelectedValue))
    {

       EmailBody = sr.ReadToEnd();

    }

    MVP ASP.NET C#
    View my blog @ http://jefferytay.wordpress.com
Page 1 of 1 (2 items)