使用Poco通过Gmail发送电子邮件

时间:2014-07-24 04:14:58

标签: c++ email poco

您好我有这个课程尝试使用Poco库发送电子邮件

CHSEmailSender::CHSEmailSender(int port,
                               string host,
                               string user,
                               string password)
    : port_(port),
      host_(host),
      user_(user),
      password_(password) {
}

void CHSEmailSender::sendEmail(string primary_recipient,
                               vector<string> cc_recipients,
                               string sender,
                               string subject,
                               string content) {
  try {
    SharedPtr<InvalidCertificateHandler> certificate_handler
        = new AcceptCertificateHandler(false);
    AutoPtr<Context> context = new Context(Context::CLIENT_USE, "", "", "",
                                           Context::VERIFY_RELAXED, 9, true,
                                           "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
    SSLManager::instance().initializeClient(NULL, certificate_handler, context);
  } catch (Exception& exception) {
    std::cout << exception.displayText() << std::endl;
  }

  MailMessage message;
  message.setSender(sender);
  message.addRecipient(MailRecipient(MailRecipient::PRIMARY_RECIPIENT,
                                     primary_recipient));
  for (auto recipient : cc_recipients) {
    message.addRecipient(MailRecipient(MailRecipient::CC_RECIPIENT,
                                       recipient));
  }
  message.setSubject(subject);
  message.setContentType("text/plain; charset=UTF-8");
  message.setContent(content, MailMessage::ENCODING_8BIT);

  SMTPClientSession session(SecureStreamSocket(SocketAddress(host_, port_)));
  session.open();
  try {
    session.login(SMTPClientSession::AUTH_LOGIN, user_, password_);
    session.sendMessage(message);
    session.close();
  } catch (Exception& exception) {
    std::cout << 3 << std::endl;
    std::cout << exception.displayText() << std::endl;
  }
}

CHSEmailSender::~CHSEmailSender() {
}

尝试通过设置实例运行一些测试并运行此

 for (int i = 0; i < 10; i++) {
    email_sender->sendEmail("xxxx@gmail.com", {}, "xxxx",
                            "email sender test", "test");
 }

然后我收到了这个错误

std::exception: Certificate validation error

有时,它有效。我收到了一些电子邮件。但有时,它会失败。我不想半失败计划。尝试解决它。无法弄清楚原因。

任何人都可以告诉我为什么?

0 个答案:

没有答案