delphi VC 操作 outlook的方法

delphi

uses  OutlookXP, ComObj

 

procedure TForm1.Button1Click(Sender: TObject);
const
  olMailItem = 0;
var
  Outlook: OleVariant;
  vMailItem: variant;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  vMailItem := Outlook.CreateItem(olMailItem);
  vMailItem.Recipients.Add('beyond748@21cn.com');
  vMailItem.Subject := 'test email';
  vMailItem.Body := 'This is a test';
  vMailItem.Attachments.Add('C:/temp/sample.txt');
  vMailItem.Send;

  VarClear(Outlook);
end;


procedure TForm1.Button2Click(Sender: TObject);
const
  olAppointmentItem = 0;
var
  Outlook: OleVariant;
  vAppointmentItem: variant;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
 { with vAppointmentItem do
  begin
    subject := '主题';
  end;    }
  vAppointmentItem := Outlook.CreateItem(olAppointmentItem);

  vAppointmentItem.Subject      := '主题';
  vAppointmentItem.Location     := '地址';
  vAppointmentItem.Start        := '8/13/2007/ 6:00:00 PM';
  vAppointmentItem.Duration     := 90;
  vAppointmentItem.ReminderMinutesBeforeStart := 45;
  vAppointmentItem.BusyStatus   := $00000000;
  vAppointmentItem.Body         := $00000000;
  vAppointmentItem.Sensitivity  := $00000000;
  vAppointmentItem.Save;
  vAppointmentItem.dispaly;
end;

procedure TForm1.Button3Click(Sender: TObject);
//发会议
const
  olAppointmentItem = olAppointmentItem;//$00000001;
var
  Outlook: OleVariant;
  vAppointmentItem: variant;
begin
  try
    Outlook := GetActiveOleObject('Outlook.Application');
  except
    Outlook := CreateOleObject('Outlook.Application');
  end;
  vAppointmentItem := Outlook.CreateItem(olAppointmentItem);  vAppointmentItem.Recipients.Add('beyond748@21cn.com');
  vAppointmentItem.Subject := '通过Delphi发送约会';
  vAppointmentItem.Body := '通过Delphi发送约会';
  vAppointmentItem.Importance := $00000002;         
  vAppointmentItem.Duration := $00000001;
  vAppointmentItem.Location := 'aaaaa';
  vAppointmentItem.ReminderMinutesBeforeStart := $00000001;
  vAppointmentItem.Start := Now;//StrToDate('2007-03-01 9:08');
  vAppointmentItem.Duration := Now;//StrToDate('2007-03-05 21:17');
  vAppointmentItem.NetMeetingAutoStart := 1;
  vAppointmentItem.ReminderMinutesBeforeStart := 13;
  vAppointmentItem.BusyStatus := olOutOfOffice;
  vAppointmentItem.Sensitivity := olNormal;
  vAppointmentItem.Send;

  VarClear(Outlook);
end;

//其中一些没有涉及到的字段可以到OutlookXP查查。

 

 

//===========================================================

//vc  好像msdn里面对这个说的不是很清楚,vb道是有实例,和vc很类似拉可以参考的。

我是在mfc里面实现的

首先在stdafx.h里面加入如下:

#import "C:/Program Files/Common Files/Microsoft Shared/OFFICE11/mso.dll" rename_namespace("Office"), named_guids
#import "C:/Program Files/Microsoft Office/Office11/MSOUTL.olb" rename_namespace("Outlook"), named_guids

using namespace Office;
using namespace Outlook;

、----------------------------------------


//读取联系人
void CmfcDlg::OnBnClickedOk()
{
 ::CoInitialize(NULL);
 {
  _ApplicationPtr spOutlook("Outlook.Application");
  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");
  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderContacts);
  _ItemsPtr spItems = spFolder->GetItems();
  _ContactItemPtr spContact;

  char s[200]={0};
  for(int i=1;i<=spItems->Count;++i)
  {
   spContact = spItems->Item(i);
   sprintf(s, "%s%s", _com_util::ConvertBSTRToString(spContact->Subject), _com_util::ConvertBSTRToString(spContact->MobileTelephoneNumber));
   AfxMessageBox(_T(s));
  }
  spContact = NULL;
  spItems  = NULL;
  spFolder = NULL;
  spNamespace = NULL;
  spOutlook = NULL;
 }
 ::CoUninitialize();
}


//读取约会
void CmfcDlg::OnBnClickedButton2()
{
 ::CoInitialize(NULL);
 {
  _ApplicationPtr spOutlook("Outlook.Application");
  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");
  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderCalendar);
  _ItemsPtr spItems = spFolder->GetItems();
  _AppointmentItemPtr spAppointment;


  char s[1024]={0};
  for(int i=1;i<=spItems->Count;++i)
  {
   spAppointment = spItems->Item(i);

   COleDateTime t(spAppointment->Start);
   CString str1 = t.Format("%Y-%m-%d(%A) %H:%M");
   AfxMessageBox(str1);

   COleDateTime t1(spAppointment->End);
   CString str2 = t1.Format("%Y-%m-%d(%A) %H:%M");
   AfxMessageBox(str2);

   sprintf(s, "主题:%s/n地点%s/n开始时间%s/n结束时间%s/n内容:%s",
    _com_util::ConvertBSTRToString(spAppointment->Subject), _com_util::ConvertBSTRToString(spAppointment->Location),
    str1, str2,
    _com_util::ConvertBSTRToString(spAppointment->Subject));
  }

  spAppointment= NULL;
  spItems  = NULL;
  spFolder = NULL;
  spNamespace = NULL;
  spOutlook = NULL;
 }
 ::CoUninitialize();
}

void CmfcDlg::OnBnClickedCancel()
{
}

//写入联系人
void CmfcDlg::OnBnClickedButton1()
{
 ::CoInitialize(NULL);
 {
  _ApplicationPtr spOutlook("Outlook.Application");
  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");
  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderContacts);
  _ItemsPtr spItems = spFolder->GetItems();
  _ContactItemPtr spContact;

  spContact = spItems->Add();
  
  spContact->put_FullName(_com_util::ConvertStringToBSTR(str_name));
  spContact->put_MobileTelephoneNumber(_com_util::ConvertStringToBSTR(str_phoneno));
  spContact->Save();

  spContact = NULL;
  spItems  = NULL;
  spFolder = NULL;
  spNamespace = NULL;
  spOutlook = NULL;
 }
 ::CoUninitialize();
}


//写入约会
void CmfcDlg::OnBnClickedButton3()
{
 ::CoInitialize(NULL);
 {
  _ApplicationPtr spOutlook("Outlook.Application");
  _NameSpacePtr spNamespace = spOutlook->GetNamespace("MAPI");
  MAPIFolderPtr spFolder = spNamespace->GetDefaultFolder(olFolderCalendar);
  _ItemsPtr spItems = spFolder->GetItems();
  _AppointmentItemPtr spAppointment;


  spAppointment = spItems->Add();


  spAppointment->put_Subject(_com_util::ConvertStringToBSTR("主题"));
  spAppointment->put_Location(_com_util::ConvertStringToBSTR("地点"));
  spAppointment->put_Start(GetTickCount());
  spAppointment->put_Start(GetTickCount());
  spAppointment->put_Body(_com_util::ConvertStringToBSTR("里面的内容"));
  spAppointment->Save();


  spAppointment= NULL;
  spItems  = NULL;
  spFolder = NULL;
  spNamespace = NULL;
  spOutlook = NULL;
 }

}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论

打赏作者

beyond748

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值