Solved

Creating an email with attachments in VB 2008

Posted on 2010-09-14
9
385 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
  • 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 69

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
 
LVL 69

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 3

Expert Comment

by:deepusreedhar
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:deepusreedhar
deepusreedhar 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:deepusreedhar
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
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.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
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: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now