Пример #1
0
/* subject and body could be null. */
int mailText(unsigned char **mail, const unsigned char *fromMail, const unsigned char *toMail, 
			 const unsigned char *mailSubject, const unsigned char *mailBody)
{
	char *szMailText = NULL;
	int mailTextLen = 0;
	char fromName[MAX_EMAIL_LEN] = {0};
	char toName[MAX_EMAIL_LEN] = {0};

	if ((NULL == *mail) || (NULL == fromMail) || (NULL == toMail))
	{
		//ACE_DEBUG((LM_ERROR, "[%s][%d] \r\n", __FILE__, __LINE__));
		return -1;
	}

	mailTextLen = ACE_OS::strlen((char* )fromMail) + 
		ACE_OS::strlen((char* )toMail) + 
		ACE_OS::strlen((char* )mailSubject) + 
		ACE_OS::strlen((char* )mailBody) + 500;

	szMailText = (char* )ACE_OS::calloc(mailTextLen, 1);
	if (NULL == szMailText)
	{
		//ACE_DEBUG((LM_ERROR, "[mailText]szMailText malloc fail!\n"));
		return -1;
	}

	ACE_OS::memset(&fromName, 0, MAX_EMAIL_LEN);
	stringCut(fromMail, NULL, "@", fromName);

	ACE_OS::memset(&toName, 0, MAX_EMAIL_LEN);
	stringCut(toMail, NULL, "@", toName);

	ACE_OS::snprintf(szMailText, mailTextLen, "From: \"%s\"<%s>\r\nTo: \"%s\"<%s>\r\nSubject: %s\r\nMIME-Version:1.0\r\nContent-Type:multipart/mixed;boundary=\"%s\"\r\n\r\n--%s\r\nContent-Type: text/plain; charset=\"gb2312\"\r\n\r\n%s\r\n\r\n--%s\r\n", fromName, fromMail, toName, toMail, mailSubject, TEXT_BOUNDARY, TEXT_BOUNDARY, mailBody, TEXT_BOUNDARY);

	*mail = (unsigned char* )realloc(*mail, strlen((char* )*mail)+strlen(szMailText)+1);
	if (NULL == *mail)
	{
		//ACE_DEBUG((LM_ERROR, "[mailText]realloc fail.\n"));
		return -1;
	}

	ACE_OS::strcat((char* )*mail, szMailText);

	free(szMailText);

	/* If I am in danger, let me konw, please */
	return (mailTextLen - strlen((char* )*mail));
}
Пример #2
0
/* subject and body could be null. */
int mailText(unsigned char **mail, const unsigned char *fromMail, const unsigned char *toMail, 
                 const unsigned char *mailSubject, const unsigned char *mailBody)
{
	char *mailText = NULL;
	int mailTextLen = 0;
	char fromName[MAX_EMAIL_LEN] = {0};
	char toName[MAX_EMAIL_LEN] = {0};

	if ((NULL == *mail) || (NULL == fromMail) || (NULL == toMail))
	{
		printf("[%s][%d] \r\n", __FILE__, __LINE__);
		return -1;
	}

	mailTextLen = strlen(fromMail)+strlen(toMail)+strlen(mailSubject)+strlen(mailBody)+500;

	mailText = calloc(mailTextLen, 1);
	if (NULL == mailText)
	{
		perror("malloc...\n");
		return -1;
	}

	memset(&fromName, 0, MAX_EMAIL_LEN);
	stringCut(fromMail, NULL, "@", fromName);

	memset(&toName, 0, MAX_EMAIL_LEN);
	stringCut(toMail, NULL, "@", toName);
	
	snprintf(mailText, mailTextLen, "From: \"%s\"<%s>\r\nTo: \"%s\"<%s>\r\nSubject: %s\r\nMIME-Version:1.0\r\nContent-Type:multipart/mixed;boundary=\"%s\"\r\n\r\n--%s\r\nContent-Type: text/plain; charset=\"gb2312\"\r\n\r\n%s\r\n\r\n--%s\r\n", fromName, fromMail, toName, toMail, mailSubject, TEXT_BOUNDARY, TEXT_BOUNDARY, mailBody, TEXT_BOUNDARY);

	*mail = realloc(*mail, strlen(*mail)+strlen(mailText)+1);
	if (NULL == *mail)
	{
		perror("realloc...\n");
		/* what should I do? */
		return -1;
	}
	
	strcat(*mail, mailText);

	free(mailText);

	/* If I am in danger, let me konw, please */
	return (mailTextLen - strlen(*mail));
}
Пример #3
0
/* static attachmemt size */
int mailAttachment(unsigned char **mail, const unsigned char *filePath)
{
	FILE *fp = NULL;
	int fileSize, base64Size, headerSize, len;
	char *attach = NULL, *base64Attach = NULL, *attachHeader = NULL;
    char fileName[MAX_EMAIL_LEN] = {0};
    const char *contentType = "Content-Type: application/octet-stream";
    const char *contentEncode = "Content-Transfer-Encoding: base64";
    const char *contentDes = "Content-Disposition: attachment";

	fp = fopen(filePath, "rb");
	if (NULL == fp)
	{
		perror("open...");
		return -1;
	}

	fseek(fp, 0, SEEK_END);
	fileSize = ftell(fp);
	if (0 > fileSize)
	{
		perror("ftell...\n");
		return -1;
	}
	
	rewind(fp);

	attach = calloc(fileSize, 1);
	if (NULL == attach)
	{
		perror("malloc...");
		return -1;
	}
    
    headerSize = strlen(contentType)+strlen(contentEncode)+strlen(contentDes)+200;
    attachHeader = calloc(headerSize, 1);
    if (NULL == attach)
	{
		perror("malloc...");
		return -1;
	}
    
    /* attachment header */
    stringCut(filePath, "/", NULL, fileName);
    
    sprintf(attachHeader, "%s;name=\"%s\"\r\n%s\r\n%s;filename=\"%s\"\r\n\r\n", contentType, fileName, contentEncode, contentDes, fileName);
    
	base64Size = BASE64_SIZE(fileSize);
	base64Attach = calloc(base64Size, 1);
	if (NULL == base64Attach)
	{
		perror("malloc...");
		return -1;
	}

	len = fread(attach, sizeof(char), fileSize, fp);

	SMTP_Print6("[%s][%d] %s size = %d, base64Size = %d \r\n",__FILE__, __LINE__, filePath, fileSize, base64Size);

    /* attachment transform to base64 */
	base64_encode(base64Attach, base64Size, attach, fileSize);

	free(attach);

	*mail = realloc(*mail, strlen(*mail)+headerSize+base64Size+1);
	if (NULL == *mail)
	{
		perror("realloc...\n");
		/* what should I do? */
		return -1;
	}

    strcat(*mail, attachHeader);
	strcat(*mail, base64Attach);
    
    free(attachHeader);
	free(base64Attach);

	return fileSize;
}
Пример #4
0
int authEmail(const int socketFd, const unsigned char *mailAddr, const unsigned char *mailPasswd)
{
	int outSize = 0, stringLen;
	char readData[SMTP_MTU] = {0};
	char writeData[SMTP_MTU] = {0};
	char userName[MAX_EMAIL_LEN] = {0};
	char userPasswd[MAX_EMAIL_LEN] = {0};

	memset(&readData, 0, SMTP_MTU);
    safeRead(socketFd, readData, SMTP_MTU);

	SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);

	/* Send: EHLO */
	safeWrite(socketFd, "EHLO Here\r\n", strlen("EHLO Here\r\n"));

	/* Recv: EHLO */
	memset(&readData, 0, SMTP_MTU);
    safeRead(socketFd, readData, SMTP_MTU);

    SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
    recvStatus(readData);

    /* Send: AUTH LOGIN */
	safeWrite(socketFd, "AUTH LOGIN\r\n", strlen("AUTH LOGIN\r\n"));

	/* Recv: AUTH LOGIN */
	memset(&readData, 0, SMTP_MTU);
    safeRead(socketFd, readData, SMTP_MTU);

    SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
    recvStatus(readData);

	/* Send: username */	
	memset(&userName, 0, MAX_EMAIL_LEN);
	memset(&writeData, 0, SMTP_MTU);
	stringCut((unsigned char*)mailAddr, NULL, "@", userName);

	outSize = BASE64_SIZE(strlen(userName));
	base64_encode(writeData, outSize, userName, strlen(userName));
	strcat(writeData, "\r\n");
	safeWrite(socketFd, writeData, strlen(writeData));
	
	/* Recv: username */
	memset(&readData, 0, SMTP_MTU);
    safeRead(socketFd, readData, SMTP_MTU);

    SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
    recvStatus(readData);

	/* Send: passwd */	
	memset(&userPasswd, 0, MAX_EMAIL_LEN);
	strcpy(userPasswd, mailPasswd);
	memset(&writeData, 0, SMTP_MTU);
	outSize = BASE64_SIZE(strlen(userPasswd));
	base64_encode(writeData, outSize, userPasswd, strlen(userPasswd));
	strcat(writeData, "\r\n");
    safeWrite(socketFd, writeData, strlen(writeData));

	/* Recv: passwd */
	memset(&readData, 0, SMTP_MTU);
    safeRead(socketFd, readData, SMTP_MTU);

    SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
    recvStatus(readData);

	return 0;
}
Пример #5
0
/* static attachmemt size */
int mailAttachment(unsigned char **mail, const unsigned char *filePath)
{
	FILE *fp = NULL;
	int fileSize, base64Size, headerSize, len;
	char *attach = NULL, *base64Attach = NULL, *attachHeader = NULL;
	char fileName[MAX_EMAIL_LEN] = {0};
	const char *contentType = "Content-Type: application/octet-stream";
	const char *contentEncode = "Content-Transfer-Encoding: base64";
	const char *contentDes = "Content-Disposition: attachment";

	fp = ACE_OS::fopen((char* )filePath, "rb");
	if (NULL == fp)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp fopen fail.\n"));
		return -1;
	}

	ACE_OS::fseek(fp, 0, SEEK_END);
	fileSize = ACE_OS::ftell(fp);
	if (0 > fileSize)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp ftell fail.\n"));
		ACE_OS::fclose(fp);
		return -1;
	}

	ACE_OS::rewind(fp);

	attach = (char* )calloc(fileSize, 1);
	if (NULL == attach)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp calloc fileSize fail.\n"));
		ACE_OS::fclose(fp);
		return -1;
	}

	headerSize = ACE_OS::strlen(contentType)+ACE_OS::strlen(contentEncode)+ACE_OS::strlen(contentDes)+200;
	attachHeader = (char* )calloc(headerSize, 1);
	if (NULL == attach)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp calloc headerSize fail.\n"));
		ACE_OS::fclose(fp);
		return -1;
	}

	/* attachment header */
	stringCut(filePath, "/", NULL, fileName);

	ACE_OS::sprintf(attachHeader, "%s;name=\"%s\"\r\n%s\r\n%s;filename=\"%s\"\r\n\r\n", contentType, fileName, contentEncode, contentDes, fileName);

	base64Size = BASE64_SIZE(fileSize);
	base64Attach = (char* )calloc(base64Size, 1);
	if (NULL == base64Attach)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp calloc base64Size fail.\n"));
		ACE_OS::fclose(fp);
		return -1;
	}

	len = ACE_OS::fread(attach, sizeof(char), fileSize, fp);

	//SMTP_Print6("[%s][%d] %s size = %d, base64Size = %d \r\n",__FILE__, __LINE__, filePath, fileSize, base64Size);

	/* attachment transform to base64 */
	base64_encode(base64Attach, base64Size, (const unsigned char *)attach, fileSize);

	free(attach);

	*mail = (unsigned char* )realloc(*mail, strlen((char* )*mail)+headerSize+base64Size+1);
	if (NULL == *mail)
	{
		//ACE_DEBUG((LM_ERROR, "[mailAttachment]fp realloc base64Size fail.\n"));
		ACE_OS::fclose(fp);
		return -1;
	}

	ACE_OS::strcat((char* )*mail, attachHeader);
	ACE_OS::strcat((char* )*mail, base64Attach);

	free(attachHeader);
	free(base64Attach);

	ACE_OS::fclose(fp);

	return fileSize;
}
Пример #6
0
int authEmail(const ACE_HANDLE socketFd, const unsigned char *mailAddr, const unsigned char *mailPasswd)
{
	int outSize   = 0; 
	char readData[SMTP_MTU] = {0};
	char writeData[SMTP_MTU] = {0};
	char userName[MAX_EMAIL_LEN] = {0};
	char userPasswd[MAX_EMAIL_LEN] = {0};

	ACE_OS::memset(&readData, 0, SMTP_MTU);
	safeRead(socketFd, readData, SMTP_MTU);

	//SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);

	/* Send: EHLO */
	char szRELO[50] = {'\0'};
	ACE_OS::sprintf(szRELO, "EHLO Here\r\n");
	safeWrite(socketFd, szRELO, ACE_OS::strlen("EHLO Here\r\n"));

	/* Recv: EHLO */
	ACE_OS::memset(&readData, 0, SMTP_MTU);
	safeRead(socketFd, readData, SMTP_MTU);

	//SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
	recvStatus(readData);

	/* Send: AUTH LOGIN */
	char szLOGIN[50] = {'\0'};
	ACE_OS::sprintf(szLOGIN, "AUTH LOGIN\r\n");
	safeWrite(socketFd, szLOGIN, ACE_OS::strlen("AUTH LOGIN\r\n"));

	/* Recv: AUTH LOGIN */
	ACE_OS::memset(&readData, 0, SMTP_MTU);
	safeRead(socketFd, readData, SMTP_MTU);

	//SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
	recvStatus(readData);

	/* Send: username */	
	ACE_OS::memset(&userName, 0, MAX_EMAIL_LEN);
	ACE_OS::memset(&writeData, 0, SMTP_MTU);
	stringCut((unsigned char*)mailAddr, NULL, (char* )"@", userName);

	outSize = BASE64_SIZE(strlen(userName));
	base64_encode(writeData, outSize, (const unsigned char *)userName, strlen(userName));
	ACE_OS::strcat(writeData, "\r\n");
	safeWrite(socketFd, writeData, strlen(writeData));

	/* Recv: username */
	ACE_OS::memset(&readData, 0, SMTP_MTU);
	safeRead(socketFd, readData, SMTP_MTU);

	//SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
	recvStatus(readData);

	/* Send: passwd */	
	ACE_OS::memset(&userPasswd, 0, MAX_EMAIL_LEN);
	ACE_OS::strcpy((char* )userPasswd, (char* )mailPasswd);
	ACE_OS::memset(&writeData, 0, SMTP_MTU);
	outSize = BASE64_SIZE(strlen(userPasswd));
	base64_encode(writeData, outSize, (const unsigned char *)userPasswd, strlen(userPasswd));
	ACE_OS::strcat(writeData, "\r\n");
	safeWrite(socketFd, writeData, strlen(writeData));

	/* Recv: passwd */
	ACE_OS::memset(&readData, 0, SMTP_MTU);
	safeRead(socketFd, readData, SMTP_MTU);

	//SMTP_Print6("[%s][%d]recv: %s\r\n", __FILE__, __LINE__, readData);
	recvStatus(readData);

	return 0;
}