Solved

Sending an email with VB.net

Posted on 2013-01-28
10
472 Views
Last Modified: 2013-01-30
I'm using Web devloper Express 2010 and VB.net to create a small application, I have successfully create the procedures and also the code fro sending the email. So now I want to format it into a nice looking email, rather than the bare bones I have just now.
I have created a formatted HTML document for the email and will need to pass some values into from strings that I will have defined.

What is the best way to do this ?


 
Protected Sub SendMailResource(ByVal strEmailAddress As String)
        Dim mail As New MailMessage()
        'set the addresses
        mail.From = New MailAddress("test@test.com")
        mail.To.Add("test1.test.com")
        mail.To.Add(strEmailAddress)
        'set the content
        mail.Subject = "Task Awaiting"
        mail.Body = "This is a test, for sending an email automatically to a resource"
        mail.IsBodyHtml = True
        'send the message
        Dim smtp As New SmtpClient("server1")
        smtp.Send(mail)
    End Sub

Open in new window

0
Comment
Question by:ndr-itsolutions
10 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 38827589
I am using a StringBuilder in one of my apps. There may be a better way to build the body, but the previous programmer did it like this and it works.

Dim mailSubject As String = "test Subject"
Dim message As String = String.Empty
Dim messageBuilder As New StringBuilder

messageBuilder.Append("<span style = 'FONT-FAMILY: Arial,sans-serif; FONT-SIZE: 11pt;'>")
messageBuilder.Append("Note:")
messageBuilder.Append("<p>")
messageBuilder.Append("The following request has been rejected:</b>")
messageBuilder.Append("<p>")
         'email
        Dim Mail As New MailMessage()

        Mail.From = New MailAddress("emailAdress")
        Mail.To.Add("emailAdress")

        'set the content
        Mail.Subject = mailSubject
        Mail.IsBodyHtml = True
        Mail.Body = message
0
 
LVL 6

Accepted Solution

by:
esolve earned 500 total points
ID: 38827881
The easiest option is to create a resource file.

If you r-click you project. Select Add Component and add a resource file (.resx)

Then also add an HTML page which will be the body of your email to the project eg. MailTemplate.html.

If you open up the .resx file you will see a dropdown in the left top from where you can select resource of type "file". With this selected you can drag your .html file onto the resource file. In the properties window you can then select to "include file in assembly" or something similar.

Then this .html file can be access via the resources file and certain strings can be replaced via code eg. emailfrom etc

I usually do add strings like this

[Name]
[Company]
[EmailFrom]

which can then easily be replaced

String.Replace("[EmailFrom]", "client@company.com")

http://msdn.microsoft.com/en-us/library/gg418542.aspx
http://support.microsoft.com/kb/319291
http://stackoverflow.com/questions/5639582/how-do-i-get-resource-file-values-in-visual-basic
0
 

Author Comment

by:ndr-itsolutions
ID: 38828284
So how would I modify my existing script to call that res file ?

Thanks

Nick
0
 
LVL 6

Expert Comment

by:esolve
ID: 38828618
If you want to call the resource file use the following:

Where your .resx file was called Resource1 like this.


        Dim sEmailBody As String
        sEmailBody = My.Resources.Resource1.HtmlPage1

        'Code to replace strings

        Mail.Subject = mailSubject
        Mail.IsBodyHtml = True
        Mail.Body = sEmailBody
0
 

Author Comment

by:ndr-itsolutions
ID: 38833126
I'm just about there, one last query, where do I add the code to replace the strings I need ? So for instance, I have a string being called from a text box:

 strComponent = ComponentDropDown.Text

So in the email I would have  Component: [Component] etc etc
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:ndr-itsolutions
ID: 38833168
Sorry, did not read that entire message, I can see where to place the text.
0
 

Author Comment

by:ndr-itsolutions
ID: 38833223
I cannot get the strings to replace, have tried using the following:

 sEmailBody = My.Resources.Resource1.HTMLPage1

        sEmailBody.Replace("[Component]", strComponent)

Open in new window

0
 
LVL 10

Expert Comment

by:Monica P
ID: 38834022
u can format the mail body content like this too

 Body = ""
        Body = Body + vbCrLf + "<HTML>"
        Body = Body + vbCrLf + "<head>"
        Body = Body + vbCrLf + "<title>Register Confirmation</title>"
        Body = Body + "<body><br>"
        Body = Body + "<img src='http://www.test.com/logo.jpg'>"
        Body = Body + vbCrLf + "<br><br>"
        Body = Body + vbCrLf + "<P align=left>Dear " + Session_UserInfo.Fname + " " + Session_UserInfo.Lname + ",</P>"
        Body = Body + vbCrLf + "<br><br>"
 Body = Body + vbCrLf + "</BODY></HTML>"
        myCDO.BodyFormat = MailFormat.Html
        myCDO.Body = Body
0
 
LVL 6

Expert Comment

by:esolve
ID: 38834114
I think you should be doing this.

sEmailBody = sEmailBody.Replace("[Component]", strComponent)

because you're replacing the text but not assigning it to anyting
0
 

Author Closing Comment

by:ndr-itsolutions
ID: 38834380
Fantastic way to create cutomised emails and pass in variables as well, once mastered , very easy to use. Thanks again
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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

912 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

18 Experts available now in Live!

Get 1:1 Help Now