Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sending an email with VB.net

Posted on 2013-01-28
10
Medium Priority
?
497 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
[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
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 2000 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
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.

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

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…
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…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
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…

598 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