How to send email from VB.Net PDF Print E-mail
Written by J. Bijvoets   
Tuesday, 14 September 2010 06:35

Sending email from Visual Basic.Net

Sometimes you want your application being able to send email from code. Depending on the means of the application you want send your email via the SMTP-object or via the Object-library of your email-application.

Choosing the right way to send email via VB.Net

Use the following questions to decide if you can send your email via SMTP:

  1. Do you want to use fancy methods or properties your email-program offers, like using mail-templates? If yes, don't use SMTP.
  2. Do you want to store the mail you've sent in some folder of your mail-program? If yes, don't use SMTP.

In all other situations, sending via SMTP seems a good idea to me. Please let me know if I'm mistaken.

Sending email via SMTP in VB.Net

I like sending via SMTP, as it is undependable of the version and type of your email application. And it's easy but powerfull. Here's an exapmle of sending some mail via SMTP of GMail:

'First add an import to the top of your file

Imports System.Net.Mail

 

'Use following code somewhere in a routine

Dim MySmtpServerAs New SmtpClient()

Dim MyMail As New MailMessage()

MySmtpServer.Credentials = New Net.NetworkCredential(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ", "mypassword")

MySmtpServer.Port = 587 'Other servers can use other ports, try 25 if unknown

MySmtpServer.Host = "smtp.gmail.com"

MyMail = New MailMessage()

MyMail.From = New MailAddress(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ")

MyMail.To.Add("ReceiverAddress")

MyMail.Subject = "GMail test mail"

MyMail.Body = "Write your mail body some way or another. Using HTML for the body is also possible. "

MyMail.Attachments.Add("C:\Temp\File1.txt") 'Adding an attachment is also possible

MyMail.Attachments.Add("C:\Temp\SecondFile.xml") 'Adding multiple attachments is also possible

MySmtpServer.Send(MyMail)

MsgBox("Done, mail send!")


Sending email via MS Outlook

MS Outlook is a very powerful tool, useable for many tasks. Combining the strength and completeness of Microsoft's objects with your own code can be a winning strategy in many situations. So here's the most basic example to send mail via the Outlook Object Library.

First add a reference to the Microsoft Outlook Object Library 14 (assuming you use MS Outlook 10).

Second add an import-statement on the top of your file:

Imports Outlook = Microsoft.Office.Interop.Outlook

Finally, use the following code:

Dim myOlApp As New Outlook.Application
Dim MyMail As Outlook.MailItem
Dim myRecipients As Outlook.Recipients
Dim myRecipient As Outlook.Recipient
myOlApp = CreateObject("Outlook.Application")
MyMail = myOlApp.CreateItem(Outlook.OlItemType.olMailItem)

myRecipients = MyItem.Recipients   
myRecipients.Add(" This e-mail address is being protected from spambots. You need JavaScript enabled to view it ")

MyMail.Body = "Dit is een test"
MyMail.Subject = "TestOnderwerp"
MyMail.Send()

Running this code will send an email, which you can find in your Sent-folder.

But Outlook has much more to offer. How about sending a task to someone, or sending an email based on some mail-template? There's lot to explore in the Outlook Object Library!


Last Updated on Wednesday, 24 November 2010 21:20