- 论坛徽章:
- 0
|
发送邮件的函数如下:
int send_mail(char *smtp_server, char *passwd, char *send_from, const char *send_to, char *body)
{
struct timeval tv;
int sockfd;
int i,cnt,rt;
int auth_base64_flag;
unsigned char buffer[100*1024], *ptr, username[MAXLINE];
int first_auth = FALSE;
struct hostent *hent;
char *cmdSMTP = NULL;
if ( (NULL == smtp_server) || (NULL == send_from) ||
(NULL == body) || ( NULL == passwd ) || (NULL == send_to) )
{
printf("smtp_server=%s send_from=%s body=%s passwd=%s send_to=%s\n", \
smtp_server,send_from,body,passwd,send_to);
return -1;
}
strncpy(username, send_from, MAXLINE);
sockfd = ConnectServer(smtp_server, 25, 20);
if (SOCKET_ERROR == sockfd)
{
mail_error("open socket");
goto error;
}
tv.tv_sec = 10;
tv.tv_usec = 0;
if ( setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0)
{
mail_error("setsockopt");
goto error;
}
getresponse(sockfd, buffer, MAXLINE);
if ( strncmp(buffer, "220", 3) )
{
mail_error("not smtp server");
goto error;
}
sprintf(buffer, "EHLO %s\r\n", smtp_server);
if (write(sockfd, buffer, strlen(buffer)) < 0)
{
mail_error("EHLO");
goto error;
}
auth_base64_flag = 0;
do
{
rt = getresponse(sockfd, buffer, MAXLINE);
if (rt <= 0)
{
mail_error("getresponse");
goto error;
}
printf("1::::::%s\n", buffer);
if ( !strncmp("250-AUTH", buffer, 8) && strstr(buffer, "LOGIN") )
auth_base64_flag = 1;
} while (strncmp(buffer, "250 ", 4) != 0) ;
printf("auth_base64_flag = %d\n", auth_base64_flag);
if (auth_base64_flag != 0)
{
cmdSMTP = "AUTH LOGIN\r\n";
if ( write(sockfd,"AUTH LOGIN\r\n", 12) < 0 )
goto error;
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
goto error;
printf("2::::::%s\n", buffer);
if ( 0 != strncmp("334 ", buffer,4) )
goto error;
//write the base64 crypted username
printf("username:::%s\n", username);
encrypt_b64(buffer, username, strlen(username) );
strcat(buffer, "\r\n");
if ( write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
goto error;
printf("3::::::%s\n", buffer);
if ( 0 != strncmp("334 ", buffer, 4) )
{
first_auth = FALSE;
}
else
{
first_auth = TRUE;
}
//write the base64 crypted passwd
printf("password:::%s\n", passwd);
encrypt_b64(buffer, passwd, strlen(passwd) );
strcat(buffer, "\r\n");
if ( write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
goto error;
printf("4::::::%s\n", buffer);
if ( 0 != strncmp("235 ", buffer, 4) )
{
first_auth = FALSE;
}
else
{
first_auth = TRUE;
}
// auth ok!
}
// the first auth failed , try use the send_from as the email account
if ( auth_base64_flag != 0 && first_auth == FALSE )
{
printf("login as email account...\n\n");
if ( write(sockfd, "AUTH LOGIN\r\n", 12) < 0 )
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( 0 != strncmp("334 ",buffer,4) )
{
printf("buffer content:%s\n", buffer);
printf("%d: error\n", __LINE__);
goto error;
}
//write the base64 crypted username
encrypt_b64(buffer, send_from, strlen(send_from) );
strcat(buffer,"\r\n");
if ( write(sockfd, buffer, strlen(buffer)) < 0)
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( 0 != strncmp("334 ",buffer,4) )
{
printf("%d: error\n", __LINE__);
goto error;
}
//write the base64 crypted passwd
encrypt_b64(buffer, passwd, strlen(passwd) );
strcat(buffer, "\r\n");
if ( write(sockfd, buffer, strlen(buffer)) < 0)
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( getresponse(sockfd, buffer, MAXLINE) < 0)
{
printf("%d: error\n", __LINE__);
goto error;
}
if ( 0 != strncmp("235 ",buffer,4) )
{
printf("%d: error\n", __LINE__);
goto error;
}
// auth ok!
}
sprintf(buffer, "MAIL FROM: <%s>\r\n", send_from);
write(sockfd, buffer, strlen(buffer)); //2008-12-01 modify
getresponse(sockfd, buffer, MAXLINE);
if (strncmp(buffer, "250", 3))
{
printf("The smtp server didn't accept sender name(%s) \n", send_from);
goto error;
}
#ifdef MAIL_DEBUG
printf("01......after send MAIL FROM:%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "RCPT TO: <%s>\r\n", send_to);
write(sockfd, buffer, strlen(buffer));
getresponse(sockfd, buffer, MAXLINE);
if (strncmp(buffer, "250", 3))
{
printf("The smtp server didn't accept the send_to(%s) ret=%s\n", send_to, buffer);
goto error;
}
#ifdef MAIL_DEBUG
printf("02......after send RCPT TO:%s\n", buffer);
#endif
write(sockfd, "DATA\r\n", 6);
getresponse(sockfd, buffer, MAXLINE);
if (strncmp(buffer, "354", 3))
{
printf("send \"DATA\" command error:%s\n", buffer);
goto error;
}
#ifdef MAIL_DEBUG
printf("03......after send DATA:%s\n", buffer);
#endif
//next we will write the header
//To: example@hotmail.com
//From: in4s@in4s.com
//Subject: e-Home Security
//MIME-Version: 1.0
//Content-Type: multipart/mixed; boundary="PtErOdAcTyL2285248"
//#define MIME "MIME-Version: 1.0"
//#define CONTENT_TYPE "Content-Type: multipart/mixed"
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "From: %s\r\n", send_from);
if (write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("04......after send from:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "To: %s\r\n", send_to);
if (write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("05......after send To:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "%s" "\r\n", MIME);
if (write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("06......after send MIME:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "Subject: %s\r\n", MAIL_SUBJECT);
if (write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("07......after send SUBJECT:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "%s\r\n", CONTENT_TYPE);
if (write(sockfd, buffer, strlen(buffer)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("08......after send CONTENT:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\r\n%s\r\n", "--" BOUNDARY);
write(sockfd, buffer, strlen(buffer));
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("09......after send BOUNDARY:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "%s", FIRST_BODY);
write(sockfd, buffer, strlen(buffer));
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("10......after send first body:::%s\n", buffer);
#endif
if (write(sockfd, body, strlen(body)) < 0)
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("11......after send BODY:::%s\n", buffer);
#endif
if(-1 == write_image_buf(sockfd))
goto error;
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("12......after send image:::%s\n", buffer);
#endif
memset(buffer, 0, sizeof(buffer));
sprintf(buffer, "\r\n%s\r\n", "--" BOUNDARY);
rt = write(sockfd, buffer, strlen(buffer));
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("13......after send BOUNDARY:::%s\n", buffer);
#endif
rt = write(sockfd, "\r\n.\r\n", 5);
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("14......after send <.>:::%s\n", buffer);
#endif
rt = write(sockfd, "quit\r\n", 6);
#ifdef MAIL_DEBUG
memset(buffer, 0, sizeof(buffer));
getresponse(sockfd, buffer, MAXLINE);
printf("15......after send quit:::%s\n", buffer);
#endif
close(sockfd);
return 0;
error:
close(sockfd);
return -1;
} |
|