gpt4 book ai didi

c - 通过 C 套接字编程的邮件格式

转载 作者:行者123 更新时间:2023-11-30 14:50:43 25 4
gpt4 key购买 nike

我正在学习 C 语言的套接字编程,学习某些东西的最好方法就是实现它。因此,我编写了一个简单的程序,通过 C 套接字编程发送带有附件和一些文本正文的邮件。到目前为止,该代码是成功的,只有一个异常(exception) - 格式化。 MIME header 显示在邮件正文中(附有屏幕截图),并且邮件主题全部困惑。

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#include <stdio.h>

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

#define SMTP_PORT "25"
#define DEFAULT_BUFLEN 1024

int sendData(SOCKET* socket, const char* data)
{
int iResult;
printf("%s", data);
iResult = send(*socket, data, (int)strlen(data), 0);
if (iResult == SOCKET_ERROR) {
printf("send failed (msg: %s): %d\n", data, WSAGetLastError());
}
return iResult;
}

void recvData(SOCKET* socket)
{
int recvbuflen = DEFAULT_BUFLEN;
char recvbuf[DEFAULT_BUFLEN];
int iResult;
iResult = recv(*socket, recvbuf, recvbuflen - 1, 0);
printf("In recvData function: %d\n", iResult);
if (iResult > 0) {
recvbuf[iResult] = '\0';
printf("%s", recvbuf);
}
else if (iResult == 0)
printf("Connection closed\n");
else
printf("recv failed: %d\n", WSAGetLastError());
}

int main(int argc, char** argv)
{
WSADATA wsaData;

int iResult;
iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
printf("WSAStartup failed: %d\n", iResult);
return 1;
}

struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;

ZeroMemory(&hints, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

iResult = getaddrinfo("mail.sharklasers.com", SMTP_PORT, &hints, &result);
if (iResult != 0) {
printf("getaddrinfo failed: %d\n", iResult);
WSACleanup();
return 1;
}

SOCKET ConnectSocket = INVALID_SOCKET;
for (ptr = result; ptr != NULL; ptr = ptr->ai_next) {
ConnectSocket = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
if (ConnectSocket == INVALID_SOCKET) {
printf("socket failed: %ld\n", WSAGetLastError());
WSACleanup();
return 1;
}

iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
if (iResult == SOCKET_ERROR) {
closesocket(ConnectSocket);
ConnectSocket = INVALID_SOCKET;
continue;
}
break;
}
freeaddrinfo(result);

if (ConnectSocket == INVALID_SOCKET) {
printf("Unable to connect to server!\n");
WSACleanup();
return 1;
}

sendData(&ConnectSocket, "HELO mail.sharklasers.com\r\n");
recvData(&ConnectSocket);
sendData(&ConnectSocket, "MAIL FROM: psgibrxp@sharklasers.com\r\n");
recvData(&ConnectSocket);
sendData(&ConnectSocket, "RCPT TO: psgibrxp@sharklasers.com\r\n");
recvData(&ConnectSocket);
sendData(&ConnectSocket, "DATA\r\n");
recvData(&ConnectSocket);
sendData(&ConnectSocket, "Subject:This is subject of my mail\r\n");
sendData(&ConnectSocket, "And this is text\r\n");
sendData(&ConnectSocket, "MIME-Version: 1.0\r\n");
sendData(&ConnectSocket, "Content-Type:multipart/mixed;boundary=\"KkK170891tpbkKk__FV_KKKkkkjjwq\"\r\n");
sendData(&ConnectSocket, "--KkK170891tpbkKk__FV_KKKkkkjjwq\r\n");
sendData(&ConnectSocket, "Content-Transfer-Encoding: quoted-printable\r\n");
sendData(&ConnectSocket, "Content-Type: text/plain;name=\"details.txt\"\r\n");
sendData(&ConnectSocket, "Content-Disposition: attachment; filename=\"details.txt\"\r\n");
sendData(&ConnectSocket, "\r\n\r\n");

FILE* MailFilePtr = fopen("details.txt", "r");
if (MailFilePtr == NULL)
printf("Error opening attachment\n");

char FileBuffer[1024];
char buf[1024];

memset(FileBuffer, 0, sizeof(FileBuffer));
while (fgets(FileBuffer, sizeof(FileBuffer), MailFilePtr))
{
sprintf(buf, "%s", FileBuffer);
buf[strlen(buf) - 1] = 0;
sendData(&ConnectSocket, buf);
memset(FileBuffer, 0, sizeof(FileBuffer));
memset(buf, 0, sizeof(buf));
}

fclose(MailFilePtr);

sendData(&ConnectSocket, "\r\n\r\n--KkK170891tpbkKk__FV_KKKkkkjjwq--\r\n\r\n");
sendData(&ConnectSocket, ".\r\n");
recvData(&ConnectSocket);

sendData(&ConnectSocket, "QUIT\r\n");

iResult = shutdown(ConnectSocket, SD_SEND);
if (iResult == SOCKET_ERROR) {
printf("shutdown failed: %d\n", WSAGetLastError());
closesocket(ConnectSocket);
WSACleanup();
return 1;
}

closesocket(ConnectSocket);
WSACleanup();
return 0;
}

