MFC发送邮件

MFC发送邮件

void CMailerDlg::OnOK() 
{
UpdateData(TRUE);
//得到服务器地址,不过注意这里最好是IP地址
CString Serv = "HELO " + m_Server + "\r\n";
//发送者的地址
CString From = "MAIL FROM:<" + m_From + ">\r\n";
//收信者地址
CString To = "RCPT TO:<" + m_To + ">\r\n";
//发送邮件的文本内容
CString Text = m_Message + "\r\n\r\n.\r\n";

char *MailMessage[] = 
{
   Serv.GetBuffer(1),
   From.GetBuffer(1),
   To.GetBuffer(1),
   "DATA\r\n",
   Text.GetBuffer(1),
   "QUIT\r\n",
   NULL
};

WSADATA Wsa;

//进行WINSOCK的设置
WSAStartup(0x0101,&Wsa);
SOCKET s = socket(AF_INET,SOCK_STREAM,0);

SOCKADDR_IN sin;
sin.sin_addr.s_addr = inet_addr(m_Server);
sin.sin_family = AF_INET;
//注意邮件服务器的侦听端口
sin.sin_port = htons(25);

if(connect(s,(LPSOCKADDR)&sin,sizeof(sin)) == SOCKET_ERROR)
   MessageBox("Error: can't connect to server :(","Error",MB_OK|MB_ICONERROR);


int iLength = 0;
int iEnd = 0;
char sBuff[255] = "";
int iMsg = 0;

//循环发送内容,直到要发送的内容发送完毕
do
{
   iLength = recv(s,(LPSTR)sBuff+iEnd, sizeof(sBuff)-iEnd,0);
   iEnd += iLength;
   sBuff[iEnd] = '\0';
   send(s,(LPSTR)MailMessage[iMsg],strlen(MailMessage[iMsg]),0);
   iMsg++;
}while(MailMessage[iMsg]);

//关闭连接
closesocket(s);
WSACleanup();
}

你可能感兴趣的:(MFC发送邮件)