Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to send an HTML file via VB.NET

Posted on 2010-11-29
14
331 Views
Last Modified: 2012-05-10
I have seen a number of examples of how to construct a simple HTML message body when sending an e-mail via VB.NET.   If the intended message is constructed elsewhere and saved as an HTML file, how can I send the file contents as the message body?

What I have thus far is:
       Dim credential As New System.Net.NetworkCredential("creds@mysite.com", "mypw")
        Dim client As New SmtpClient()
        client.Host = "smtp.mysite.com"
        client.Credentials = credential
        Dim email As New MailMessage
        email.From = New MailAddress("MyFrom@FromEmail.com")
        email.To.Add("ToAddr@ToEmail.com")
        email.Subject = "My Subject"
        email.IsBodyHtml = True
        email.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
        StrMailBody = <I need to specify an HTML file string here>
        email.Body = StrMailBody
        client.Send(email)

The code works fine with a simple text entry for email.body, but I need to figure out how to send the contents of an HTML file.
0
Comment
Question by:mdreed
  • 6
  • 6
  • 2
14 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 34230185
use html elements in your email.Body to get the effects you want.
for example:
email.Body = "<b>Technical Support" + Chr(13) + "Company Name Ltd" + Chr(13) + "Mailto:support@company.com" + Chr(13) + "http://url" + Chr(13) + _
                               "marketing blurb" + _
                               "more marketing blurb</b>"

check
http://www.dreamincode.net/forums/topic/50500-html-body-in-email/
http://snipplr.com/view/17383/send-html-email/
0
 
LVL 42

Accepted Solution

by:
sedgwick earned 250 total points
ID: 34230199
if the intended message is constructed elsewhere and saved as an HTML file, you can read the html file and attach the content to the email.Body:

email.Body = File.ReadAllText("c:\temp\myfile.html")
0
 

Author Comment

by:mdreed
ID: 34230486
sedqwick:
Your code works fine, except the file I saved as HTML lost its embedded images.  How do I correct the omission?
Thanks.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34233229
0
 

Author Comment

by:mdreed
ID: 34233665
CC:

All of the examples in the links you provided I have reviewed already, and they all show the HTML body text in code.  What I need/want to do is to create a document by some other means (like MSWord), save the document as HTML, and then send that file (the contents including any images) as an e-mail from VB.NET.

I have successfully sent the HTML file as outlined by sedqwick above, but I can't figure out how to retain the embedded images.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34233681
Does your HTML file already contain embeded images or does it contain img tags?
0
 

Author Comment

by:mdreed
ID: 34233715
CC:

It contains something like the following:

<v:imagedata src="SQL%20Express%20Setup_files/image001.jpg" o:title="Image1"/>
</v:shape><![endif]--><![if !vml]><img width=547 height=85
src="SQL%20Express%20Setup_files/image002.jpg" v:shapes="_x0000_i1025"><![endif]></p>
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34233744
That does not look HTML to me. In that case, I think you would have to parse the HTML in your program, making a list of all the images used, and replacing them with embeded images 1 by 1.
0
 

Author Comment

by:mdreed
ID: 34233776
I took a Word document and saved as HTML and got what I sent in my last post.  There is obviously much more, but that is the part about the image reference.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34233816
That is XML. Why do you need the email to be composed separately. Can it not be composed in your application?
0
 

Author Comment

by:mdreed
ID: 34233875
The app will be used for advertising, where an ad person would create a document with images, text, etc.  Then, select all of the e-mail recipients who will get the message.  That is why I am looking at creating an ad file and then sending it.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 34233918
Another option is to export the document as an image and then using that image in the email body.
0
 

Author Closing Comment

by:mdreed
ID: 34233977
Thanks to sedqwick for the html file solution.  As for the embedded image issue, I may have to convert the document to an image as suggested by CodeCruiser.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34234122
Something not as expected technically does not mean the answer is not right!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.Net - KeyPress Event 4 38
VB.NET 2008 Publish Error 2 33
VB.NET 2008 - SQL Timeout 9 34
VS 2015 is giving [No relevant source lines] 2 26
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question