Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to send an HTML file via VB.NET

Posted on 2010-11-29
14
Medium Priority
?
349 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:Meir Rivkin
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:
Meir Rivkin earned 750 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 750 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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