您的位置:

C++实现邮件发送功能

一、C++邮件发送功能

C++实现邮件发送功能需要借助第三方库,比如Poco、OpenSSL等。使用这些库,可以通过SMTP协议在C++代码中嵌入发送邮件的功能,也可以在代码中直接调用邮件发送接口。以下是基于Poco库实现的C++邮件发送示例:

#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/StringPartSource.h>

using namespace Poco::Net;

int main(int argc, char** argv)
{
    // 从控制台参数中读取邮件发送参数
    std::string smtpServer = argv[1];
    std::string sender = argv[2];
    std::string recipient = argv[3];
    std::string subject = argv[4];
    std::string content = argv[5];
    
    // 构造邮件消息对象
    MailMessage message;
    message.setSender(sender);
    message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
    message.setSubject(subject);
    message.setContentType("text/plain; charset=UTF-8");
    message.setContent(content, MailMessage::ENCODING_8BIT);
    
    // 构造SMTP客户端会话对象
    SMTPClientSession smtpSession(smtpServer);
    smtpSession.login();
    smtpSession.sendMessage(message);
    smtpSession.close();
    
    return 0;
}

二、邮件发送功能流程图

邮件发送功能流程图如下:

三、Excel邮件发送功能

将Excel表格转换成邮件并发送也是常见的邮件发送需求。可以使用LibXL等第三方Excel操作库实现这一功能。以下是基于LibXL实现的Excel邮件发送示例:

#include <xl.h>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SMTPClientSession.h>

using namespace Poco::Net;

int main(int argc, char** argv)
{
    // 从控制台参数中读取邮件发送参数
    std::string smtpServer = argv[1];
    std::string sender = argv[2];
    std::string recipient = argv[3];
    std::string subject = argv[4];
    std::string fileName = argv[5];
    
    // 打开Excel表格
    BookHandle book = xlCreateBook();
    if (book)
    {
        if (xlBookLoad(book, fileName.c_str()))
        {
            SheetHandle sheet = xlBookGetSheet(book, 0);
            if (sheet)
            {
                int numRows = xlSheetLastRow(sheet);
                int numCols = xlSheetLastCol(sheet);
                std::string content;
                for (int r = 0; r < numRows; ++r)
                {
                    for (int c = 0; c < numCols; ++c)
                    {
                        content += xlSheetReadStr(sheet, r, c, NULL) + "\t";
                    }
                    content += "\n";
                }
                
                // 构造邮件消息对象并发送
                MailMessage message;
                message.setSender(sender);
                message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
                message.setSubject(subject);
                message.setContentType("text/plain; charset=UTF-8");
                message.setContent(content, MailMessage::ENCODING_8BIT);
                
                SMTPClientSession smtpSession(smtpServer);
                smtpSession.login();
                smtpSession.sendMessage(message);
                smtpSession.close();
            }
        }
        xlBookRelease(book);
    }
    
    return 0;
}

四、Word邮件发送功能

将Word文档转换成邮件并发送也是常见的邮件发送需求。可以使用Aspose Words等第三方Word操作库实现这一功能。以下是基于Aspose Words实现的Word邮件发送示例:

#include <Aspose.Words.Cpp/Model/Document/Document.h>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/StringPartSource.h>

using namespace Aspose::Words;
using namespace Poco::Net;

int main(int argc, char** argv)
{
    // 从控制台参数中读取邮件发送参数
    std::string smtpServer = argv[1];
    std::string sender = argv[2];
    std::string recipient = argv[3];
    std::string subject = argv[4];
    std::string fileName = argv[5];
    
    // 打开Word文档并读取内容
    std::unique_ptr doc(new Document(fileName));
    std::string content = doc->get_OriginalFileName();
    
    // 构造邮件消息对象并发送
    MailMessage message;
    message.setSender(sender);
    message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
    message.setSubject(subject);
    message.setContentType("text/plain; charset=UTF-8");
    message.setContent(content, MailMessage::ENCODING_8BIT);
    
    SMTPClientSession smtpSession(smtpServer);
    smtpSession.login();
    smtpSession.sendMessage(message);
    smtpSession.close();
    
    return 0;
}

  

