溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++發(fā)送郵件實現(xiàn)代碼

發(fā)布時間:2020-10-25 19:25:52 來源:腳本之家 閱讀:192 作者:Yetongfff 欄目:編程語言

本文實例為大家分享了C++發(fā)送郵件的具體代碼,供大家參考,具體內(nèi)容如下

首先,別忘了要設(shè)置發(fā)送郵箱的smtp,例如,假設(shè)你需要用網(wǎng)易郵箱,你需要去你的163郵箱設(shè)置開啟smtp(有的郵箱還需要設(shè)置授權(quán)碼)。接著就可以用以下代碼發(fā)送郵件了:

// SendMail.h
#ifndef _SEND_MAIL_H_
#define _SEND_MAIL_H_

#include <windows.h>
#include <stdio.h>
#include <WinSock.h>
#include <iostream>
using namespace std;

// 協(xié)議中加密部分使用的是base64方法
char ConvertToBase64(char c6);
void EncodeBase64(char *dbuf, char *buf128, int len);
void SendMail(char *email, const char *body);
int OpenSocket(struct sockaddr *addr);

#endif

// SendMail.cpp

#include "SendMail.h"

#pragma comment(lib, "ws2_32.lib")

struct Base64Date6
{
  unsigned int d4 : 6;
  unsigned int d3 : 6;
  unsigned int d2 : 6;
  unsigned int d1 : 6;
};


char ConvertToBase64(char uc)
{
  if (uc < 26)
  {
    return 'A' + uc;
  }
  if (uc < 52)
  {
    return 'a' + (uc - 26);
  }
  if (uc < 62)
  {
    return '0' + (uc - 52);
  }
  if (uc == 62)
  {
    return '+';
  }
  return '/';
}

