一、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; }
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/295859.html