Creating an email with attachments in VB 2008

Posted on 2010-09-14
Last Modified: 2012-05-10
I have a program that our company have been working on in VB 2008 that needs the ability to send an email with an attachment.

I have looked at various options including:
-Creating an email using System.Diagnostics.Process.Start(sParams) and passing the Mailto: value to it.
This opens up a blank email in your default email client, but will not accept attachments that I can see.
-Using the System.Web.Mail.SmtpMail method to create an email. But the issue we have here is that you must specify an SMTP server, something ideally I would rather our clients not have to do.
-Pasting the file which is an XML file, into the body of the email.

Ideally what I am after is a method that is universal and not specific to users of Outlook etc.
I know I could possible create a reference to Outlook object, but this is less that ideal as not all users have it installed.

Thanks a lot.
Question by:chrismanncalgavin
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
  • 3
  • 3
  • 2
  • +1

Expert Comment

ID: 33671319
Try using CDO library
here there are many examples
LVL 70

Expert Comment

by:Éric Moreau
ID: 33671332
If you are sure that your users have Outlook, check
Otherwise, SMTP is the way to go (even if you are using gmail as your SMTP server):

Author Comment

ID: 33671473
Thanks. I looked at CDO previously as well, but it appears to be unsupported by the .NET framework and therefore a dated way to do this.

emoreau - How would I go about using gmail as the SMTP server, would I need an account setup for the company to use and hard code the settings into the program?
Would this be a good way to go?
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

LVL 70

Accepted Solution

Éric Moreau earned 300 total points
ID: 33671519
>>How would I go about using gmail as the SMTP server, would I need an account setup for the company to use and hard code the settings into the program?

Yes you can create a gmail acocunt that only your application will use.

When you are using external e-mail providers, you need to provide credentials and a different port. Have a look at for an example. If port 587 (as used in the sample is not working, try 465.

I never hardcode settings like those. I prefer to keep them in a config file (or in the database).


Expert Comment

by:Deepu Sreedhar
ID: 33680915
Here we go...
You need this line as well before the beginning of class
Imports System.Net.Mail
Public Sub EmailFile(ByVal strFileName As String, ByVal strToEmailAddress As String, ByVal strSubject As String, ByVal strbody As String)
        strbody = strbody & vbCrLf & vbCrLf & "Please find the attached file." & vbCrLf & vbCrLf
        Dim message As New MailMessage(strFromEmailAddress, strToEmailAddress, strSubject, strbody)
        message.IsBodyHtml = False ' True for HTML format
        Dim attachment As New Attachment(strFileName)
        If File.Exists(strFileName) Then _
        Dim emailClient As New SmtpClient(gblStrEmailServer)
        Catch ex As Exception
            MsgBox("ERROR in sending Report-EmailReport", ex.Message)
        End Try
    End Sub

Open in new window


Assisted Solution

by:Deepu Sreedhar
Deepu Sreedhar earned 200 total points
ID: 33680937
gblStrEmailServer in the above code should be your mailserver IP or URL
Need to set the strFromEmailAddress. Otherwise, include that also as a parameter if you are sending from different accounts
You can use this function to call from your project like this

call EmailFile("C:\Test.txt","","Sample Subject", "some text for body")

Expert Comment

by:Deepu Sreedhar
ID: 33680949
Sorry, I didn't check this point
"Using the System.Web.Mail.SmtpMail method to create an email. But the issue we have here is that you must specify an SMTP server, something ideally I would rather our clients not have to do."
Please ignore the comments if they don't help you

Author Comment

ID: 33681048
Thanks all so far.

I have decided to go the Gmail route using the SmtpClient and System.Net.Mail method.
Seems to work well.

For reference of others, the code I am using is attached.
Imports System.Net.Mail                

Dim oMsg As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage
oMsg.From = New MailAddress("")
oMsg.Subject = "My Email Subjet"
oMsg.Body = "Dear Sir/Madam" & vbCrLf & vbCrLf
oMsg.Body = "This is my test email" & vbCrLf
oMsg.Body &= "Date: " & Format(Now, "dd/MM/yyyy") & vbCrLf
oMsg.Body &= "Thank you"
oMsg.Attachments.Add(New Attachment(MyFilename))
Dim myClient As New System.Net.Mail.SmtpClient
myClient.Host = ""
myClient.UseDefaultCredentials = False
myClient.Port = 587
myClient.EnableSsl = True
myClient.Credentials = New System.Net.NetworkCredential("mygmailusername", "mygmailpassword")
  MessageBox.Show("Email sent successfully!", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
  Console.WriteLine("SEND FAIL")
  MessageBox.Show("Error sending email!" & vbCrLf & ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
oMsg = Nothing
myClient = Nothing

Open in new window


Author Closing Comment

ID: 33681063
Good comments, very useful.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Check out this infographic on what you need to make a good email signature that will work perfectly for your organization.
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: …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…
Suggested Courses

630 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