Solved

How to send an HTML file via VB.NET

Posted on 2010-11-29
14
327 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Comment

by:mdreed
Comment Utility
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
Comment Utility
Does your HTML file already contain embeded images or does it contain img tags?
0
 

Author Comment

by:mdreed
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Something not as expected technically does not mean the answer is not right!
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

744 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

11 Experts available now in Live!

Get 1:1 Help Now