大家好,
我知道有像jwsmtp和vmime或poco这样的外部库可以帮助你用c++发送电子邮件。然而,我在配置和链接它们时遇到了问题。因此,我想知道是否有人有通过我的gmail帐户在c++(Windows7os)中发送电子邮件的源代码。
发布于 2012-02-02 21:17:01
如果你真的想用一种很难的方式来做这件事,你必须使用一个TLS库,比如OpenSSL或者Windows Schannel API来建立一个与服务器的TLS连接。如何做到这一点的一个例子可以在这里找到:http://www.coastrd.com/c-schannel-smtp
然而,我认为让这些外部库工作起来会容易得多。
发布于 2012-02-02 22:13:14
我总是幸运地与Poco一起构建。唯一的诀窍是,首先您必须构建OpenSSL并将Poco构建脚本更改为其位置。
发布于 2014-01-12 13:43:30
如果这对任何人都有用,下面是一些C++代码,它们在Microsoft Windows上基本上形成了一个带有附件、主题和正文文本EMail消息。它启动默认的EMail (MAPI)客户端,添加了附件并填充了其他属性。遗憾的是,它并不直接与SMTP服务器交互。我在我们的产品中使用它来让用户向我发送调试跟踪等等。
如果你想使用下面的代码,你需要替换ptiString和ptiStringArray,它们是管理字符串和字符串数组的内部类。TObjList只是一个类容器管理模板。给出了一个is all数组。
希望这能帮到你。
the .h
//---------------------------------------------------------------------------
// Only for Microsoft Windows
// uses Mapi32.dll
// uses MAPI to launch the EMail client, add attachments and message body
// to whatever client is in use. It doesn't seem to work with Windows Live Mail
// see this link: http://msdn.microsoft.com/en-us/library/windows/desktop/dd296721(v=vs.85).aspx
// and be aware of UAC crap-o-la:
// http://social.msdn.microsoft.com/Forums/office/en-US/63e9f5b2-f5f2-4cf8-bdc2-ca1fad88ebe5/problem-with-outlook-and-mapisendmail-returns-mapiefailure-when-outlook-is-running
//
#ifndef emailsenderH
#define emailsenderH
#include <windows.h>
#include <mapi.h>
#include "ptiString/ptiStringArray.h"
#include "ptiString/TObjList.h"
//---------------------------------------------------------------------------
class EMailSender
{
private:
HWND m_hLib;
ptiString Subject;
ptiStringArray Body;
TObjList<MapiFileDesc> Attachments;
// don't copy this.
EMailSender(const EMailSender &rhs);
EMailSender &operator =(const EMailSender &rhs);
public:
EMailSender();
~EMailSender();
void AddAttachment(ptiString &filename);
void AddAttachments(ptiStringArray &filename);
void SetMessage(const ptiStringArray &);
void SetMessage(const wchar_t *);
void SetSubject(const ptiString &subj);
void SetSubject(const wchar_t *subj);
bool isEMailSupported();
bool Send(HWND hWndParent);
};
#endifthe .cpp
#include "emailsender.h"
//---------------------------------------------------------------------------
EMailSender::EMailSender():m_hLib(0)
{
m_hLib = LoadLibrary( "mapi32.dll" );
}
EMailSender::~EMailSender()
{
if(m_hLib)
FreeLibrary(m_hLib);
}
bool EMailSender::isEMailSupported()
{
if(m_hLib)
return(true);
return(false);
}
void EMailSender::AddAttachment(ptiString &filename)
{
MapiFileDesc *pFile = Attachments.Add();
ZeroMemory( pFile, sizeof(MapiFileDesc) );
pFile->nPosition = (unsigned long)-1;
char *fname = const_cast<char *>( filename.toUTF8() );
// convert filename to full path filename
long length = MAX_PATH;
char *Buffer = new char[length];
int retval = GetFullPathName(fname, length, Buffer, 0);
if(length && length>retval)
{
filename = Buffer;
fname = const_cast<char *>( filename.toUTF8() );
pFile->lpszPathName = fname;
}
delete [] Buffer;
}
void EMailSender::SetSubject(const wchar_t *subj)
{
Subject = subj;
}
void EMailSender::SetSubject(const ptiString &subj)
{
Subject = subj;
}
void EMailSender::SetMessage(const wchar_t *msg)
{
Body = msg;
}
void EMailSender::SetMessage(const ptiStringArray &msg)
{
Body = msg;
}
void EMailSender::AddAttachments(ptiStringArray &filename)
{
for(int i=0; i<filename.Count(); i++)
AddAttachment(filename[i]);
}
bool EMailSender::Send(HWND hWndParent)
{
if (!m_hLib)
return false;
LPMAPISENDMAIL SendMail = (LPMAPISENDMAIL) GetProcAddress(m_hLib, "MAPISendMail");
if (!SendMail)
return false;
MapiMessage message;
ZeroMemory( &message, sizeof(MapiMessage) );
message.lpszSubject = (LPSTR) Subject.toUTF8();
MapiFileDesc *pFile = 0;
int fcnt = Attachments.Count();
message.nFileCount = fcnt;
if(fcnt)
{
pFile = new MapiFileDesc[fcnt];
for(int i=0; i<fcnt; i++)
memcpy( &pFile[i], Attachments[i], sizeof(MapiFileDesc) );
message.lpFiles = pFile;
}
ptiString note( Body.GetText() );
char *cnote = const_cast<char *>( note.toUTF8() );
message.lpszNoteText = cnote;
int nError = SendMail(0, (ULONG)hWndParent, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0);
if (nError != SUCCESS_SUCCESS && nError != MAPI_USER_ABORT && nError != MAPI_E_LOGIN_FAILURE)
return false;
if(pFile)
delete [] pFile;
return true;
}https://stackoverflow.com/questions/9112917
复制相似问题