Saturday, May 26, 2012

Sending email using QTP from Microsoft Outlook

In the previous article, we saw how to send emails from  QTP via Gmail & Yahoo Mail. There we wrote the code that connects to GMail/Yahoo SMTP Server and sends mails to any email id. But if you have MS Outlook installed in your machine, you can directly use Outlook to send emails to the required mail ids.  Also, the code to send mails from Outlook is relatively simpler than code for sending mails from Gmail/Yahoo using Microsoft CDO technology. Let’s see how the code works -

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Function fnSendEmailFromOutlook
'Create an object of type Outlook
Set objOutlook = CreateObject("Outlook.Application")
Set myMail = objOutlook.CreateItem(0)
'Set the email properties
myMail.To = "some_mail_id@gmail.com"
myMail.CC = "some_mail_id_2@gmail.com; some_other_mail@yahoo.com" 'Sending mails to multiple ids
myMail.BCC = "" 'If BCC is not required, then this line can be omitted
myMail.Subject = "Sending mail from MS Outlook using QTP"
myMail.Body= "Test Mail Contents"
myMail.Attachments.Add("D:\Attachment.txt") 'Path of the file to be attached
'Send the mail
myMail.Send
Wait(3)
'Clear object reference
Set myMail = Nothing
Set objOutlook = Nothing
End Function

No comments: