Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Creating an email with attachments in VB 2008

Posted on 2010-09-14
9
Medium Priority
?
398 Views
Last Modified: 2012-05-10
Hi,
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.
0
Comment
Question by:chrismanncalgavin
[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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:aledev
ID: 33671319
Try using CDO library
here there are many examples
http://msdn.microsoft.com/en-us/library/ms528097(v=EXCHG.10).aspx
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 33671332
If you are sure that your users have Outlook, check http://www.emoreau.com/Entries/Articles/2010/07/Sending-e-mails-with-attachments-a-different-method.aspx
Otherwise, SMTP is the way to go (even if you are using gmail as your SMTP server): http://www.emoreau.com/Entries/Articles/2007/09/Using-SystemNetMail.aspx
0
 
LVL 8

Author Comment

by:chrismanncalgavin
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?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Accepted Solution

by:
Éric Moreau earned 1200 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 http://www.eggheadcafe.com/tutorials/aspnet/9ae03b00-69e9-465e-adb9-a5bcc222d161/sending-mail-using-system.aspx 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).

0
 
LVL 3

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 _
                 message.Attachments.Add(attachment)
        Dim emailClient As New SmtpClient(gblStrEmailServer)
        Try
            emailClient.Send(message)
        Catch ex As Exception
            MsgBox("ERROR in sending Report-EmailReport", ex.Message)
        End Try
    End Sub

Open in new window

0
 
LVL 3

Assisted Solution

by:Deepu Sreedhar
Deepu Sreedhar earned 800 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","xyz@example.com","Sample Subject", "some text for body")
0
 
LVL 3

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
0
 
LVL 8

Author Comment

by:chrismanncalgavin
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("myemailaccount@gmail.com")
oMsg.To.Add("mysender@mydomain.com")
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 = "smtp.gmail.com"
myClient.UseDefaultCredentials = False
myClient.Port = 587
myClient.EnableSsl = True
myClient.Credentials = New System.Net.NetworkCredential("mygmailusername", "mygmailpassword")
Try
  myClient.Send(oMsg)
  Console.WriteLine("SUCCESS")
  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

0
 
LVL 8

Author Closing Comment

by:chrismanncalgavin
ID: 33681063
Good comments, very useful.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Are you irritated by repeating emails issue in Microsoft Outlook 2016 after recent update ?  Lets’ see how to resolve and prevent duplicate emails in the Outlook 2016 using some simple techniques.
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…

715 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