⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 smtpio.c

📁 LINUX下的收发E-MAIL的程序
💻 C
📖 第 1 页 / 共 2 页
字号:
{
//      we are going to reset the smtp server (just to be nice) 
    char buffer[256];
    char command[8];

    strncpy(command, "RSET\r\n", 255);
    if (send(sock, command, strlen(command), 0) < 0) {
	smtperrno = S_ERR_RSET_SEND;
	return ERROR;
    }

    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, ReqMlActOK) == -1)) {
	smtperrno = S_ERR_RSET_RECV;
	return ERROR;
    }
    return SUCCESS;
}


int eml_smtp_quit(const int sock)
{
    // lets tell the server we want to quit now 
    char buffer[256];
    char command[8];

    strncpy(command, "QUIT\r\n", 255);
    if (send(sock, command, strlen(command), 0) < 0) {
	smtperrno = S_ERR_QUIT_SEND;
	return ERROR;
    }

    if ((recvline(sock, buffer, 255) < 0)
	|| (find_string(buffer, ServClose) == -1)) {
	smtperrno = S_ERR_QUIT_RECV;
	return ERROR;
    }
    return SUCCESS;
}


int eml_smtp_close(int sock)
{
    close(sock);
    return SUCCESS;
}


int eml_smtp_open(Server * server)
{
    char buffer[513];
    int sock = -1;

    smtperrno = S_NO_ERROR;
    if (!Resolve(server)) {
	smtperrno = S_ERR_RESOLVE;
	return -1;
    }

    sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (sock < 0) {
	smtperrno = S_ERR_SOCKET;
	return sock;
    }

    server->sin.sin_family = AF_INET;
    server->sin.sin_port = htons(server->port);

    if (connect_timeo
	(sock, (struct sockaddr *) &server->sin, sizeof(server->sin),
	 emlTimeout) < 0) {
	smtperrno = S_ERR_CONNECT;
	close(sock);
	return -1;
    }

    if ((recvline(sock, buffer, 512) < 0)
	|| (find_string(buffer, ServiceRdy) == -1)) {
//      the smtp server failed to send a response (usually version info) 
	smtperrno = S_ERR_SERV_ACK;
	close(sock);
	return -1;
    }
//      Because some servers send more than just their version info...
//      (like NT SMTP servers...), lets keep checking the buffer until
//      there is nothing left. 
    // TODO: Fix this so that we don't have to do this horrid hack 
    if (find_string(buffer, "Microsoft") > -1) {
	recvline(sock, buffer, 512);
    } else if (find_string(buffer, "earthlink.net") > -1) {
	// Earthlink likes to send 2 extra lines of data... 
	recvline(sock, buffer, 512);
	recvline(sock, buffer, 512);

    }
    return sock;
}


