vbaemailoutlooksendqtp

QTP, send mailer address


I am sending an email using the QTP outlook object model.

Here is the piece of code.

'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

Now I needed to retrieve the sender email address & store it in an environment variable. myMail.Sender or myMail.sendermailaddres both of them are not working me.


Solution

  • The following code will give you the first email address the user you're connected to Outlook has access to:

    objOutlook.Session.Accounts.Item(0)
    

    I use a loop to find the account I want to send from like this:

    iAccount = 0
    For iLoop = 1 To oOutlook.Session.Accounts.Count
        If UCase(Trim(oOutlook.Session.Accounts.Item(iLoop))) = UCase(Trim(EmailData("SendFrom"))) Then
            iAccount = iLoop
            Exit For
        End If
    Next
    

    where EmailData is a Dictionary object containing the items I'm using for the mail item. When creating the mail item I use Set oMailItem.SendUsingAccount = oOutlook.Session.Accounts.Item(iAccount) to specify the account it should be sent from.