如何用socket发邮件阿??? 例子:http://www.vckbase.com/code/network/winsock/SendMail.zip 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 CString CSMTP::GetServerHostName(){ return m_sSMTPServerHostName;}BOOL CSMTP::Connect(){ CString sHello; TCHAR local_host[80]; if(m_bConnected) return TRUE; if(!m_wsSMTPServer.Create()) { m_sError=_T("Unable to creat the socket."); return FALSE; } if(!m_wsSMTPServer.Connect(GetServerHostName(),GetPort())) { m_sError=_T("Unable to connect to server"); m_wsSMTPServer.Close(); return FALSE; } gethostname(local_host,80); sHello.Format("HELLO%s\r\n",local_host); m_wsSMTPServer.Send((LPCTSTR)sHello,sHello.GetLength()); if(!get_response(GENERIC_SUCCESS)) { m_wsSMTPServer.Close(); return FALSE; } m_bConnected=TRUE; return TRUE;}BOOL CSMTP::Disconnect(){ BOOL ret; if(!m_bConnected) return TRUE; CString sQuit=_T("QUIT\r\n"); m_wsSMTPServer.Send((LPCTSTR)sQuit,sQuit.GetLength()); ret=get_response(QUIT_SUCCESS); m_wsSMTPServer.Close(); m_bConnected=FALSE; return ret;}UINT CSMTP::GetPort(){ return m_nPort;}CString CSMTP::GetMailerName(){ return m_sMailerName;}CString CSMTP::GetLastError(){ return m_sError;}BOOL CSMTP::SendMessage(CMailMessage* msg){ ASSERT(msg!=NULL); if(!m_bConnected) { m_sError=_T("Must be connected"); return FALSE; } if(FormatMailMessage(msg)==FALSE) { return FALSE; } if(transmit_message(msg)==FALSE) { return FALSE; } return TRUE;}BOOL CSMTP::FormatMailMessage(CMailMessage* msg){ ASSERT(msg!=NULL); if(prepare_header(msg)==FALSE) { return FALSE; } if(msg->m_sBody.Right(2)!="\r\n") msg->m_sBody+="\r\n"; return TRUE;}void CSMTP::SetServerProperties(LPCTSTR sServerHostName,UINT nPort){ ASSERT(sServerHostName!=NULL); if(sServerHostName==NULL) return; m_sSMTPServerHostName=sServerHostName; m_nPort=nPort;}BOOL CSMTP::prepare_header(CMailMessage* msg){ ASSERT(msg!=NULL); CString sTo; CString sDate; CString& sHeader=msg->m_sHeader; if(msg->GetNumRecipients()<+0) { m_sError="No Recipients"; return FALSE; } sHeader=""; sTo=""; CString sEmail=""; CString sFriendly=""; for(int i=0;i<msg->GetNumRecipients();i++) { msg->GetRecipient(sEmail,sFriendly,i); sTo+=(i>0?",":""); sTo+=sFriendly; sTo+="<"; sTo+=sEmail; sTo+=">"; } msg->m_tDateTime=msg->m_tDateTime.GetCurrentTime(); sDate=msg->m_tDateTime.Format("%a,%d%b%y%H:%M:%S%Z"); sHeader.Format("Date:%s\r\n"\"From:%s\r\n"\"To:%s\r\n"\"Subject:%s\r\n"\"X-Mailer:<%s>\r\n\r\n", (LPCTSTR)sDate,(LPCTSTR)msg->m_sFrom,(LPCTSTR)sTo,(LPCTSTR)msg->m_sSubject, (LPCTSTR)m_sMailerName); return TRUE;}CString CSMTP::prepare_body(CMailMessage* msg){ ASSERT(msg!=NULL); CString sTemp; CString sCooked=""; LPCTSTR szBad="\r\n.\r\n"; LPCTSTR szGood="\r\n..\r\n"; int nPos; int nStart=0; int nBadLength=strlen(szBad); sTemp=msg->m_sBody; if(sTemp.Left(3)==".\r\n") sTemp="."+sTemp; while((nPos=sTemp.Find(szBad))>-1) { sCooked=sTemp.Mid(nStart,nPos); sCooked+=szGood; sTemp=sCooked+sTemp.Right(sTemp.GetLength()-(nPos+nBadLength)); } return sTemp;}BOOL CSMTP::transmit_message(CMailMessage* msg){ CString sFrom; CString sTo; CString sTemp; CString sEmail; ASSERT(msg!=NULL); if(!m_bConnected) { m_sError=_T("Must be connected"); return FALSE; } sFrom.Format("MAIL From:<%s>\r\n",(LPCTSTR)msg->m_sFrom); m_wsSMTPServer.Send((LPCTSTR)sFrom,sFrom.GetLength()); if(!get_response(GENERIC_SUCCESS)) return FALSE; for(int i=0;i<msg->GetNumRecipients();i++) { msg->GetRecipient(sEmail,sTemp,i); sTo.Format("RCPT TO:<%s>\r\n",(LPCTSTR)sEmail); m_wsSMTPServer.Send((LPCTSTR)sTo,sTo.GetLength()); get_response(GENERIC_SUCCESS); } sTemp="DATA\r\n"; m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength()); if(!get_response(DATA_SUCCESS)) { return FALSE; } m_wsSMTPServer.Send((LPCTSTR)msg->m_sHeader,msg->m_sHeader.GetLength()); sTemp=prepare_body(msg); m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength()); sTemp="\r\n.\r\n"; m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength()); if(!get_response(GENERIC_SUCCESS)) { return FALSE; } return TRUE;}BOOL CSMTP::get_response(UINT response_expected){ ASSERT(response_expected>=GENERIC_SUCCESS); ASSERT(response_expected<LAST_RESPONSE); CString sResponse; UINT response; response_code* pResp;//shortthand if(m_wsSMTPServer.Receive(response_buf,RESPONSE_BUFFER_SIZE)==SOCKET_ERROR) { m_sError=_T("Socket Error"); return FALSE; } sResponse=response_buf; sscanf((LPCTSTR)sResponse.Left(3),"%d",&response); pResp=&response_table[response_expected]; if(response!=pResp->nResponse) { m_sError.Format("%d:%s",response,(LPCTSTR)pResp->sMessage); return FALSE; } return TRUE;} 可以给例程,若需要将EMail和需要一起发个短信息给我。 TO:ahr(电磁波) 我下载了那个例程,怎么总是报错:WSAETIMEDOUT:Connection timed out谢谢指点 你的smtp服务器地址不对吧?我用它没问题的 你在输入SMTP信息的时候,要注意,比如你用263的,就是smtp.263.net但是这时候发件人的信箱不能是263的,可以用hotmail的等,收件人一定要是263的,你这样输入再试一下。因为各服务器提供的SMTP服务不一样,所以不是随便输入就可以发送成功的。 pscp 文件传输问题 怎样在CtreeCtrl销毁前得到在ITem上保存的数据 请问哪个版本的vc开始提供哪个代码录入的气泡提示功能,vc1.5没有 如何判断一个double型的变量超出了自己能记录的范围? 问几个计算机课程的专业英文 请问在win98下向其他程序注入自己的dll时,怎么让我的dll在其他dll都加载完了以 再次提问,在VC里如何获取并行端口的地址????? 100分 自动刷新页面如何实现? 有那位老兄可以帮一下,工具条的问题 怎样才算“已掌握”MFC呢? 请问如何将BSTR转为string? 如何得到位图的纯数据信息,并从下到上写到一个新的BYTE*中?
{
return m_sSMTPServerHostName;
}
BOOL CSMTP::Connect()
{
CString sHello;
TCHAR local_host[80];
if(m_bConnected)
return TRUE;
if(!m_wsSMTPServer.Create())
{
m_sError=_T("Unable to creat the socket.");
return FALSE;
}
if(!m_wsSMTPServer.Connect(GetServerHostName(),GetPort()))
{
m_sError=_T("Unable to connect to server");
m_wsSMTPServer.Close();
return FALSE;
}
gethostname(local_host,80);
sHello.Format("HELLO%s\r\n",local_host);
m_wsSMTPServer.Send((LPCTSTR)sHello,sHello.GetLength());
if(!get_response(GENERIC_SUCCESS))
{
m_wsSMTPServer.Close();
return FALSE;
}
m_bConnected=TRUE;
return TRUE;
}
BOOL CSMTP::Disconnect()
{
BOOL ret;
if(!m_bConnected)
return TRUE;
CString sQuit=_T("QUIT\r\n");
m_wsSMTPServer.Send((LPCTSTR)sQuit,sQuit.GetLength());
ret=get_response(QUIT_SUCCESS);
m_wsSMTPServer.Close();
m_bConnected=FALSE;
return ret;
}
UINT CSMTP::GetPort()
{
return m_nPort;
}
CString CSMTP::GetMailerName()
{
return m_sMailerName;
}
CString CSMTP::GetLastError()
{
return m_sError;
}
BOOL CSMTP::SendMessage(CMailMessage* msg)
{
ASSERT(msg!=NULL);
if(!m_bConnected)
{
m_sError=_T("Must be connected");
return FALSE;
}
if(FormatMailMessage(msg)==FALSE)
{
return FALSE;
}
if(transmit_message(msg)==FALSE)
{
return FALSE;
}
return TRUE;
}
BOOL CSMTP::FormatMailMessage(CMailMessage* msg)
{
ASSERT(msg!=NULL);
if(prepare_header(msg)==FALSE)
{
return FALSE;
}
if(msg->m_sBody.Right(2)!="\r\n")
msg->m_sBody+="\r\n";
return TRUE;
}
void CSMTP::SetServerProperties(LPCTSTR sServerHostName,UINT nPort)
{
ASSERT(sServerHostName!=NULL);
if(sServerHostName==NULL)
return;
m_sSMTPServerHostName=sServerHostName;
m_nPort=nPort;
}
BOOL CSMTP::prepare_header(CMailMessage* msg)
{
ASSERT(msg!=NULL);
CString sTo;
CString sDate;
CString& sHeader=msg->m_sHeader;
if(msg->GetNumRecipients()<+0)
{
m_sError="No Recipients";
return FALSE;
}
sHeader="";
sTo="";
CString sEmail="";
CString sFriendly="";
for(int i=0;i<msg->GetNumRecipients();i++)
{
msg->GetRecipient(sEmail,sFriendly,i);
sTo+=(i>0?",":"");
sTo+=sFriendly;
sTo+="<";
sTo+=sEmail;
sTo+=">";
}
msg->m_tDateTime=msg->m_tDateTime.GetCurrentTime();
sDate=msg->m_tDateTime.Format("%a,%d%b%y%H:%M:%S%Z");
sHeader.Format("Date:%s\r\n"\"From:%s\r\n"\"To:%s\r\n"\"Subject:%s\r\n"\"X-Mailer:<%s>\r\n\r\n",
(LPCTSTR)sDate,(LPCTSTR)msg->m_sFrom,(LPCTSTR)sTo,(LPCTSTR)msg->m_sSubject,
(LPCTSTR)m_sMailerName);
return TRUE;
}
CString CSMTP::prepare_body(CMailMessage* msg)
{
ASSERT(msg!=NULL);
CString sTemp;
CString sCooked="";
LPCTSTR szBad="\r\n.\r\n";
LPCTSTR szGood="\r\n..\r\n";
int nPos;
int nStart=0;
int nBadLength=strlen(szBad);
sTemp=msg->m_sBody;
if(sTemp.Left(3)==".\r\n")
sTemp="."+sTemp;
while((nPos=sTemp.Find(szBad))>-1)
{
sCooked=sTemp.Mid(nStart,nPos);
sCooked+=szGood;
sTemp=sCooked+sTemp.Right(sTemp.GetLength()-(nPos+nBadLength));
}
return sTemp;
}
BOOL CSMTP::transmit_message(CMailMessage* msg)
{
CString sFrom;
CString sTo;
CString sTemp;
CString sEmail;
ASSERT(msg!=NULL);
if(!m_bConnected)
{
m_sError=_T("Must be connected");
return FALSE;
}
sFrom.Format("MAIL From:<%s>\r\n",(LPCTSTR)msg->m_sFrom);
m_wsSMTPServer.Send((LPCTSTR)sFrom,sFrom.GetLength());
if(!get_response(GENERIC_SUCCESS))
return FALSE;
for(int i=0;i<msg->GetNumRecipients();i++)
{
msg->GetRecipient(sEmail,sTemp,i);
sTo.Format("RCPT TO:<%s>\r\n",(LPCTSTR)sEmail);
m_wsSMTPServer.Send((LPCTSTR)sTo,sTo.GetLength());
get_response(GENERIC_SUCCESS);
}
sTemp="DATA\r\n";
m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
if(!get_response(DATA_SUCCESS))
{
return FALSE;
}
m_wsSMTPServer.Send((LPCTSTR)msg->m_sHeader,msg->m_sHeader.GetLength());
sTemp=prepare_body(msg);
m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
sTemp="\r\n.\r\n";
m_wsSMTPServer.Send((LPCTSTR)sTemp,sTemp.GetLength());
if(!get_response(GENERIC_SUCCESS))
{
return FALSE;
}
return TRUE;
}
BOOL CSMTP::get_response(UINT response_expected)
{
ASSERT(response_expected>=GENERIC_SUCCESS);
ASSERT(response_expected<LAST_RESPONSE);
CString sResponse;
UINT response;
response_code* pResp;//shortthand
if(m_wsSMTPServer.Receive(response_buf,RESPONSE_BUFFER_SIZE)==SOCKET_ERROR)
{
m_sError=_T("Socket Error");
return FALSE;
}
sResponse=response_buf;
sscanf((LPCTSTR)sResponse.Left(3),"%d",&response);
pResp=&response_table[response_expected];
if(response!=pResp->nResponse)
{
m_sError.Format("%d:%s",response,(LPCTSTR)pResp->sMessage);
return FALSE;
}
return TRUE;
}
我下载了那个例程,怎么总是报错:
WSAETIMEDOUT:Connection timed out谢谢指点
比如你用263的,
就是smtp.263.net
但是这时候发件人的信箱不能是263的,可以用hotmail的等,
收件人一定要是263的,
你这样输入再试一下。
因为各服务器提供的SMTP服务不一样,所以不是随便输入就可以发送成功的。