大家好,
我知道有外部库,比如jwsmtp和vmime或poco,可以帮助你用c ++发送电子邮件。但是,我无法配置它们并链接它们。因此,我想知道是否有人通过我的Gmail帐户在c ++(Windows 7操作系统)中发送电子邮件的源代码。
答案 0 :(得分:2)
如果你真的想要这么做,你必须使用像OpenSSL或Windows Schannel API这样的TLS库来建立与服务器的TLS连接。可以在此处找到如何执行此操作的示例:http://www.coastrd.com/c-schannel-smtp
但是,我认为让这些外部库工作会更容易。
答案 1 :(得分:1)
我总是和Poco一起建立运气。唯一的技巧是首先必须构建OpenSSL并将Poco构建脚本更改为其位置。
答案 2 :(得分:0)
如果这对任何人都有用,这里有一些C ++代码,基本上在Microsoft Windows上形成带有附件,主题和正文的电子邮件消息。它启动了默认的EMail(MAPI)客户端,其中添加了附件并填写了其他属性。唉,它不直接与SMTP服务器交互。我在我们的产品中使用它来让用户向我发送调试跟踪和什么不是。
如果你想使用下面的代码,你需要替换ptiString和ptiStringArray,它们只是用于管理字符串和字符串数组的内部类。 TObjList只是一个类容器管理模板。给出一个全部阵列。
希望这会有所帮助......
.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);
};
#endif
.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;
}