Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to send an HTML file via VB.NET

Posted on 2010-11-29
14
Medium Priority
?
341 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
[X]
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
  • 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

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…
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

721 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