int eml_send_mail_init(Server * server)
{
    int sock;
    smtperrno = S_NO_ERROR;

    sock = eml_smtp_open(server);
    if (sock < 0) {
	return sock;
    }
    if (!eml_smtp_helo(sock)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    return sock;
}


int eml_send_mail_active(int sock, Email * msg)
{
    int fields, i;
    char *buff = NULL;

    if (!eml_smtp_mail(sock, msg->from_addr)) {
	eml_smtp_close(sock);
	return ERROR;
    }
//      Determine number of direct recipients.  Do not CC if no direct 
//      recipients are specified. 
    fields = get_num_fields(msg->to_addr, ';');
    if (fields > 0) {
	char *orig_rcpt = get_field(msg->to_addr, 1, ';');
// Do a RCPT TO for each of the direct recipients. 
	for (i = 1; i <= fields; i++) {
	    if (i != 1)
		buff = get_field(msg->to_addr, i, ';');
	    else
		buff = orig_rcpt;

	    if (!eml_smtp_rcpt(sock, buff, (char *) NULL)) {
		free(buff);
		eml_smtp_close(sock);
		return ERROR;
	    }

	    if (i != 1)
		free(buff);
	}

	// Do a RCPT TO for each of the carbon copies.
	fields = get_num_fields(msg->cc_addr, ';');
	if (fields > 0) {
	    for (i = 1; i <= fields; i++) {
		buff = get_field(msg->cc_addr, i, ';');
		if (!eml_smtp_rcpt(sock, buff, orig_rcpt)) {
		    free(buff);
		    eml_smtp_close(sock);
		    return ERROR;
		}
		free(buff);
	    }
	}
	free(orig_rcpt);
    }
    if (!eml_smtp_data(sock, msg->header, msg->body)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    if (!eml_smtp_rset(sock)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    if (!eml_smtp_quit(sock)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    eml_smtp_close(sock);	// close the socket 
    msg->status = msg->status | DELIVERED;	//update status to delivered
//      eml_free_mail(msg);
    return SUCCESS;		// everything went smoothly
}


int eml_send_mail_needAuth(Server * server, Email * msg, char *username,
			   char *password)
{
    int sock;
    msg->status = (msg->status & 0) | DELIVERING;
    sock = eml_send_mail_init(server);
    if (sock <= 0) {
	return ERROR;
    }
    if (!eml_smtp_auth(sock, username, password)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    if (!eml_send_mail_active(sock, msg)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    return SUCCESS;		// everything went smoothly :) 
}


int eml_send_mail_noNeedAuth(Server * server, Email * msg)
{
    int sock;
    msg->status = (msg->status & 0) | DELIVERING;
    sock = eml_send_mail_init(server);
    if (sock <= 0) {
	return ERROR;
    }
    if (!eml_send_mail_active(sock, msg)) {
	eml_smtp_close(sock);
	return ERROR;
    }
    return SUCCESS;		// everything went smoothly :) 
}


int eml_prepare_mail(char *smtphost, int port, char *from, char *to,
		     char *cc, char *subject, char *textbody,
		     Mlist * mimefiles)
{
    char *body = NULL;
    char *boundary = NULL;
    char *buffer = NULL;
    Mlist *tmp = NULL;
    int index;
    char xmailer[] = "HUC mail 1.0 \0";

    mymail.status = UNDELIVERED;
    if (mymail.status & UNDELIVERED) {
	snprintf(mymail.xmailer, 32, xmailer);
    }

    snprintf(smtpserver.hostname, 32, smtphost);

    smtpserver.port = port;

    snprintf(mymail.from_addr, 32, from);

    snprintf(mymail.to_addr, 80, to);

    trim_whtspc(cc);
    snprintf(mymail.cc_addr, 80, cc);

    buffer = make_8bit(subject);
    trim_whtspc(buffer);
    snprintf(mymail.subject, 64, buffer);

    free(buffer);


//      Set the body of the message into the structure  
    if (mimefiles != NULL) {
	if ((boundary = strdup(eml_mime_get_boundary())) == NULL) {
	    free(boundary);
	    goto bad;
	}
	body = g_malloc0(200 + strlen(boundary) + strlen(textbody));
	index = 0;
	index +=
	    sprintf(body,
		    "This is a multi-part message in MIME format.\n\n");

	if ((buffer = strdup(textbody)) == NULL) {
	    free(buffer);
	    goto bad;
	}
	index += sprintf(body + index, "--%s\n", boundary);
	index +=
	    sprintf(body + index,
		    "Content-Type: text/plain\nContent-Transfer-Type: 8bit\n\n%s",
		    word_wrap(buffer, 80));
	free(buffer);
	tmp = mimefiles;
	while (tmp != NULL) {
	    index +=
		eml_mime_insert_part(&body, index, boundary,
				     (char *) tmp->data);
	    tmp = tmp->next;
	}
	body = realloc(body, strlen(body) + strlen(boundary) + 7);
	// insert the end marking boundary 
	sprintf(body + index, "--%s--\n", boundary);
	body[index + 2 + strlen(boundary) + 3] = '\0';

	snprintf(mymail.body, 1024, body);

	free(body);
    } else {
	index = strlen(textbody);
//              snprintf(mymail.body,1024,textbody);
	printf("before encode: %s\n", textbody);
	snprintf(mymail.body, 1024,
		 word_wrap(eml_mime_encode_base64(textbody, &index, 0),
			   80));
	printf("after encode: %s\n", mymail.body);
    }

//   setup the mime stuff       
    if (mimefiles != NULL) {
	snprintf(mymail.mime_version, 32, "1.0");
	buffer = g_malloc0(sizeof(char) * 76);
	sprintf(buffer, "multipart/mixed; boundary=\"%s\"\0", boundary);

	snprintf(mymail.content_type, 64, buffer);
	free(buffer);
    } else {
//      for now we just use this, later on we should autodetect
//      if its 7bit or 8bit or if we should use quoted_printable etc.. 
	snprintf(mymail.mime_version, 32, "1.0");

	snprintf(mymail.content_transfer_encoding, 64, "base64");

	snprintf(mymail.content_type, 64, "text/plain;charset=\"gb2312\"");
    }
    format_header(&mymail);

    return SUCCESS;

  bad:smtperrno = S_ERR_PREPARE_MAIL_SERVER;
    printf("prepare failed!!!!!!\n");
    return ERROR;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -