By Anish Pillai Anish Pillai Posted under Advanced Concepts

How to send emails using QTP from Microsoft Outlook

2 Flares Twitter 1 Facebook 0 Google+ 1 LinkedIn 0 Email -- 2 Flares ×

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 -

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

Please let us know if the above code works for you. Please use the comments section or contacts form to get in contact with us regarding any issues/suggestions. Happy Reading.. :–)

If you liked this article, you can join our blog to get new posts delivered directly in your inbox.

2 Flares Twitter 1 Facebook 0 Google+ 1 LinkedIn 0 Email -- 2 Flares ×
  • Pingback: How to send emails using QTP from Gmail and Yahoo - Automation Repository - Automation Repository

  • Pingback: Part 5: Automating Outlook using QTP | Emails in Outlook - Automation Repository - Automation Repository

  • Mac

    What if you have 2 different profiles/account in outlook? Let say you want to send on a second profile instead of the default?

  • Anish10110

    Hi Mac,

    In case there are multiple profiles in outlook, we can select the desired account using <strong>SendUsingAccount</strong> method. Once the desired account is selected, we can send mail using the same code. Example: Let us consider that we have 2 accounts in Outlook – abc@test.com (default) & def@test.com. Now to send a mail from def@test.com, we can use the following code.

    <blockquote>
    Set objOutlook = CreateObject("Outlook.Application")
    Set myMail = objOutlook.CreateItem(0)

    'Set the email properties
    myMail.To = "some_mail_id@gmail.com"
    myMail.Subject = "Test-VBS"
    myMail.Body= "Test Mail Contents"

    'Loop through all the accounts
    For Each oAccount in objOutlook.Session.Accounts
    If oAccount.SmtpAddress = "def@test.com" Then

    ' Send the mail
    myMail.SendUsingAccount = oAccount
    myMail.Send()

    End If
    Next
    </blockquote>

  • Abhishek

    Cooool…

    • Anish10110

      :)

  • Manju Nair

    Hi Anish,

    Is there any way by which we can send emails to lotus notes just as we are doing for Outlook?I have tried lots of code by nothing seems to be working fine.

    QTP QC Addin has an in built method called 'QCConnection.SendMail' ,but this also is unable to add attachments .The user recieves only plain text and no attachment.

    It will be really great if you post something related to sending mails to lotus notes using qtp.

    Thanks,

    Manju

2 Flares Twitter 1 Facebook 0 Google+ 1 LinkedIn 0 Email -- 2 Flares ×