Tag Archives: GMail

Sending SMTP Email with Advanced Features in Visual Basic.NET | Part 1 – Adding Multiple Attachments

This is Part 1 of ‘Sending Email with Advanced Features’ which will show how you can send an email message and add not just one attachment, but pretty much as many attachments as you want. If you haven’t already, I recommend you check out this article I made on the basics of setting up and sending email using the System.NET.Mail SMTP feature in the DotNET Framework. The code I’m using is compatible with Visual Basic .NET 2005, VB 2008, and VB.NET 2010 since I’m using the classes under the System.NET.Mail namespace which is made available in the .NET 2.0, .NET 3.0, .NET 3.5, and the .NET 4.0 Frameworks. I am using SMTP and a Google GMail account to send the email message.

First of all to get started you will want a instance of a few classes. Below are the ones you’ll need to setup…

  • System.Net.Mail.SmtpClient
  • System.Net.Mail.MailMessage
  • System.Net.Mail.Attachment

Below are the variables I am using for the classes above..

Continue reading

Sending eMail using SMTP in Visual Basic.NET & Visual Basic 2008, VB 2010, and higher

This small article is just going to cover the Normal basic features of an email client to send mail messages. This post shows how to Send a email using Visual Basic 2005, VB 2008, VB.NET 2010, and newer.

Edit: I suggest you also check out the other articles i’ve make that expands on this article about sending your email messages with advanced features. Check out this one, and another one will be done shortly.

 Note: I do want to thank the author of an article at The Code Project for which I got the info for using gMail as the smtp host. 🙂


Covered:

  1. Attachment
  2. To
  3. From
  4. Subject
  5. Body

To send a mail message through .NET 05, VB 08, Visual Basic 2010  you will want to head to the System.Net.Mail Namespace which contains the important Classes for setting up and sending mail messages. The main class to use is the SMTPClient class. Other classes that will be used is MailMessage and Attachment. To get started Three(Objects) needs to be made to…

  1. The SMTPClient Class
  2. The MailMessage Class
  3. And the Attachment Class

 

The following code will first setup the System.Net.Mail.MailMessage Class.

'
'This will contain the actual message to send.
Dim message As System.Net.Mail.MailMessage

 

The next class is the SMTPClient…

'
'The Simple Mail Tranfer Protocol client with the Host and Port number to use. You will want to change these settings to what you need to use.
Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)

The code above has created and setup the SMTP Client. Googles gMail actually requires 587 to be the port number from what I understand. You can leave out the Port number if your SMTP doesn’t require a specific port. Also remember if you are not using the GMail to send your messages then you need to change the Host parameter to the Host you are going to use to send your mails. Example: “mysmtp.host.com”

 

The final Class is the Attachment class…

        '
        'Will contain the attachment info to send with the message.
        Dim attach As System.Net.Mail.Attachment
        '
        'Setup the mail message using 4x Textbox controls that contains the From/To/Subject/Message-Body info.
        'You just  need to set it up the way you want it to be…
        Message = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text)

 

The code below will check a attachment to make sure the file exists..

        'Need to make sure the attachment exists before adding it to the message.
        If My.Computer.FileSystem.FileExists(txtAttachment.Text) Then Message.Attachments.Add(attach)

 

Set the Secure Socket Layer to be ‘TRUE’

        '
        'Use Secure Socket Layer to Encrypt the connection for sending the mail.
        'This needs be set to “True” if you plan on using GMail or Hotmail or Yahoo as your host.
        smtp.EnableSsl = True

 

Then setup your Credentials for your Login info. (Username and Password)

        '
        'Setup your account information for authorization…
        smtp.Credentials = New System.Net.NetworkCredential("yourUsername@yourmail.com", "password")

 

Now that the Mail setup is completed. The only thing left to do is Send your message.

        Try
            '
            'Send the mail message that was setup.
            smtp.Send(Message)
            '
            'Usually it was successful in sending the message if it makes it to here
            'Otherwise it should throw a error message from the Catch exception below.
            'You need to remember that after this point its up to the Host to do the rest.
            MessageBox.Show("Well, the mail message appears to have been asuccess!", _
                "Successful?",MessageBoxButtons.OK, MessageBoxIcon.Information)

        Catch exc As Net.Mail.SmtpException

            MessageBox.Show(exc.StatusCode.ToString, " Something Happened?", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try

Well all of this code in the article works just fine for me to send messages with attachments. If using VB.NET 02/03 you may have to do some modification. I don’t think .NET 1.0/1.1 has the System.Net.Mail namespace and class. Please leave a comment if you know if/what is different so I can update this article. Hopefully it does for your too 🙂  Take care…

Jason

Minor Revision: 2015