블로그 (Blog)/개발로그 (Devlogs)

SMTP 서버 + 보안연결 메일 보내기

ihmin 2024. 3. 15. 10:44

출처: http://www.codeproject.com/Articles/98355/SMTP-Client-with-SSL-TLS

공개된 CSmtp 라이브러리를 사용하여.. 메일 보내기..

CSmtp_v2_3_ssl.zip
1.74MB


아래는 MinGW를 사용하여 gmail의 TLS 보안 연결로 메일 보내는 예제..
OpenSSL 라이브러리 필요하며, 리눅스에서도 사용 가능..

#include "CSmtp.h"
#include <iostream>
 
int main()
{
    bool bError = false;
 
    try
    {
        CSmtp mail;
 
#define test_gmail_tls
 
#if defined(test_gmail_tls)
        mail.SetSMTPServer("smtp.gmail.com",587);
        mail.SetSecurityType(USE_TLS);
#elif defined(test_gmail_ssl)
        mail.SetSMTPServer("smtp.gmail.com",465);
        mail.SetSecurityType(USE_SSL);
#elif defined(test_hotmail_TLS)
        mail.SetSMTPServer("smtp.live.com",25);
        mail.SetSecurityType(USE_TLS);
#elif defined(test_aol_tls)
        mail.SetSMTPServer("smtp.aol.com",587);
        mail.SetSecurityType(USE_TLS);
#elif defined(test_yahoo_ssl)
        mail.SetSMTPServer("plus.smtp.mail.yahoo.com",465);
        mail.SetSecurityType(USE_SSL);
#endif
 
        mail.SetLogin("user_id");
        mail.SetPassword("user_password");
        mail.SetSenderName("User");
        mail.SetSenderMail("user@domain.com");
        mail.SetReplyTo("user@domain.com");
        mail.SetSubject("The message");
        mail.AddRecipient("friend@domain2.com");
        mail.SetXPriority(XPRIORITY_NORMAL);
        mail.SetXMailer("The Bat! (v3.02) Professional");
        mail.AddMsgLine("Hello,");
        mail.AddMsgLine("");
        mail.AddMsgLine("...");
        mail.AddMsgLine("How are you today?");
        mail.AddMsgLine("");
        mail.AddMsgLine("Regards");
        mail.ModMsgLine(5,"regards");
        mail.DelMsgLine(2);
        mail.AddMsgLine("User");
 
        //mail.AddAttachment("../test1.jpg");
        //mail.AddAttachment("c:\\test2.exe");
        //mail.AddAttachment("c:\\test3.txt");
        mail.Send();
    }
    catch(ECSmtp e)
    {
        std::cout << "Error: " << e.GetErrorText().c_str() << ".\n";
        bError = true;
    }
    if(!bError)
        std::cout << "Mail was send successfully.\n";
    return 0;
}