哪位大哥有电子邮件的源代码,我在网上下载的怎么都用不了,报告什么533 Server err,这是怎么回事?

解决方案 »

  1.   

    CSocket mailsock;
    CString m_MailMsg;
    HWND hwnd;CString OnMailConnect(DWORD m_dwSize)
    {    char* buf;
    CString strbuf;
    DWORD dwRead=0;
    while(dwRead<m_dwSize)
    {
    buf=new char[2048];
    memset(buf,0,2048);
    UINT rec=mailsock.Receive(buf,2048,0);
    dwRead+=rec;
    strbuf+=buf;
    delete buf;
    }
    return strbuf;
    }UINT mailThread(LPVOID pParam)
    {
    CString handMsg;
    CString sendMsg;
    mailsock.Create();
    mailsock.Connect("pop.163.net",110);
    m_MailMsg+=OnMailConnect(1);::SetWindowText(hwnd,m_MailMsg);sendMsg="user chenbin27\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_MailMsg+=sendMsg;
    m_MailMsg+=OnMailConnect(1);::SetWindowText(hwnd,m_MailMsg);sendMsg="pass ***\r\n";//***代表你的密码
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_MailMsg+=sendMsg;
    m_MailMsg+=OnMailConnect(1);::SetWindowText(hwnd,m_MailMsg);sendMsg="stat\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    CString statMsg=OnMailConnect(1);
    m_MailMsg+=sendMsg;
    m_MailMsg+=statMsg;::SetWindowText(hwnd,m_MailMsg);statMsg.Delete(0,4);
    int dashpos=statMsg.Find(" ");
    statMsg.Delete(dashpos,statMsg.GetLength()-dashpos);
    statMsg.Replace("\r\n","");int mailCount=atoi(statMsg);for(int i=1;i<mailCount+1;i++)
    {
    sendMsg.Format("list %d\r\n",i);
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    CString recMsg=OnMailConnect(1);
    m_MailMsg+=sendMsg;
    m_MailMsg+=recMsg;::SetWindowText(hwnd,m_MailMsg);recMsg.Delete(0,4);
    int recCounter=recMsg.Find(" ");
    recMsg.Delete(0,recCounter);
    recMsg.Replace("\r\n","");
    DWORD mailsize=atoi(recMsg);sendMsg.Format("retr %d\r\n",i);
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    CString mailContent=OnMailConnect(mailsize);
    m_MailMsg+=sendMsg;
    m_MailMsg+=recMsg;
    ::SetWindowText(hwnd,m_MailMsg);CString sub=mailContent;
    int subPos=sub.Find("Subject:");
    sub.Delete(0,subPos);
    subPos=sub.Find("\r\n");
    CString strSubject=sub.Left(subPos);strSubject.Replace("Subject:","");
    strSubject.Replace("\\",".");
    strSubject.Replace("/",".");
    strSubject.Replace(":",".");
    strSubject.Replace("*",".");
    strSubject.Replace("?",".");
    strSubject.Replace("\"",".");
    strSubject.Replace("<",".");
    strSubject.Replace(">",".");
    strSubject.Replace("|",".");CString fname;
    fname.Format("c:\\%s(%d).eml",strSubject,i);
    CFile fp;
    fp.Open(fname,CFile::modeCreate|CFile::modeWrite);
    fp.Write(mailContent,mailContent.GetLength());
    fp.Close();sendMsg.Format("dele %d\r\n",i);
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    recMsg=OnMailConnect(1);
    m_MailMsg+=sendMsg;
    m_MailMsg+=recMsg;
    ::SetWindowText(hwnd,m_MailMsg);
    }
    sendMsg="quit\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    CString recMsg=OnMailConnect(1);
    m_MailMsg+=sendMsg;
    m_MailMsg+=recMsg;
    ::SetWindowText(hwnd,m_MailMsg);
    mailsock.Close();
    return 0;
    }void CMailDlg::OnRec() 
    {
    hwnd=GetDlgItem(IDC_EDIT_MAIL)->GetSafeHwnd();
    AfxBeginThread(mailThread,NULL,THREAD_PRIORITY_IDLE);
    }
    CString smtpRec(CString strInstruction)
    {
    CString smtpRe;
    UINT sndCounter=0;
    char* buf;
    buf=new char[256];
    memset(buf,0,256); mailsock.Receive(buf,256,0);
    smtpRe=buf; delete[] buf;
    return smtpRe;

    }UINT threadSend(LPVOID pParam)
    {
    return 0;
    }
      

  2.   


    void CMailDlg::OnSend() 
    {
    CString handMsg;
    CString sendMsg;
    mailsock.Create();
    mailsock.Connect("smtp.21cn.com",25);
    handMsg=smtpRec("220");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="EHLO howtotell\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("250");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="auth login\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("334");
    m_recMsg+=handMsg;
    UpdateData(FALSE);CString user;
    user="cqbai";
    sendMsg.Format("%s\r\n",Base64(user,user.GetLength()));
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("334");
    m_recMsg+=handMsg;
    UpdateData(FALSE);CString psw;
    psw="****";//你的密码
    sendMsg.Format("%s\r\n",Base64(psw,psw.GetLength())); 
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("235");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="MAIL FROM: <[email protected]>\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("250");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="RCPT TO: <[email protected]>\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("250");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="DATA\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    m_recMsg+=sendMsg;
    handMsg=smtpRec("354");
    m_recMsg+=handMsg;
    UpdateData(FALSE);
    CString From,To,DateTime,Subject,BHead,Body;
    CTime tDateTime;From="FROM:chenbin<[email protected]>\r\n";
    To="TO: Howtotell<[email protected]>\r\n";
    tDateTime = tDateTime.GetCurrentTime();
    DateTime = "Date:";
    DateTime += tDateTime.Format("%a, %d %b %Y %H:%M:%S %Z");
    DateTime += "\r\n";
    Subject="Subject:这是主题\r\n";
    BHead += "MIME_Version:1.0\r\n";
    BHead += "Content-type:multipart/mixed;\r\n      boundary=\"@Howtotell _Border@\"\r\n\r\n";Body+="--@Howtotell _Border@\r\n";
    Body+="Content-Type: text/plain;\r\n charset=\"gb2312\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
    Body+="这是文本内容";
    Body+="\r\n\r\n";
    Body+="--@Howtotell _Border@\r\n";
    Body+="Content-Transfer-Encoding: base64\r\nContent-Disposition:attachment;\r\n    filename=\"info.txt\"";Body+="\r\n\r\n";
    int length;
    char* buf;
    CFile fp;
    fp.Open("c:\\slave.exe",CFile::modeRead);
    length=fp.GetLength();
    buf=new char[length];
    fp.Read(buf,length);
    fp.Close();
    CString attachment=Base64(buf,length);
    delete []buf;
    Body+=attachment;Body+="\r\n\r\n--@Howtotell _Border@";
    Body+="\r\n.\r\n";
    sendMsg= From + To + DateTime + Subject + BHead + Body;
    CFile ff;
    ff.Open("c:\\mail.eml",CFile::modeCreate|CFile::modeWrite);
    ff.Write(sendMsg,sendMsg.GetLength());
    ff.Close();
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    handMsg=smtpRec("250");
    m_recMsg+=handMsg;
    UpdateData(FALSE);sendMsg="quit\r\n";
    mailsock.Send(sendMsg,sendMsg.GetLength(),0);
    handMsg=smtpRec("250");
    m_recMsg+=sendMsg;
    m_recMsg+=handMsg;
    UpdateData(FALSE);mailsock.Close();}CString CMailDlg::Base64(LPCTSTR szEncoding, int nSize)
    { //Base64编码字符集:
    CString m_sBase64Alphabet = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
    char *szOutput;
    //计算空间
    int size = (nSize + 2) / 57 * 2;
    size += nSize % 3 != 0 ? (nSize - nSize % 3 + 3) / 3 * 4 : nSize / 3 * 4;
    szOutput = new char[size + 1];
    memset(szOutput, 0, size + 1); LPCTSTR szInput = szEncoding; int nBitsRemaining = 0, nPerRowCount = 0;//换行计数
        register int nBitStorage = 0, nScratch = 0;
    int i, lp, endlCount; for(i=0, lp=0, endlCount = 0; lp < nSize; lp++)
    {
    nBitStorage = (nBitStorage << 8) | (szInput[lp] & 0xff);//1 byte//the lowest-byte to 0 not cycle
    nBitsRemaining += 8;//读了一个字节,加八位 do//nBitStorage"剩下的位"记录变量
    {
    nScratch = nBitStorage >> (nBitsRemaining - 6) & 0x3f;//提出最前的六位
    szOutput[i++] = m_sBase64Alphabet[nScratch];
    nPerRowCount++;
    if(nPerRowCount >= 76)
    {
    szOutput[i++] = '\r';
    szOutput[i++] = '\n';
    endlCount += 2;
    nPerRowCount = 0;
    }
    nBitsRemaining -= 6;
    }while(nBitsRemaining >= 6);
    } if(nBitsRemaining != 0)
    {
    //原数据最后多一个两个字节时进入,编码未结束nBitsRemaining!=0
    nScratch = nBitStorage << (6-nBitsRemaining);//空位补0
    nScratch &= 0x3f;
    szOutput[i++] = m_sBase64Alphabet[nScratch];
    nPerRowCount++;
    if(nPerRowCount >= 76)
    {
    szOutput[i++] = '\r';
    szOutput[i++] = '\n';
    endlCount += 2;
    nPerRowCount = 0;
    }
    }  // Pad with '=' as per RFC 1521
    while((i - endlCount) % 4 != 0)
    {
    szOutput[i++] = '=';
    nPerRowCount++;
    if(nPerRowCount >= 76)
    {
    szOutput[i++] = '\r';
    szOutput[i++] = '\n';
    endlCount += 2;
    nPerRowCount = 0;
    }
    }

    CString strResult = szOutput;
    delete[] szOutput;
    return strResult;
    }void CMailDlg::OnButton1() 
    {
    CString str="command.com";
           GetDlgItem(IDC_EDIT_S)->SetWindowText(Base64(str,str.GetLength())); return;
    char buff[256];
    CAsyncSocket soc;
    soc.Create();
    soc.AsyncSelect(FD_CONNECT);
    soc.Connect("pop.21cn.com",25);
    int counter=0;
    while(TRUE)
    {
    soc.Receive(buff,256,0);
    CString str;
    str=buff; if(str.Left(3).CompareNoCase("220")==0)
    break; Sleep(500);
    counter++;
    if(counter>10)
    break;
    }
    AfxMessageBox(buff);
    soc.Send("quit\r\n",16,0);
    soc.Close(); return; CString sendMsg;CString From,To,DateTime,Subject,BHead,Body;
    CTime tDateTime;From="FROM:chenbin<[email protected]>\r\n";
    To="TO: Howtotell<[email protected]>\r\n";
    tDateTime = tDateTime.GetCurrentTime();
    DateTime = "Date:";
    DateTime += tDateTime.Format("%a, %d %b %Y %H:%M:%S %Z");
    DateTime += "\r\n";
    Subject="Subject:这是主题\r\n";
    BHead += "MIME_Version:1.0\r\n";
    BHead += "Content-type:multipart/mixed;\r\n      boundary=\"@Howtotell _Border@\"\r\n\r\n";Body+="--@Howtotell _Border@\r\n";
    Body+="Content-Type: text/plain;\r\n charset=\"gb2312\"\r\nContent-Transfer-Encoding: 7bit\r\n\r\n";
    Body+="这是文本内容";
    Body+="\r\n\r\n";
    Body+="--@Howtotell _Border@\r\n";
    Body+="Content-Transfer-Encoding: base64\r\nContent-Disposition:attachment;\r\n    filename=\"通讯录.txt\"";Body+="\r\n\r\n";
    int length;
    char* buf;
    CFile fp;
    fp.Open("c:\\33.txt",CFile::modeRead);
    length=fp.GetLength();
    buf=new char[length];
    fp.Read(buf,length);
    fp.Close();
    CString attachment=Base64(buf,length);
    delete []buf;
    Body+=attachment;Body+="\r\n\r\n--@Howtotell _Border@";
    Body+="\r\n.\r\n";
    sendMsg= From + To + DateTime + Subject + BHead + Body;

    fp.Open("c:\\test.eml",CFile::modeCreate|CFile::modeWrite);
    fp.Write(sendMsg,sendMsg.GetLength());
    fp.Close();
    }