?
Solved

Sending an email with VB.net

Posted on 2013-01-28
10
Medium Priority
?
492 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
PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

 
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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

765 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