VC下利用MSMAPI32.OCX编写邮件功能

来源:互联网 发布:ios 网络加载动画 编辑:程序博客网 时间:2024/04/30 18:22
我在开发项目时碰到需要发邮件功能,在网上找了好多天都没有找到令我满意的,要不就是邮件发丢,要不就是不能添加附件,可以添加附件的就是附件太大的发送失败,没有办法只好继续找了,终于有一个VB的工程,该工程利MSMAPI32.OCX来实现邮件功能的,我把Vb转成VC现在我已经实现了我满意的结果,
在这里我发布出来和有需要的人共享我的成果,希望对你有所帮助;
1.在工程中添加microsoft MAPI Messages control和microsoft MAPI Session control 组件
project->Add to project->components an control->microsoft MAPI Messages control->insert 同理添加第二个组件,类名称一般使用默认的值参数两个类CMapiSession和CMapiMessages
2.在对话框中添加对应的组件,添加组件后在对话框资源中有两个图标,给两个资源
修改ID:IDC_MAPIMESSAGES IDC_MAPISESSION
3.头文件包含,为这两个控件添加关系变量
CMapiMessagesm_message;CMapiSessionm_session;
//------
DDX_Control(pDX, IDC_MAPIMESSAGES8, m_message);DDX_Control(pDX, IDC_MAPISESSION1, m_session);

4.添加代码
CString strUserName = "test@163.com";//发件人m_session.SetUserName(strUserName);CString strpassword = "123456";m_session.SetPassword(strpassword);//密码m_session.SetDownLoadMail(FALSE);m_session.SetLogonUI(TRUE);m_session.SignOn();long nNewVale = 0;nNewVale =m_session.GetSessionID();m_message.SetSessionID(nNewVale);m_message.Compose();CString strRecipAddress = "test1234@sina.com";//收信人地址m_message.SetRecipAddress(strRecipAddress);CString strRecipDisplayName = "test1234";m_message.SetRecipDisplayName(strRecipDisplayName);//收信人姓名CString strMsgSubject = "测试";m_message.SetMsgSubject(strMsgSubject);//发信的主题CString strMsgNotText = "测试邮件";m_message.SetMsgNoteText(strMsgNotText);//发信的内容long nAttachmentIndex = 0;m_message.SetAttachmentIndex(nAttachmentIndex);//附件索引CString strAttachmentPath = "C://aaa.txt";m_message.SetAttachmentPathName(strAttachmentPath);//附件路径nAttachmentIndex = 1;strAttachmentPath = "C://bbb.txt";m_message.SetAttachmentIndex(nAttachmentIndex);m_message.SetAttachmentPathName(strAttachmentPath);nAttachmentIndex = 2;strAttachmentPath = "C://ccc.txt";m_message.SetAttachmentIndex(nAttachmentIndex);m_message.SetAttachmentPathName(strAttachmentPath);VARIANT vDialog;vDialog.vt = VT_BOOL;//设置变量类型vDialog.boolVal = FALSE;m_message.Send(vDialog);m_session.SignOff();
5.配置你的outLook
需要配置你的邮件帐户和你的帐户属性.如POP3和SMTP等
以上代码在VC6.0下编译通过



原创粉丝点击