您的位置:首页 > 编程语言 > Qt开发

QTP - 24 Working with MS Outlook 与outlook交互

2012-02-08 14:34 423 查看

24 Working with MS Outlook

Concept: Outlook operation can be automated using COM.

24.0 Outlook Object model

Application

--Namespace

**AddressLists

**AddressEntries

**Folders

**MAPIFolder

**Items

* Actoins

* Attachments

* UserProperties

* RecurrencePattern

* Recipients



--Assistant

--Explorer

--Inspector

**Page

Note: “Namespace”– Outlook uses only a single namespace “MAPI”

“Folders”– collection of folders under the namespace (Mail folders, Public Folders, PST)

“Items” –collection of outlook items (contacts, emails, calendar, appointment etc)

“MAPIFolder” – sub folders present inside the MAPI folders

24.1 Launching Outlook Application

Set olApp=CreateObject("Outlook.Application")

Set olMAPI=olApp.GetNamespace("MAPI")

24.2 Logging in to Namespace

olMAPI.Logon(Profile, password, showDialog, NewSession)

When outlook logs on to the default profile automatically:

olMAPI.Logon ,,False , True

olMAPI.Logoff

24.3 Enumerating the top level folder

思路:主要利用Folder的方法和属性

For Each topFolder in olMAPI.Folders

print ""& (topFolder.Name)

Next

其他的方法和属性:

*Access to specific folder

Set oFirstFolder = olMAPI.Folders.GetFirst

Set oFirstFolder = olMAPI.Folders.Item(1)

Set oFirstFolder = olMAPI.Folders.Item("Public Folders")

*Get parent folder

Set oFirstFolderParent = oFirstFolderChild.Parent

(Note: 如果oFirstFolderChild已经是top folder,就会报错。所以要error handling)

*Get specific child folder

Set oFirstFolderChild = oFirstFolder.Folders("Inbox")

*Get folder name

print oFirstFolderChild.Name

*Get folder path

print oFirstFolderChild.FolderPath

*GetDefaultFolder

Set oFolder = olMAPI.GetDefaultFolder(olFolderInbox)

'Constants for default folders

Const olFolderDeletedItems = 3

Const olFolderOutbox = 4

Const olFolderSentMail = 5

Const olFolderInbox = 6

Const olFolderCalendar = 9

Const olFolderContacts = 10

Const olFolderJournal = 11

Const olFolderNotes = 12

Const olFolderTasks = 13

Const olFolderDrafts = 16

Const olPublicFoldersAllPublicFolders = 18

Const olFolderConflicts = 19

Const olFolderSyncIssues = 20

Const olFolderLocalFailures = 21

Const olFolderServerFailures = 22

Const olFolderJunk = 23

*Get folder’s emails

Set allEmails = oFolder.Items

24.4 Downloading attachment from a specific email

Set oFolder = olMAPI.GetDefaultFolder(6)

Set email = oFolder.items(1) ‘按邮件时间排序,最早邮件为1.

For Each attachment In email.Attachments

attachment.SaveAsFile "C:\" & attachment.FileName

Next

23.5 Filter the emails: Restrict Method

You cancheck the email if be read:

If email.Unread = True Then

UsingRestrict method to filter emails:

Set allEmails = oFolder.Items

Set unreadEmails = allEmails.Restrict("[CC]='' And Not [Unread] = True")

print unreadEmails.Count

Accessing email message properties:

Property

Description

Attachments

Collection of attachments in the mail

CC

CC of the mail

ConversationIndex

Index of the mail in conversation

ConversationTopic

Subject of the conversion(without RE: FW: etc)

HTMLBody

HTML text of the body

ItemProperties

Collection of all email properties

Parent

Get parent folder

Recipients

Collection of all recipient

SenderEmailAddress

Email Address of the sender

SenderEmailType

Sender email type. E.g. SMTP

SenderName

Name of the Sender

Size

Size of the mail

Subject

Subject of the email address

To

To list

UnRead

Tells if email is unread or not

Body BodyFormat

Text of the email

Format of the email(HTML, Plain text)

24.6 Sending an E-mail Message

Mail Types:

Const olMailItem = 0

Const olAppointmentItem = 1

Const olContactItem = 2

Const olTaskItem = 3

Const olJournalItem = 4

Const olNoteItem = 5

Const olPostItem = 6

Const olDistributionListItem = 7

Send email code:

Set newMail = olApp.CreateItem(olMailItem)



'Populate one or more of the email address fields

newMail.To = "xing-xing.tan@hp.com"

newMail.CC = "xing-xing.tan@hp.com"

newMail.BCC = "xing-xing.tan@hp.com"



'Edit in the message's subject and body information

newMail.Subject = "Test Subject By QTP"

newMail.Body = "Body of the email"

24.7 Outlook Security Dialogs:

Method1: Send the email through Outlook UI:

'Create a description

Set oMailWindow = Description.Create

oMailWindow("title").Value = emailCaption

oMailWindow("title").RegularExpression = False



sTitle = "title:=Microsoft Office Outlook"

'Check if a spelling cancelled dialog is present

If Window(oMailWindow).Window(sTitle).Exist(2) Then

Window(oMailWindow).Window(sTitle).WinButton("text:=&Yes").Click

End If

Method2: Using QTP Recovery Scenario

Method3: Use “Working with Modal Dialog boxes” as discussedin the “Working with APIs” chapter

24.8 Getting address from email message:

For Each oRecip In item.Recipients

'Is the recipient in TO list

If oRecip.Type = olTo Then

sTo = sTo & oRecip.Address & ";"

'Is the recipient in CC list

ElseIf oRecip.Type = olCC Then

sCC = sCC & oRecip.Address

End If

Next

24.9 Clicking links inside an email message:

思考:Outlooksupport 3 formats: Plain text, RTF, html. But Outlook can get HTMLcontent no matter which format displayed. Save HTML context into IE browser,then can be done.



Set oFolder = olMAPI.GetDefaultFolder(6)

Set email = oFolder.Items(1)

HTMLText = email.HTMLBody

SystemUtil.Run "iexplore.exe"

Wait 5

Browser("creationtime:=0").object.document.write HTMLText

Set allLinks = Browser("creationtime:=0").object.document.links

24.10 Launching QTP using an email trigger

不做研究。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: