如何使用cdosys.dll实现发送邮件
2004-11-12 08:42
1186 查看
CDO for Windows 2000 |
Using Native COM Support in Microsoft Visual C++
Microsoft Visual C++ version 5.0 or later has native COM support through the#importstatement. Using this statement allows you to automatically add type information contained in a type library into your project. The generated files normally have the same name as the component DLL with the extensions .tlh and .tli.
When using the #import statement, make sure to import the ActiveX Data Objects type information before the Microsoft CDO for Windows 2000 type information. The following examples demonstrate using the
#importstatement:#import "c:/program files/common files/system/ado/msado15.dll" no_namespace raw_interfaces_only rename("EOF", "EndOfFile")
#import <cdosys.dll> no_namespace raw_interfaces_only
#include "cdosysstr.h"
#include "cdosyserr.h"
void main() {
CoInitialize(NULL);
IMessage* pMsg = NULL;
HRESULT hr =
CoCreateInstance( __uuidof(Message),
NULL,
CLSCTX_INPROC_SERVER,
__uuidof(IMessage),
(void**) &pMsg
);
...
CoUninitialize();
}
Note
You must have the ADO 2.5 type information in the same namespace as CDO type information. CDO interfaces make use of ADO types in arguments. These types are not qualified using namespaces in the CDO interface definitions. If these types are not in the same namespace, errors will occur. You can do one of the following to solve this problem:
Rename the default ADODB ADO namespace to CDO.
Import type information into the global namespace, if feasible.
Rename both CDO and ADODB to another namespace, such as CDOSYS.
The GUID definitions for ADO and CDO types can be retrieved using the
__uuidofoperator supported by Visual C++. You can use
#importflags to specify the CLSID_XXX and IID_XXX definitions if this format is desired.
Consider the following issues when using the #import statement:
The header file generated by the #import directive does not contain the module constant definitions contained within the type library. However, the Platform SDK provides two separate headers that can be included within your project so that these constants are available. These files are
cdosysstr.hand
cdosyserr.h. The first contains the field name constants, and the second includes the CDO custom HRESULT error codes. If you do not include these, you will have to manually insert these values. For example, the field "urn:schemas:httpmail:to" has an associated constant called cdoTo in the string constant header file
cdosysstr.h.
The
#importstatement strips the default values for arguments when generating the header files. If you use #import, you will need to explicitly specify the defaults. For example:
hr = pBp->AddBodyPart(-1); // the -1 is the default value in cdosys.h
Using Interface Pointer Wrapper Classes with Import
The
raw_interfaces_onlyflag to
#importdirective suppresses the creation of "smart pointer" C++ wrapper classes. However, in many cases these wrappers simplify working with CDO in C++. The following example demonstrates using the wrapper classes produced by the
#importstatement:#import "c:/program files/common files/system/ado/msado15.dll" no_namespace rename("EOF", "EndOfFile")
#import <cdosys.dll> no_namespace
#include "cdosysstr.h"
#include "cdosyserr.h"
void main() {
CoInitialize(NULL);
{
try {
IMessagePtr iMsg(__uuidof(Message));
FieldsPtr Flds;
Flds = iMsg->Fields;
Flds->Item[cdoTo]->Value = _variant_t("example@example.com");
Flds->Item[cdoFrom]->Value = _variant_t("example@example.com");
Flds->Item[cdoSubject]->Value = _variant_t("a test");
Flds->Item[cdoTextDescription]->Value = _variant_t("this is a test");
Flds->Update();
iMsg->Send();
}
catch( _com_error err) {
// ...
}
}
CoUninitialize();
return 1;
}
Using the CDO Namespace
You can use namespaces with the #import statement. However, when you import the ADO type information, you must make sure to add it to the CDO namespace, or suppress the namespace by adding it to the global namespace. If you do not do this, all ADO type information will reside in the ADODB namespace by default, and CDO arguments that are ADO types will not resolve at compile time. The following example demonstrates this:#import "c:/program files/common files/system/ado/msado15.dll" rename("ADODB","CDO") rename("EOF", "EndOfFile")
#import <cdosys.dll>
#include "cdosysstr.h"
#include "cdosyserr.h"
void main() {
CoInitialize(NULL);
{
try {
CDO::IMessagePtr iMsg(__uuidof(CDO::Message));
CDO::FieldsPtr Flds;
Flds = iMsg->Fields;
Flds->Item[cdoTo]->Value = _variant_t("example@example.com");
Flds->Item[cdoFrom]->Value = _variant_t("example@example.com");
Flds->Item[cdoSubject]->Value = _variant_t("a test");
Flds->Item[cdoTextDescription]->Value = _variant_t("this is a test");
Flds->Update();
iMsg->Send();
}
catch( _com_error err) {
// ...
}
}
}
CoUninitialize();
return 1;
}
相关文章推荐
- 如何使用 Cdosys.dll 库使用ASP发送邮件带附件
- 如何使用 Cdosys.dll 库使用 Visual C# 中发送电子邮件带有附件接收邮件
- 如何使用 Cdosys.dll 库使用ASP发送邮件带附件
- 如何使用 Cdosys.dll 库使用 Visual C# 中发送电子邮件带有附件接收邮件
- 在Windows2003系统中使用VBS利用cdosys.dll发送邮件
- 如何使用ASP.NET中的类库实现邮件的发送
- 如何使用JMAIL组件实现邮件的发送与接收
- java实现发送邮件可多个附件内嵌图片-commons-email使用(未测试)
- thinkphp5中使用phpmailer实现发送邮件功能
- 如何在 Laravel 中使用 SMTP 发送邮件(适用于 163、QQ、Gmail 等)
- 使用spring-boot-starter-mail实现邮件发送
- 看了传智播客的视频,学会了使用JavaMail实现邮件发送。
- Node.js使用qq邮箱实现邮件发送
- 使用javamail实现邮件发送功能完整代码
- django实现如何向QQ邮箱发送邮件
- Spring中使用 JavaMailSenderImpl来实现邮件的发送
- 如何使用GMail发送邮件
- Ajax详解及其案例分析之如何获得Ajax对象,使用Ajax对象发送GET和POST请求,校验用户名,POST和GET请求时的乱码处理,实现级联的下拉列表
- 使用python 3实现发送邮件功能
- 使用libcurl库实现SMTP发送邮件