c语言中发送邮件的函数 c语言 发邮件

C语言发送邮件

基本框架就是这,需要其他功能自己添加吧

创新互联专业为企业提供公主岭网站建设、公主岭做网站、公主岭网站设计、公主岭网站制作等企业网站建设、网页设计与制作、公主岭企业网站模板建站服务,十载公主岭做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

C语言实现简单的SMTP发送邮件

#include windows.h

#include stdio.h

#include WinSock.h

#include iostream

using namespace std;

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

struct Base64Date6

{

unsigned int d4:6;

unsigned int d3:6;

unsigned int d2:6;

unsigned int d1:6;

};

// 协议中加密部分使用的是base64方法

char ConvertToBase64 (char c6);

void EncodeBase64 (char *dbuf, char *buf128, int len);

void SendMail (char *email,char *body);

int OpenSocket (struct sockaddr *addr);

int main()

{

char EmailTo[] = "hanyinlong@126.com";

char EmailContents[] = "From: \"lucy\"hanyinlong@126.com\r\n"

"To: \"dasiy\"hanyinlong@126.com\r\n"

"Subject: Hello\r\n\r\n"

"Hello World, Hello Email!";

SendMail(EmailTo, EmailContents);

return 0;

}

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的实现

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;

}

// 发送邮件

void SendMail(char *email, 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);

hostent* hptr = gethostbyname("smtp.126.com");

memcpy(their_addr.sin_addr.S_un.S_addr, hptr-h_addr_list[0], hptr-h_length);

printf("IP of smpt.126.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);

// 连接邮件服务器,如果连接后没有响应,则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);

}

coutrbufendl;

// 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: "rbufendl;

// 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: "rbufendl;

// USER

memset(buf, 0, 1500);

sprintf_s(buf, 1500, "hanyinlong@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: "bufendl;

memset(rbuf, 0, 1500);

recv(sockfd, rbuf, 1500, 0);

cout"User Login Receive: "rbufendl;

// PASSWORD

sprintf_s(buf, 1500, "XXXXXXXXXXXX");//你的邮箱密码

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: "bufendl;

memset(rbuf, 0, 1500);

recv(sockfd, rbuf, 1500, 0);

cout"Send Password Receive: "rbufendl;

// MAIL FROM

memset(buf, 0, 1500);

sprintf_s(buf, 1500, "MAIL FROM: hanyinlong@126.com\r\n");

send(sockfd, buf, strlen(buf), 0);

memset(rbuf, 0, 1500);

recv(sockfd, rbuf, 1500, 0);

cout"set Mail From Receive: "rbufendl;

// 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: "rbufendl;

// DATA 准备开始发送邮件内容

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: "rbufendl;

// 发送邮件内容,\r\n.\r\n内容结束标记

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: "rbufendl;

// 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: "rbufendl;

//清理工作

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;

}

不明白C语言的SendMessage函数

不是等于VM_ 应该是WM_。

WM指的是WindowMessage 即windows消息。

以WM_开头的是预定义的windows消息。

windows是基于消息驱动的操作系统,当程序接收到系统发送的消息后会做出相应的响应。

例如程序向窗口发送一个 WM_PAINT消息,则窗口会重新绘制。

c语言中fputc是什么意思

fputc是C语言中的一个函数。功能是将字符ch写到文件指针fp所指向的文件的当前写指针的位置。函数格式:int fputc (int c, FILE *fp)。

在正常调用情况下,函数返回写入文件的字符的ASCII码值,出错时,返回EOF(-1)。当正确写入一个字符或一个字节的数据后,文件内部写指针会自动后移一个字节的位置。EOF是在头文件 stdio.h中定义的宏。

扩展资料

函数名称:写字符文件函数fputc()

函数格式:int fputc (int c, File *fp)

参数解释:fp为文件指针,值是执行fopen()打开文件时获得的。

c为输出的字符量。

虽然函数被定义为整型数,但仅用其低八位。

参考资料来源:百度百科-fputc


当前标题:c语言中发送邮件的函数 c语言 发邮件
分享URL:http://azwzsj.com/article/dospgcp.html