📄 emsmtpc.c
字号:
/***************************************************************************
* emsmtpc.c - Embedded SMTP Client
*
* Simply, it implements a function that send out a text/plain mail.
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <string.h>
#include <stdio.h>
#include "emsmtpc.h"
char *hello_msg={"HELO EPBX_JF\n"};
/*char *mailServer = {"smtp.163.split.netease.com"};*/
char *mailServer = {"host"};
/***************************************************************************
* strScan()
*
* Extract the return code and check it against the passed reference
* code (str).
*
* Returns 0 on success, -1 on error.
*/
int strScan(char *line, char *str, int len)
{
int i;
for (i=0; i<strlen(line)-len; i++)
{
if (line[i] == str[0])
{
if (!strncmp(line, str, 3))
return(0);
}
}
}
/***************************************************************************
* dialog()
*
* Perform a dialog with the connected mail Server. If the command
* is present(non-NULL), send it through the socket to the server.
* If the response is present, check it against the return code from
* the mail Server.
*
* Returns 0 on success, -1 on error.
*/
int dialog(int sd, char *command, char *resp)
{
int ret, len;
char line[128];
if (command != NULL)
{
len = strlen(command);
if (write(sd, command, len) != len)
return -1;
}
if (resp != NULL)
{
ret = read(sd, line, sizeof(line)-1);
line[ret] = 0;
if (strScan(line, resp, 3) == -1)
return -1;
}
return 0;
}
/***************************************************************************
* sendMail()
*
* Given a passed mailHeader structure, send the email to the mail
* Server defined by the mailServer variable.
*
* Returns 0 on success, -1 on error.
*/
int sendMail(struct mailHeader *mail)
{
int connfd, result, ret, goodMsg = 0;
struct sockaddr_in servaddr;
char mailRcpt[129];
char line[256];
connfd = socket(AF_INET, SOCK_STREAM, 0);
bzero((void *)&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(25);
servaddr.sin_addr.s_addr = inet_addr(mailServer);
/* If the prior inet_addr results in a '-1' (or error), then
* we assume that the gateway symbolic is not a dotted-notation
* IP address. It must therefore be fully-qualified domain
* name and we use hostGetByName to resolve it.
*/
if (servaddr.sin_addr.s_addr == 0xffffffff)
{
struct in_addr addrs;
addrs.s_addr = hostGetByName(mailServer);
memcpy(&servaddr.sin_addr, &addrs, sizeof(struct in_addr));
/* servaddr.sin_addr = addrs; */ /* This is OK! */
printf ("%x\n",servaddr.sin_addr.s_addr);
}
result = connect(connfd, (struct sockaddr *)&servaddr, sizeof(servaddr));
if (result == OK)
printf("Connected!\n");
do
{
/*Look for initial salutation*/
if (dialog(connfd, NULL, "220")) break;
/* Send HELO and await response */
if (dialog(connfd, hello_msg, "250")) break;
/* Send MAIL FROM and await response */
/* \r\n means <CR><LF>*/
sprintf(line, "MAIL FROM:<%s>\r\n", mail->sender);
if (dialog(connfd, line, "250")) break;
/* Send RCPT TO and await response */
sprintf(line, "RCPT TO:<%s>\r\n", mail->recipient);
if (dialog(connfd, line, "250")) break;
/* Send DATA and await response */
if (dialog(connfd, "Data\r\n", "354")) break;
/* Send out the header first */
sprintf(line, "From:<%s>\r\n", mail->sender);
if (dialog(connfd, line, NULL)) break;
sprintf(line, "To:<%s>\r\n", mail->recipient);
if (dialog(connfd, line, NULL)) break;
sprintf(line, "Subject:<%s>\r\n", mail->subject);
if (dialog(connfd, line, NULL)) break;
if (mail->contentType[0] != 0)
{
if (dialog(connfd, mail->specialHeaders, NULL)) break;
}
if (mail->contentType[0] != 0)
{
sprintf(line, "Content-Type: %s\r\n", mail->contentType);
if (dialog(connfd, line, NULL)) break;
}
/* <09>charset="gb2312"<CR><LF> */
sprintf(line, "%ccharset=\"gb2312\"\r\n", 9);
if (dialog(connfd, line, NULL)) break;
if (dialog(connfd, "Content-Transfer-Encoding: 7bit\r\n\r\n", NULL))
break; /* Double "\r\n" is used to seperate content from header */
if (dialog(connfd, mail->contents, NULL)) break;
/* Send mail-end and await response */
if (dialog(connfd, "\r\n.\r\n", "250")) break;
if (dialog(connfd, "QUIT\r\n", "221")) break;
goodMsg =1;
}
while (0);
close(connfd);
return(goodMsg);
}
void textMail(void)
{
struct mailHeader mail;
char buffer[512];
/*hostAdd("smtp.163.split.netease.com","202.108.44.206");*/
/*hostAdd("mail.163.com","202.108.45.151");*/
bzero((void *)&mail, sizeof(mail));
bzero((void *)buffer, sizeof(buffer));
mail.contents = buffer;
strcpy(mail.subject, "This is a text message");
strcpy(mail.sender, "test1@test.com");
strcpy(mail.recipient, "test@test.com");
strcpy(mail.contentType, "text/plain;");
strcpy(mail.specialHeaders, "X-header: JFong's EPBX\r\n");
strcpy(mail.contents, "Here is a plain\r\ntext message\r\n");
/*printf("\n%d\n",hostGetByName(mailServer));*/
/*printf("%c",'\101');*//* print 'A'*/
if (sendMail(&mail) == 1)
printf("Text send!\n");
}
void htmlMail(void)
{
struct mailHeader mail;
char buffer[512];
bzero((void *)&mail, sizeof(mail));
bzero((void *)buffer, sizeof(buffer));
mail.contents = buffer;
strcpy(mail.subject, "This is an HTML message");
strcpy(mail.sender, "test1@test.com");
strcpy(mail.recipient, "test@test.com");
strcpy(mail.contentType, "text/html");
strcpy(mail.specialHeaders, "X-header: JFong's EPBX\n");
strcpy(mail.contents,
"<HTML>"
"<BODY><H1><center>Example HTML Mail!</center></H1>"
"<BR><H2>This is sample text.</H2>"
"</BODY></HTML>"
"\r\n");
if (sendMail(&mail) == 1)
printf("Html send!\n");
}
void htmlTabMail(void)
{
struct mailHeader mail;
char buffer[512];
bzero((void *)&mail, sizeof(mail));
bzero((void *)buffer, sizeof(buffer));
mail.contents = buffer;
strcpy(mail.subject, "This is an HTML table message");
strcpy(mail.sender, "test1@test.com");
strcpy(mail.recipient, "test@test.com");
strcpy(mail.contentType, "text/html");
strcpy(mail.specialHeaders, "X-header: JFong's EPBX\n");
strcpy(mail.contents,
"<HTML><BODY><H2>Remote Device Data:</H2><BR>"
"<CENTER>"
"<TABLE BORDER><CAPTION>Sensor Data</CAPTION>");
{
int sensor1 = 52,
sensor2 = 2,
sensor3 = 17;
sprintf(&mail.contents[strlen(mail.contents)],
"<TR><TH>Sensor</TH><TH>Value</TH></TR>"
"<TR><TD>Sensor 1</TD><TD><FONTcolor=RED>%d</FONT>"
"</TD></TR>"
"<TR><TD>Sensor 2</TD><TD>%d</TD></TR>"
"<TR><TD>Sensor 3</TD><TD>%d</TD></TR>"
"</TABLE></CENTER>"
"</BODY></HTML>",
sensor1, sensor2, sensor3);
}
if (sendMail(&mail) == 1)
printf("Html Table send!\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -