Monday, April 18, 2011

QTP script to send an email from outlook

'Call to the function for sending the mail on mail id 'abc@abc.com'
Call SendMail("abc@abc.com","Hi","Hi","")

' THis function will create an email and send it using outlook

Function SendMail(SendTo, Subject, Body, Attachment)

'CreateObject method to create an Outlook Application object
Set ol=CreateObject("Outlook.Application")

'Create a new outlook mail object
Set Mail=ol.CreateItem(0)

'Add the email address to the recipient list of the message
Mail.to=SendTo

'Add the subject of the message
Mail.Subject=Subject

'Add the body of the mail message
Mail.Body=Body

'Add the attachment to the mail message
If (Attachment <> "") Then
Mail.Attachments.Add(Attachment)
End If

'Send the mail message
Mail.Send

'Free up the memory
ol.Quit
Set Mail = Nothing
Set ol = Nothing

End Function

Interested in more QTP codes? click here

7 comments:

  1. How to keep more than one person in CC ?

    ReplyDelete
  2. Nice question, I never had a chance to try this one. I guess separating out the email ids with a ; should be handy.

    Also, In the SendMail function add a new parameter for cc. Add the line "Mail.cc="

    ReplyDelete
  3. Hi guys,
    For the above script i am getting " General run error "
    at mail.send line, can anybody help to solve the error. And can tell me what other changes i have to do.

    Shiri

    ReplyDelete
  4. What can this thing do? Email you when the script fail?

    ReplyDelete
  5. To keep more than one in CC:
    ObjectName.CC = "email1@email.com,email2@email.com"

    ReplyDelete
  6. Can we pickup email id from excel sheet to send multiple mails.

    ReplyDelete
  7. Can we pickup the email ids from the xcel sheet to attache multiple recipents.

    ReplyDelete

Please leave your comment here...