代码为C++

问题描述:

嗨,大家好发送电子邮件,代码为C++

我知道有外部库有像jwsmtp和vmime或POCO,帮助您在C发送电子邮件++。但是,我无法配置它们并链接它们。因此,我想知道是否有人通过我的gmail帐户拥有用C++(windows 7 os)发送电子邮件的源代码。

如果您确实想要这么做,您必须使用TLS库(如OpenSSLWindows Schannel API)与服务器建立TLS连接。这样做的一个例子可以在这里找到:http://www.coastrd.com/c-schannel-smtp

但是,我认为这将是更容易得到这些外部库的工作。

我一直有与波科建立运气。唯一的技巧是首先你必须构建OpenSSL并将Poco构建脚本更改为它的位置。

如果对任何人都有用,下面是一些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; 
}