C语言发送邮件相关
各位大侠,帮个忙!
最近我在Linux下用C写一个用来发送邮件的程序。
邮件的内容和附件分别从两个文件中读取,都采用base64编码。
附件的处理没有任何问题,但在处理邮件内容时出现问题。
当邮件内容从存文本文件中读取的时候,没有任何问题。
但是,当邮件内容从word文件中读取的时候,经发送后,收到的都是乱码,请高手指点其中原委!
另外,还有一个问题,如果无法确定word文件中是否含有图片等非ascii字符,但又要以此word文件中的内容作为邮件内容发送时,应该如何进行处理?
谢谢!!!
[解决办法]
发邮件只能转化成base64编码,utf8只是对字符编码,而不是对字节和位编码。
[解决办法]
能直接对word文件进行编码,下面是一个jpg图片作为附件的邮件发送程序,只要把mime类型改为msword就可以:
/*
funtion : send email
data : 2007-01-30
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <linux/if_ether.h>
#include <net/if.h>
#include <errno.h>
#define ETH_NAME "eth0"
#define SD_BOTH 2
#define SOCKET_ERROR -1
#define INVALID_SOCKET -1
#define CHAR64(c) (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])
#define B0(a) (a & 0xFF)
#define B1(a) (a >> 8 & 0xFF)
#define B2(a) (a >> 16 & 0xFF)
#define B3(a) (a >> 24 & 0xFF)
int s;
struct sockaddr_in remote;
unsigned short port;
int rt;
char *send_data;
char *recv_data;
char *hostname;
struct hostent *ht;
char *usersrc,*userdes,*passsrc,*passdes;
char *From,*To,*Date,*Subject,*tmpbuf;
char *Filename;
// base64 tables
static char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static signed char index_64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,
-1, -1,-1,-1,-1,
-1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
};
char * base64_encode(const unsigned char *value, int vlen);
unsigned char * base64_decode(const char *value, int *rlen);
int Base64Encode(char * base64code, const char * src, int src_len );
int Base64Decode(char * buf, const char * base64code, int src_len);
void Destory()
{
if(s!=INVALID_SOCKET)
{
shutdown(s,SD_BOTH);
close(s);
}
free(From);
free(To);
free(Date);
free(Subject);
free(Filename);
free(tmpbuf);
free(recv_data);
free(userdes);
free(passdes);
}
// Base64 code table
// 0-63 : A-Z(25) a-z(51), 0-9(61), +(62), /(63)
char Base2Chr( unsigned char n )
{
n &= 0x3F;
if ( n < 26 )
return ( char )( n + 'A' );
else if ( n < 52 )
return ( char )( n - 26 + 'a' );
else if ( n < 62 )
return ( char )( n - 52 + '0' );
else if ( n == 62 )
return '+';
else
return '/';
}
int Base64Encode22( char * const aDest, const unsigned char * aSrc, int aLen )
{
char * p = aDest;
int i;
unsigned char t;
for ( i = 0; i < aLen; i++ )
{
switch ( i % 3 )
{
case 0 :
*p++ = Base2Chr( *aSrc >> 2 );
t = ( *aSrc++ << 4 ) & 0x3F;
break;
case 1 :
*p++ = Base2Chr( t | ( *aSrc >> 4 ) );
t = ( *aSrc++ << 2 ) & 0x3F;
break;
case 2 :
*p++ = Base2Chr( t | ( *aSrc >> 6 ) );
*p++ = Base2Chr( *aSrc++ );
break;
}
}
if ( aLen % 3 != 0 )
{
*p++ = Base2Chr( t );
if ( aLen % 3 == 1 )
*p++ = '=';
*p++ = '=';
}
*p = 0; // aDest is an ASCIIZ string
return ( p - aDest ); // exclude the end of zero
}
int Base64Enc11(char *buf, char*text,int size)
{
int buflen = 0;
while(size>0)
{
*buf++ = basis_64[ (text[0] >> 2 ) & 0x3f];
if(size>2)
{
*buf++ = basis_64[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = basis_64[((text[1] & 0xF) << 2) | (text[2] >> 6)];
*buf++ = basis_64[text[2] & 0x3F];
}
else
{
switch(size)
{
case 1:
*buf++ = basis_64[(text[0] & 3) << 4 ];
*buf++ = '=';
*buf++ = '=';
break;
case 2:
*buf++ = basis_64[((text[0] & 3) << 4) | (text[1] >> 4)];
*buf++ = basis_64[((text[1] & 0x0F) << 2) | (text[2] >> 6)];
*buf++ = '=';
break;
}
}
text +=3;
size -=3;
buflen +=4;
}
*buf = 0;
return buflen;
}
unsigned char *encode(unsigned char *src,int srclen)
{
int n,buflen,i,j;
int pading=0;
unsigned char *buf;
static unsigned char *dst;
buf=src;
buflen=n=srclen;
if(n%3!=0) /* pad with "=" by using a temp buffer */
{
pading=1;
buflen=n+3-n%3;
buf=malloc(buflen+1);
memset(buf,0,buflen+1);
memcpy(buf,src,n);
for(i=0;i<3-n%3;i++)
buf[n+i]="=";
}
dst=malloc(buflen*4/3+1);
memset(dst,0,buflen*4/3+1);
for(i=0,j=0;i<buflen;i+=3,j+=4)
{
dst[j]=(buf[i]&0xFC)>>2;
dst[j+1]=((buf[i]&0x03)<<4) + ((buf[i+1]&0xF0)>>4);
dst[j+2]=((buf[i+1]&0x0F)<<2) + ((buf[i+2]&0xC0)>>6);
dst[j+3]=buf[i+2]&0x3F;
}
for(i=0;i<buflen*4/3;i++) /* map 6 bit value to base64 ASCII character */
dst[i]=basis_64[dst[i]];
if(pading)
free(buf);
return dst;
}
char *base64_encode(const unsigned char *value, int vlen) {
unsigned char oval = 0 ;
char *result = (char *)malloc((vlen * 4) / 3 + 5) ;
char *out = result;
while (vlen >= 3) {
*out++ = basis_64[value[0] >> 2];
*out++ = basis_64[((value[0] << 4) & 0x30) | (value[1] >> 4)];
*out++ = basis_64[((value[1] << 2) & 0x3C) | (value[2] >> 6)];
*out++ = basis_64[value[2] & 0x3F];
value += 3;
vlen -= 3;
}
if (vlen > 0) {
*out++ = basis_64[value[0] >> 2];
oval = (value[0] << 4) & 0x30 ;
if (vlen > 1) oval |= value[1] >> 4;
*out++ = basis_64[oval];
*out++ = (vlen < 2) ? '=' : basis_64[(value[1] << 2) & 0x3C];
*out++ = '=';
}
*out = '\0';
return result;
}