Solved

Sending an email with VB.net

Posted on 2013-01-28
10
468 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

758 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

16 Experts available now in Live!

Get 1:1 Help Now