您好,登錄后才能下訂單哦!
這篇文章主要介紹了C語言如何實現發送郵件功能的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇C語言如何實現發送郵件功能文章都會有所收獲,下面我們一起來看看吧。
首先需要知道發送郵件的服務器IP地址和端口,我這里使用的是網易的126郵箱,如果是其他郵箱需要替換成其他的郵箱域名,域名和端口一般可以在郵箱網頁的幫助項里面找到。用域名比較方便維護以免后續更換ip之后還要修改代碼。
發送郵件一共需要8個步驟,具體見代碼main函數里面的注釋
代碼中的用戶名密碼以及收件人發件人等信息需要修改為你自己的。而且用戶名和密碼需要進行base64編碼后才能發送,具體base64算法沒有貼了,百度上到處都是。
完整代碼如下:
#include <stdio.h> #include <stdlib.h> #include "winsock2.h" extern int Base64Encod(char * data,char *output,int len,int *oputlen); #define EMAIL_SERVER "smtp.126.com" //按需修改 int CreateSocketAndConnect() { struct sockaddr_in server_addr; struct hostent *Addr; int ret; int sock = socket(AF_INET, SOCK_STREAM, 0); if(sock < 0){ printf("sock create failed\n"); return -1; } Addr = gethostbyname(EMAIL_SERVER); //用域名獲取對應IP if(Addr == NULL){ printf("get ip error\n"); return -1; } server_addr.sin_family = AF_INET; server_addr.sin_port = htons(25); server_addr.sin_addr.S_un.S_addr = (*(struct in_addr*)Addr->h_addr_list[0]).S_un.S_addr; ret = connect(sock, (struct sockaddr *)&server_addr, sizeof(server_addr)); if(ret < 0){ printf("connect server failed\n"); return -1; } return sock; } int server_send_recv_display(int sock, char *buff) { int ret = 0; char recvbuff[320] = {0}; send(sock, buff, strlen(buff), 0); ret = recv(sock, recvbuff, 320, 0); printf("服務器返回消息:len = %d \n recv:[%s] \n",ret,recvbuff); return ret; } /*HELO*/ int email_send_helo_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff, "HELO 126.com\r\n"); ret = server_send_recv_display(sock, buff); if(ret <= 0) { return -1; } return 0; } /*AUTH LOGIN 身份認證*/ int email_send_authlogin_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff,"%s","AUTH LOGIN\r\n"); ret = server_send_recv_display(sock, buff); if(ret <= 0) { return -1; } return 0; } //發送用戶名 int email_send_username_fun(int sock) { char buff[320] = {0}; char encbuff[120] = {0}; int ret, len = 0; sprintf(buff, "xxxxxxxxxx@126.com"); //需要改成自己的用戶名 Base64Encod(buff, encbuff, strlen(buff), &len); memset(buff, 0x00, sizeof(buff)); sprintf(buff,"%s\r\n",encbuff); ret = server_send_recv_display(sock, buff); if(ret <= 0) { return -1; } return 0; } //發送密碼 int email_send_password_fun(int sock) { char buff[320] = {0}; char encbuff[120] = {0}; int ret, len = 0; sprintf(buff, "123456789"); //需要改成自己的密碼,有的郵箱是需要填授權碼不是登錄用的密碼 Base64Encod(buff, encbuff, strlen(buff), &len); memset(buff, 0x00, sizeof(buff)); sprintf(buff, "%s\r\n", encbuff); ret = server_send_recv_display(sock, buff); if(ret < 0){ return -1; } return 0; } //發送發件人 int email_send_from_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff, "MAIL FROM: <xxxxxxxxxx@126.com>\r\n"); ret = server_send_recv_display(sock, buff); if(ret < 0){ return -1; } return 0; } //發送收件人 int email_send_to_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff, "RCPT TO: <xxxxxx@163.com>\r\n"); ret = server_send_recv_display(sock, buff); if(ret < 0){ return -1; } return 0; } //通知服務器準備發送郵件 int email_send_ready_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff, "DATA\r\n"); ret = server_send_recv_display(sock, buff); if(ret < 0){ return -1; } return 0; } //發送郵件 int email_send_maintext_fun(int sock) { char buff[320] = {0}; int ret; sprintf(buff, "From: \"pc\"<xxxxxxxxxx@126.com>\r\n" //發件人 "To: \"con\"<xxxxxx@163.com>\r\n" //收件人 "Subject: email test\r\n\r\n" //主題 "test:\r\n" //正文, 以 \r\n.\r\n 結束 " line1\r\n" " line2\r\n.\r\n"); ret = server_send_recv_display(sock, buff); if(ret < 0){ return -1; } return 0; } #define MAX_BUFF_SIZE 512 int main() { int sock; char buff[MAX_BUFF_SIZE]; WSADATA wsa; int ret; if(WSAStartup(MAKEWORD(1,1), &wsa) != 0){ printf("WSA startup failed\n"); return 1; } sock = CreateSocketAndConnect(); if(sock < 0){ return -1; } printf("connect to server success\n"); memset(buff, 0x00, sizeof(buff)); while(1){ ret = recv(sock, buff, MAX_BUFF_SIZE, 0); if(ret < 0){ printf("recv error\n"); continue; } printf("%s", buff); break; } //1.send helo if(email_send_helo_fun(sock) < 0){ return 1; } //2.send auth login if(email_send_authlogin_fun(sock) < 0){ return 1; } //3.send username if(email_send_username_fun(sock) < 0){ return 1; } //4.send password if(email_send_password_fun(sock) < 0){ return 1; } //5.send from if(email_send_from_fun(sock) < 0){ return 1; } //6.send to if(email_send_to_fun(sock) < 0){ return 1; } //7.send notice ready if(email_send_ready_fun(sock) < 0){ return 1; } //8.send email if(email_send_maintext_fun(sock) < 0){ return 1; } closesocket(sock); return 0; }
關于“C語言如何實現發送郵件功能”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“C語言如何實現發送郵件功能”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。