c++ - 如何用C++发送邮件

标签 c++ email smtp

我正在尝试使用 C++ visual studio 2013 发送电子邮件。通过在线搜索,我找到了一些这样做的代码示例,但我仍然无法发送电子邮件。在一些代码中,我得到一个错误,不能包含文件,没有这样的文件或目录

#include sys/socket.h

我也试过 boost,但卡住了。谁能帮我? 我试过的最后一个代码:

#include<iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>

using namespace std;
#define HELO "HELO 192.168.1.1\r\n"
#define DATA "DATA\r\n"
#define QUIT "QUIT\r\n"

//#define h_addr h_addr_list[0]
//FILE *fin;
int sock;
struct sockaddr_in server;
struct hostent *hp, *gethostbyname();
char buf[BUFSIZ+1];
int len;
char *host_id="192.168.1.10";
char *from_id="rameshgoli@domain.com";
char *to_id="rameshgoli@domain.com";
char *sub="testmail\r\n";
char wkstr[100]="hello how r u\r\n";

/*=====Send a string to the socket=====*/

void send_socket(char *s)
{
    write(sock,s,strlen(s));
    write(1,s,strlen(s));
    //printf("Client:%s\n",s);
}

//=====Read a string from the socket=====*/

void read_socket()
{
    len = read(sock,buf,BUFSIZ);
    write(1,buf,len);
    //printf("Server:%s\n",buf);
}

/*=====MAIN=====*/
int main(int argc, char* argv[])
{

    /*=====Create Socket=====*/
    sock = socket(AF_INET, SOCK_STREAM, 0);
    if (sock==-1)
    {  
        perror("opening stream socket");
        exit(1);
    }
    else
        cout << "socket created\n";

    /*=====Verify host=====*/
    server.sin_family = AF_INET;
    hp = gethostbyname(host_id);
    if (hp==(struct hostent *) 0)
    {
        fprintf(stderr, "%s: unknown host\n", host_id);
        exit(2);
    }

    /*=====Connect to port 25 on remote host=====*/
    memcpy((char *) &server.sin_addr, (char *) hp->h_addr, hp->h_length);
    server.sin_port=htons(25); /* SMTP PORT */
    if (connect(sock, (struct sockaddr *) &server, sizeof server)==-1)
    {
        perror("connecting stream socket");
        exit(1);
    }
    else
        cout << "Connected\n";
    /*=====Write some data then read some =====*/
    read_socket(); /* SMTP Server logon string */
    send_socket(HELO); /* introduce ourselves */
    read_socket(); /*Read reply */
    send_socket("MAIL FROM: "); 
    send_socket(from_id);
    send_socket("\r\n");
    read_socket(); /* Sender OK */
    send_socket("VRFY ");
    send_socket(from_id);
    send_socket("\r\n");
    read_socket(); // Sender OK */
    send_socket("RCPT TO: "); /*Mail to*/
    send_socket(to_id);
    send_socket("\r\n");
    read_socket(); // Recipient OK*/
    send_socket(DATA);// body to follow*/
    send_socket("Subject: ");
    send_socket(sub);
    read_socket(); // Recipient OK*/
    send_socket(wkstr);
    send_socket(".\r\n");
    read_socket(); 
    send_socket(QUIT); /* quit */
    read_socket(); // log off */

    //=====Close socket and finish=====*/
    close(sock);
    exit(0);
}

最佳答案

使用类似POCO 的网络库

http://pocoproject.org/docs/Poco.Net.SMTPClientSession.html

MailMessage msg;
msg.addRecipient (MailRecipient (MailRecipient::PRIMARY_RECIPIENT,
                                       "bob@example.com", "Bob"));
msg.setSender ("Me <me@example.com>");
msg.setSubject ("Subject");
msg.setContent ("Content");

SMTPClientSession smtp ("mail.example.com");
smtp.login ();
smtp.sendMessage (msg);
smtp.close ();

关于c++ - 如何用C++发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19767431/

相关文章:

c++ - 无法打开源文件 : 'WIN32' : No such file or directory

c++ - 使用 libdbus-c++ 获得了 DBus::Path - 下一步是什么?

java - 尝试在 Spring Boot 中发送电子邮件时出现 NoSuchMethodError

ruby-on-rails - 保存设计用户时出现Rails EOFError(到达文件末尾)

java - 如何在linux机器中导入jar文件

c++ - 使用通配符生成 Knuth–Morris–Pratt 前缀表

c++ - 使用 std::enable_if 禁用构造函数

python - 从 MatPlotLib Canvas 获取二进制图像数据?

PHP - 将特殊字符转换为 HTML 实体

c# - 不安装 SMTP 服务器发送邮件