创建邮件项、附加报表并将邮件项发送给用户的经理
此代码示例先创建有附件的邮件项,再将邮件项发送给用户的经理。
示例
该示例仅对 Microsoft Exchange Server 帐户正常运行。 必须在 Active Directory 目录服务中为用户建立经理关系。 该示例使用 ExchangeUser 对象通过调用 GetExchangeUserManager 方法来确定当前用户的管理器。
如果使用 Visual Studio 测试此代码示例,必须先添加对 Microsoft Outlook 15.0 对象库组件的引用,并在导入 Microsoft.Office.Interop.Outlook 命名空间时指定 Outlook 变量。 不得将 Imports 或 using 语句直接添加到此代码示例中的函数前面,这两个语句必须后跟公共类声明。 下面的代码行演示了如何在 Visual Basic 和 C# 中执行导入和分配。
Imports Outlook = Microsoft.Office.Interop.Outlook
using Outlook = Microsoft.Office.Interop.Outlook;
Private Sub SendSalesReport()
Dim mail As Outlook.MailItem = CType(Application.CreateItem( _
Outlook.OlItemType.olMailItem), Outlook.MailItem)
mail.Subject = "Quarterly Sales Report FY06 Q4"
Dim currentUser As Outlook.AddressEntry = _
Application.Session.CurrentUser.AddressEntry
If currentUser.Type = "EX" Then
Dim manager As Outlook.ExchangeUser = _
currentUser.GetExchangeUser().GetExchangeUserManager()
' Add recipient using display name, alias, or smtp address
mail.Recipients.Add(manager.PrimarySmtpAddress)
mail.Recipients.ResolveAll()
mail.Attachments.Add("c:\sales reports\fy06q4.xlsx", _
Outlook.OlAttachmentType.olByValue)
mail.Send()
End If
End Sub
private void SendSalesReport()
{
Outlook.MailItem mail = Application.CreateItem(
Outlook.OlItemType.olMailItem) as Outlook.MailItem;
mail.Subject = "Quarterly Sales Report FY06 Q4";
Outlook.AddressEntry currentUser =
Application.Session.CurrentUser.AddressEntry;
if (currentUser.Type == "EX")
{
Outlook.ExchangeUser manager =
currentUser.GetExchangeUser().GetExchangeUserManager();
// Add recipient using display name, alias, or smtp address
mail.Recipients.Add(manager.PrimarySmtpAddress);
mail.Recipients.ResolveAll();
mail.Attachments.Add(@"c:\sales reports\fy06q4.xlsx",
Outlook.OlAttachmentType.olByValue, Type.Missing,
Type.Missing);
mail.Send();
}
}