Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 399
  • Last Modified:

Creating an email with attachments in VB 2008

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
chrismanncalgavin
Asked:
chrismanncalgavin
  • 3
  • 3
  • 2
  • +1
2 Solutions
 
aledevCommented:
Try using CDO library
here there are many examples
http://msdn.microsoft.com/en-us/library/ms528097(v=EXCHG.10).aspx
0
 
Éric MoreauSenior .Net ConsultantCommented:
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
 
chrismanncalgavinAuthor Commented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Éric MoreauSenior .Net ConsultantCommented:
>>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
 
Deepu SreedharBI Software EngineerCommented:
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
 
Deepu SreedharBI Software EngineerCommented:
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
 
Deepu SreedharBI Software EngineerCommented:
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
 
chrismanncalgavinAuthor Commented:
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
 
chrismanncalgavinAuthor Commented:
Good comments, very useful.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now