如何使用c++; 我试图用C++ Visual Studio 2013发送电子邮件。通过在线搜索,我找到了一些这样做的代码示例,但是我仍然无法发送电子邮件。在某些代码中,出现了一个错误,即不能包含文件。在该代码中没有这样的文件或目录

如何使用c++; 我试图用C++ Visual Studio 2013发送电子邮件。通过在线搜索,我找到了一些这样做的代码示例,但是我仍然无法发送电子邮件。在某些代码中,出现了一个错误,即不能包含文件。在该代码中没有这样的文件或目录,c++,email,smtp,C++,Email,Smtp,#包括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 HE

#包括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);
}
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
#定义HELO“HELO 192.168.1.1\r\n”
#定义数据“数据\r\n”
#定义退出“退出\r\n”
//#定义h_地址h_地址列表[0]
//文件*fin;
int袜子;
服务器中的结构sockaddr_;
结构hostent*hp,*gethostbyname();
字符buf[BUFSIZ+1];
内伦;
char*host_id=“192.168.1.10”;
char*from_id=”rameshgoli@domain.com";
字符*到_id=”rameshgoli@domain.com";
char*sub=“testmail\r\n”;
char wkstr[100]=“你好,你好,r\n”;
/*====向套接字发送字符串=====*/
无效发送_套接字(字符*s)
{
书写(sock、s、strlen);
写(1,s,strlen);;
//printf(“客户端:%s\n”,s);
}
//====从套接字读取字符串=====*/
void read_socket()
{
len=读取(sock、buf、BUFSIZ);
写入(1,buf,len);
//printf(“服务器:%s\n”,buf);
}
/*====MAIN=====*/
int main(int argc,char*argv[])
{
/*====创建套接字=====*/
sock=socket(AF\u INET,sock\u STREAM,0);
如果(sock==-1)
{  
perror(“开放流插座”);
出口(1);
}
其他的
cout h_addr,hp->h_length);
server.sin_port=htons(25);/*SMTP端口*/
if(connect(sock,(struct sockaddr*)&server,sizeof server)=-1)
{
perror(“连接流插座”);
出口(1);
}
其他的

cout大多数头文件是特定于POSIX的,在Windows上不存在

而不是

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#包括
#包括
#包括
#包括
包括公正

#include <winsock2.h>
#包括
此外,在Windows上,您不使用
read
write
从套接字读取/写入,而是使用
recv
send


您可能想阅读。

使用网络库,如

MailMessage消息;
msg.addRecipient(MailRecipient)(MailRecipient::PRIMARY_RECIPIENT,
"bob@example.com","鲍勃"),;
msg.setSender(“我”);
msg.setSubject(“主题”);
msg.setContent(“内容”);
SMTPClientSession smtp(“mail.example.com”);
smtp.login();
smtp.sendMessage(msg);
smtp.close();

中提供的代码对我来说非常适合,无需使用外部库或进行艰苦的工作(只需下载并运行)。如果遇到任何问题,您也可以按照说明进行操作。

是否可以使用命令提示符发送邮件?是否查看邮件日志中的错误消息?
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 ();