Solved

How to send an HTML file via VB.NET

Posted on 2010-11-29
14
330 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now