Solved

Sending an email with VB.net

Posted on 2013-01-28
10
474 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

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.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

803 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