[C++] 纯文本查看 复制代码
Wrap_errno SendMail_UsingOOM(
const std::wstring& to,
const std::wstring& cc,
const std::wstring& bcc,
const std::wstring& subject,
const std::wstring& body,
const std::wstring& attachmentPath,
bool showUI
) {
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
CLSID clsid;
HRESULT hr;
Wrap_errno err;
memset(&err, 0, sizeof err);
LPCOLESTR progID = L"Outlook.Application";
hr = CLSIDFromProgID(progID, &clsid);
if (FAILED(hr)) {
CoUninitialize();
return err;
}
// 创建Outlook对象
IDispatch* pOutlookApp = NULL;
hr = CoCreateInstance(
clsid, // CLSID of the server
NULL,
CLSCTX_LOCAL_SERVER, // Outlook.Application is a local server
IID_IDispatch, // Query the IDispatch interface
(void**)&pOutlookApp); // Output
if (FAILED(hr)) {
// 处理错误
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
CoUninitialize();
return err;
}
// 获取Namespace对象
IDispatch* pNS = NULL;
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(L"MAPI");
VARIANT result;
VariantInit(&result);
err = AutoWrapA(DISPATCH_METHOD, &result, pOutlookApp, (wchar_t*)L"GetNamespace", 1, x);
if (err.hr < 0) {
// 处理错误
VariantClear(&result);
VariantClear(&x);
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
if (pNS != NULL) {
pNS->Release();
}
CoUninitialize();
return err;
}
pNS = result.pdispVal;
VariantClear(&x);
}
//指定邮箱账户的名称,通常是邮箱地址。
{
VARIANT vtNewSession;
vtNewSession.vt = VT_BSTR;
vtNewSession.bstrVal = NULL;//空为当前账户
VARIANT vtShowDialog;
vtShowDialog.vt = VT_BSTR;
vtShowDialog.bstrVal = NULL;//空为当前账户密码
VARIANT vtMissing;
vtMissing.vt = VT_BOOL;
vtMissing.boolVal = VARIANT_TRUE;
AutoWrapA(DISPATCH_METHOD, NULL, pNS, (wchar_t*)L"Logon", 3, vtNewSession,
vtShowDialog, vtMissing);
VariantClear(&vtShowDialog);
VariantClear(&vtNewSession);
VariantClear(&vtMissing);
}
// 创建新邮件
IDispatch* pMail = NULL;
{
VARIANT x;
x.vt = VT_I4;
x.lVal = 0; // Outlook::olMailItem
VARIANT result;
VariantInit(&result);
err = AutoWrapA(DISPATCH_METHOD, &result, pOutlookApp, (wchar_t*)L"CreateItem", 1, x);
if (err.hr < 0) {
if (pMail != NULL) {
pMail->Release();
}
if (pNS != NULL) {
pNS->Release();
}
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
VariantClear(&x);
VariantClear(&result);
CoUninitialize();
return err;
}
pMail = result.pdispVal;
}
//设置邮件的主题
// pMail->Subject = _bstr_t(L"Feedback of All-In-One Code Framework");
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(subject.c_str());
AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Subject", 1, x);
VariantClear(&x);
}
//设置邮件的收件人
// pMail->To = _bstr_t(L"codefxf@microsoft.com");
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(to.c_str());
AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"To", 1, x);
VariantClear(&x);
}
//设置邮件的抄送
// pMail->cc
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(cc.c_str());
AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Cc", 1, x);
VariantClear(&x);
}
//设置邮件的密送
//// pMail->bcc
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(bcc.c_str());
AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Bcc", 1, x);
VariantClear(&x);
}
//邮件内容
// pMail->body
{
VARIANT x;
x.vt = VT_BSTR;
x.bstrVal = SysAllocString(body.c_str());
AutoWrapA(DISPATCH_PROPERTYPUT, NULL, pMail, (wchar_t*)L"Body", 1, x);
VariantClear(&x);
}
//获取MailItem对象
//添加邮件附件
//pAtta = pMail->GetAttachments
//pAtta->Add(source,type,position,displayname)
IDispatch* pAtta = NULL;
{
VARIANT result;
VariantInit(&result);
err = AutoWrapA(DISPATCH_PROPERTYGET, &result, pMail, (wchar_t*)L"Attachments", 0);
if (err.hr < 0)
{
if (pMail != NULL)
{
pMail->Release();
}
if (pNS != NULL)
{
pNS->Release();
}
if (pOutlookApp != NULL)
{
pOutlookApp->Release();
}
VariantClear(&result);
CoUninitialize();
return err;
}
pAtta = result.pdispVal;
VARIANT path;
path.vt = VT_BSTR;
path.bstrVal = SysAllocString(attachmentPath.c_str());
VARIANT x;
x.vt = VT_I4;
x.lVal = 1;
err = AutoWrapA(DISPATCH_METHOD, NULL, pAtta, (wchar_t*)L"Add", 4, path, x, x, path);
if (err.hr < 0)
{
if (pAtta != NULL)
{
pAtta->Release();
}
if (pMail != NULL)
{
pMail->Release();
}
if (pNS != NULL)
{
pNS->Release();
}
if (pOutlookApp != NULL)
{
pOutlookApp->Release();
}
VariantClear(&result);
VariantClear(&path);
CoUninitialize();
return err;
}
VariantClear(&path);
}
//显示邮件
// pMail->Display(true);
{
VARIANT vtModal;
vtModal.vt = VT_BOOL;
vtModal.boolVal = VARIANT_TRUE;
err = AutoWrapA(DISPATCH_METHOD, NULL, pMail, (wchar_t*)L"Display", 1, vtModal);
if (err.hr < 0) {
if (pMail != NULL) {
pMail->Release();
}
if (pNS != NULL) {
pNS->Release();
}
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
VariantClear(&vtModal);
CoUninitialize();
return err;
}
}
//发送邮件
AutoWrapA(DISPATCH_METHOD, NULL, pMail, (wchar_t*)L"Send", 0);
//执行Outlook应用程序的登出操作。
// pNS->Logoff()
err = AutoWrapA(DISPATCH_METHOD, NULL, pNS, (wchar_t*)L"Logoff", 0);
if (err.hr < 0) {
if (pMail != NULL) {
pMail->Release();
}
if (pNS != NULL) {
pNS->Release();
}
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
CoUninitialize();
return err;
}
if (pMail != NULL) {
pMail->Release();
}
if (pNS != NULL) {
pNS->Release();
}
if (pOutlookApp != NULL) {
pOutlookApp->Release();
}
CoUninitialize();
err.hr = S_OK;
return err;
}
int main() {
SendMail_UsingOOM(L"Yong.Han@darwinprecisions.com", L"Yong.Han@darwinprecisions.com", L"Yong.Han@darwinprecisions.com", L"测试", L"It`s OOM Test.Do not care it.", L"", true);
return 0;
}