Solved

Creating an email with attachments in VB 2008

Posted on 2010-09-14
9
395 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
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

by:
É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 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 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","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

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