问题:如何获取正确格式的邮件正文?我想我缺少一些 SMTP 命令或命令序列,但我不确定。

注意

  1. 我现在使用 Guerrilla Mail,因为 Gmail 需要 SSL\TLS 来进行通信(如果我错了,请纠正我)。我将为此使用 openssl 库。
  2. 这不是最终代码。我确实必须清理一些代码。不过,我们也欢迎提供代码审查建议。
  3. 我是来学习的,所以欢迎任何与C语言和网络编程相关的链接、书籍引用。

编辑

编辑的代码基于迄今为止的评论和答案。不幸的是问题仍然存在。我没有读取文件,而是直接传递 base64 编码的附件(这是有效的)。编辑后的代码的输出如下面的屏幕截图所示

    sendData(&ConnectSocket, "MIME-Version: 1.0\r\n");
sendData(&ConnectSocket, "Content-Type:multipart/mixed;boundary=\"977d81ff9d852ab2a0cad646f8058349\"\r\n");
sendData(&ConnectSocket, "Subject:This is subject of my mail\r\n");
sendData(&ConnectSocket, "--977d81ff9d852ab2a0cad646f8058349\r\n");
sendData(&ConnectSocket, "Content-Type: text/plain; charset=\"utf-8\"\r\n");
sendData(&ConnectSocket, "Content-Transfer-Encoding: quoted-printable\r\n\r\n");
sendData(&ConnectSocket, "Hi Me,=0A=0AThis is an empty file.=0A=0ARegards,=0A<ME>=0A=0A---- =0ASent using Guerrillamail.com =0ABlock or report abuse : https://www.guerrillamail.com//abuse/?a=3DUVJzDA8SW6Q1mwa14nUTcwfCX9ne0dhd=0A \r\n\r\n");
sendData(&ConnectSocket, "--977d81ff9d852ab2a0cad646f8058349\r\n");
sendData(&ConnectSocket, "Content-Type: text/plain\r\n");
sendData(&ConnectSocket, "Content-Transfer-Encoding: base64\r\n");
sendData(&ConnectSocket, "Content-Disposition: attachment; filename=\"details.txt\"\r\n\r\n");
sendData(&ConnectSocket, "U2FtcGxlIFRleHQu");
sendData(&ConnectSocket, "\r\n\r\n--977d81ff9d852ab2a0cad646f8058349--\r\n\r\n");
sendData(&ConnectSocket, ".\r\n");
recvData(&ConnectSocket);
sendData(&ConnectSocket, "QUIT\r\n");

编辑后的输出邮件屏幕截图:

Output Mail Screenshot

最佳答案

正文需要用空行与标题分隔。

sendData(&ConnectSocket, "MIME-Version: 1.0\r\n");
sendData(&ConnectSocket, "Content-Type:multipart/mixed;boundary=\"977d81ff9d852ab2a0cad646f8058349\"\r\n");
sendData(&ConnectSocket, "Subject:This is subject of my mail\r\n");
sendData(%ConnectSocket, "\r\n"); /* added */
sendData(&ConnectSocket, "--977d81ff9d852ab2a0cad646f8058349\r\n");
sendData(&ConnectSocket, "Content-Type: text/plain; charset=\"utf-8\"\r\n");
sendData(&ConnectSocket, "Content-Transfer-Encoding: quoted-printable\r\n\r\n");
sendData(&ConnectSocket, "Hi Me,=0A=0AThis is an empty file.=0A=0ARegards,=0A<ME>=0A=0A---- =0ASent using Guerrillamail.com =0ABlock or report abuse : https://www.guerrillamail.com//abuse/?a=3DUVJzDA8SW6Q1mwa14nUTcwfCX9ne0dhd=0A \r\n\r\n");
sendData(&ConnectSocket, "--977d81ff9d852ab2a0cad646f8058349\r\n");
sendData(&ConnectSocket, "Content-Type: text/plain\r\n");
sendData(&ConnectSocket, "Content-Transfer-Encoding: base64\r\n");
sendData(&ConnectSocket, "Content-Disposition: attachment; filename=\"details.txt\"\r\n\r\n");
sendData(&ConnectSocket, "U2FtcGxlIFRleHQu");
sendData(&ConnectSocket, "\r\n\r\n--977d81ff9d852ab2a0cad646f8058349--\r\n\r\n");
sendData(&ConnectSocket, ".\r\n");

当您向 SMTP 发送内容时,您应该正确检查结果代码是否符合预期(大多数命令为 2xx,DATA 为 345)。

随着时间的推移,使用静态边界字符串将无法正常工作(尽管我认为如果您发现它嵌入在内容中,您可以通过编码边界字符串来替换或混淆)。

当消息长度超过大约 1,000 个字符时,将整个消息作为单个长行发送将开始中断。 Quoted-printable 有用的原因之一是它允许您在内容中透明地嵌入换行符(尽管在这种情况下,仅在消息包含换行符的情况下发送文字换行符就已经让您走得很远了)。

格式良好的消息还应具有 From:To: header 。

关于c - 通过 C 套接字编程的邮件格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48862157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com