五、Spring Boot邮件发送功能

与C++不同,Java有自带的邮件发送功能,可以直接使用JavaMail API实现邮件发送。在Spring Boot项目中,可以使用Spring Framework邮件发送模块简化邮件发送功能的实现。以下是基于Spring邮件发送模块实现的邮件发送示例:

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import java.util.Properties;

public class DemoApplication
{
    public static void main(String[] args)
    {
        // 读取邮件发送参数
        String smtpServer = args[0];
        String sender = args[1];
        String recipient = args[2];
        String subject = args[3];
        String content = args[4];
        
        // 构造邮件发送客户端对象
        JavaMailSender mailSender = new JavaMailSenderImpl();
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host", smtpServer);
        mailSender.setJavaMailProperties(properties);
        
        // 构造邮件消息对象并发送
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sender);
        message.setTo(recipient);
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }
}

六、代码完整示例

以下是C++实现邮件发送功能的完整示例代码,其中包括了C++、Excel、Word三个功能的实现:
#include <xl.h>
#include <Aspose.Words.Cpp/Model/Document/Document.h>
#include <Poco/Net/MailMessage.h>
#include <Poco/Net/SMTPClientSession.h>
#include <Poco/Net/StringPartSource.h>

using namespace Poco::Net;

void sendMail(const std::string& smtpServer, const std::string& sender, const std::string& recipient, const std::string& subject, const std::string& content)
{
    MailMessage message;
    message.setSender(sender);
    message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT, recipient));
    message.setSubject(subject);
    message.setContentType("text/plain; charset=UTF-8");
    message.setContent(content, MailMessage::ENCODING_8BIT);
    
    SMTPClientSession smtpSession(smtpServer);
    smtpSession.login();
    smtpSession.sendMessage(message);
    smtpSession.close();
}

void sendExcel(const std::string& smtpServer, const std::string& sender, const std::string& recipient, const std::string& subject, const std::string& fileName)
{
    BookHandle book = xlCreateBook();
    if (book)
    {
        if (xlBookLoad(book, fileName.c_str()))
        {
            SheetHandle sheet = xlBookGetSheet(book, 0);
            if (sheet)
            {
                int numRows = xlSheetLastRow(sheet);
                int numCols = xlSheetLastCol(sheet);
                std::string content;
                for (int r = 0; r < numRows; ++r)
                {
                    for (int c = 0; c < numCols; ++c)
                    {
                        content += xlSheetReadStr(sheet, r, c, NULL) + "\t";
                    }
                    content += "\n";
                }
                
                sendMail(smtpServer, sender, recipient, subject, content);
            }
        }
        xlBookRelease(book);
    }
}

void sendWord(const std::string& smtpServer, const std::string& sender, const std::string& recipient, const std::string& subject, const std::string& fileName)
{
    std::unique_ptr<Aspose::Words::Document> doc(new Aspose::Words::Document(fileName));
    std::string content = doc->get_OriginalFileName();
    sendMail(smtpServer, sender, recipient, subject, content);
}

int main(int argc, char** argv)
{
    std::string smtpServer = argv[1];
    std::string sender = argv[2];
    std::string recipient = argv[3];
    std::string subject = argv[4];
    std::string fileName = argv[5];
    
    std::string extension = fileName.substr(fileName.find_last_of(".") + 1);
    if (extension == "xls" || extension == "xlsx")
    {
        sendExcel(smtpServer, sender, recipient, subject, fileName);
    }
    else if (extension == "doc" || extension == "docx")
    {
        sendWord(smtpServer, sender, recipient, subject, fileName);
    }
    else
    {
        std::ifstream ifs(fileName);
        std::string content((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
        sendMail(smtpServer, sender, recipient, subject, content);
    }
    
    return 0;
}