// base64的實現(xiàn)
void EncodeBase64(char *dbuf, char *buf128, int len)
{
  struct Base64Date6 *ddd = NULL;
  int      i = 0;
  char     buf[256] = { 0 };
  char     *tmp = NULL;
  char     cc = '\0';

  memset(buf, 0, 256);
  strcpy_s(buf, 256, buf128);
  for (i = 1; i <= len / 3; i++)
  {
    tmp = buf + (i - 1) * 3;
    cc = tmp[2];
    tmp[2] = tmp[0];
    tmp[0] = cc;
    ddd = (struct Base64Date6 *)tmp;
    dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
    dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
    dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3);
    dbuf[(i - 1) * 4 + 3] = ConvertToBase64((unsigned int)ddd->d4);
  }
  if (len % 3 == 1)
  {
    tmp = buf + (i - 1) * 3;
    cc = tmp[2];
    tmp[2] = tmp[0];
    tmp[0] = cc;
    ddd = (struct Base64Date6 *)tmp;
    dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
    dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
    dbuf[(i - 1) * 4 + 2] = '=';
    dbuf[(i - 1) * 4 + 3] = '=';
  }
  if (len % 3 == 2)
  {
    tmp = buf + (i - 1) * 3;
    cc = tmp[2];
    tmp[2] = tmp[0];
    tmp[0] = cc;
    ddd = (struct Base64Date6 *)tmp;
    dbuf[(i - 1) * 4 + 0] = ConvertToBase64((unsigned int)ddd->d1);
    dbuf[(i - 1) * 4 + 1] = ConvertToBase64((unsigned int)ddd->d2);
    dbuf[(i - 1) * 4 + 2] = ConvertToBase64((unsigned int)ddd->d3);
    dbuf[(i - 1) * 4 + 3] = '=';
  }
  return;
}
// 發(fā)送郵件
void SendMail(char *email, const char *body)
{
  int   sockfd = { 0 };
  char  buf[1500] = { 0 };
  char  rbuf[1500] = { 0 };
  char  login[128] = { 0 };
  char  pass[128] = { 0 };
  WSADATA WSAData;
  struct sockaddr_in their_addr = { 0 };
  WSAStartup(MAKEWORD(2, 2), &WSAData);
  memset(&their_addr, 0, sizeof(their_addr));

  their_addr.sin_family = AF_INET;
  their_addr.sin_port = htons(25);  // 一般是25端口不需要改
  hostent* hptr = gethostbyname("smtp.126.com");     // 端口和服務(wù)器
  memcpy(&their_addr.sin_addr.S_un.S_addr, hptr->h_addr_list[0], hptr->h_length);
  printf("IP of smpt.163.com is : %d:%d:%d:%d\n",
    their_addr.sin_addr.S_un.S_un_b.s_b1,
    their_addr.sin_addr.S_un.S_un_b.s_b2,
    their_addr.sin_addr.S_un.S_un_b.s_b3,
    their_addr.sin_addr.S_un.S_un_b.s_b4);

  // 連接郵件服務(wù)器,如果連接后沒有響應(yīng),則2 秒后重新連接
  sockfd = OpenSocket((struct sockaddr *)&their_addr);
  memset(rbuf, 0, 1500);
  while (recv(sockfd, rbuf, 1500, 0) == 0)
  {
    cout << "reconnect..." << endl;
    Sleep(2);
    sockfd = OpenSocket((struct sockaddr *)&their_addr);
    memset(rbuf, 0, 1500);
  }

  cout << rbuf << endl;

  // EHLO
  memset(buf, 0, 1500);
  sprintf_s(buf, 1500, "EHLO HYL-PC\r\n");
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "EHLO REceive: " << rbuf << endl;

  // AUTH LOGIN
  memset(buf, 0, 1500);
  sprintf_s(buf, 1500, "AUTH LOGIN\r\n");
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Auth Login Receive: " << rbuf << endl;

  // USER
  memset(buf, 0, 1500);
  sprintf_s(buf, 1500, "xxxxxx@126.com");//你的郵箱賬號
  memset(login, 0, 128);
  EncodeBase64(login, buf, strlen(buf));
  sprintf_s(buf, 1500, "%s\r\n", login);
  send(sockfd, buf, strlen(buf), 0);
  cout << "Base64 UserName: " << buf << endl;
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "User Login Receive: " << rbuf << endl;

  // PASSWORD
  sprintf_s(buf, 1500, "********");//你的郵箱密碼
  memset(pass, 0, 128);
  EncodeBase64(pass, buf, strlen(buf));
  sprintf_s(buf, 1500, "%s\r\n", pass);
  send(sockfd, buf, strlen(buf), 0);
  cout << "Base64 Password: " << buf << endl;

  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Send Password Receive: " << rbuf << endl;

  // MAIL FROM
  memset(buf, 0, 1500);
  sprintf_s(buf, 1500, "MAIL FROM: <xxxx@126.com>\r\n"); //此處要和發(fā)郵件的郵箱保持一致
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "set Mail From Receive: " << rbuf << endl;

  // RCPT TO 第一個收件人
  sprintf_s(buf, 1500, "RCPT TO:<%s>\r\n", email);
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Tell Sendto Receive: " << rbuf << endl;

  // DATA 準(zhǔn)備開始發(fā)送郵件內(nèi)容
  sprintf_s(buf, 1500, "DATA\r\n");
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Send Mail Prepare Receive: " << rbuf << endl;

  // 發(fā)送郵件內(nèi)容,\r\n.\r\n內(nèi)容結(jié)束標(biāo)記
  sprintf_s(buf, 1500, "%s\r\n.\r\n", body);
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Send Mail Receive: " << rbuf << endl;

  // QUIT
  sprintf_s(buf, 1500, "QUIT\r\n");
  send(sockfd, buf, strlen(buf), 0);
  memset(rbuf, 0, 1500);
  recv(sockfd, rbuf, 1500, 0);
  cout << "Quit Receive: " << rbuf << endl;

  //清理工作
  closesocket(sockfd);
  WSACleanup();
  return;
}
// 打開TCP Socket連接
int OpenSocket(struct sockaddr *addr)
{
  int sockfd = 0;
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
  {
    cout << "Open sockfd(TCP) error!" << endl;
    exit(-1);
  }
  if (connect(sockfd, addr, sizeof(struct sockaddr)) < 0)
  {
    cout << "Connect sockfd(TCP) error!" << endl;
    exit(-1);
  }
  return sockfd;
}

測試?yán)蹋?/p>

// SendMailTest.cpp

#include "SendMail.h"

int main()
{
  int num = 100;
  string EmailContents = "From: \"lucy\"<xxxx@126.com>\r\n" 
              + string("To: \"dasiy\"<yyyy@126.com>\r\n")
              + "Subject: Hello\r\n\r\n" 
              + "test sending variable" + to_string(num) + "\n";

  char EmailTo[] = "yyyy@126.com";  //此處是送達的郵箱, 需和EmailContents里的保持一致


  SendMail(EmailTo, EmailContents.c_str());
  return 0;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI