1、下载JMail4.3安装
2、添加对话框应用程序:
一、发信模块:
2、点击写信按钮:
void CJMailDlg::OnSend()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_NAME)->EnableWindow(TRUE);
GetDlgItem(IDC_SENDAD)->EnableWindow(TRUE);
GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE);
GetDlgItem(IDC_SMTP)->EnableWindow(TRUE);
GetDlgItem(IDC_POP)->EnableWindow(FALSE);
GetDlgItem(IDC_REVAD)->EnableWindow(TRUE);
GetDlgItem(IDC_SUBJECT)->EnableWindow(TRUE);
GetDlgItem(IDC_BODY)->EnableWindow(TRUE);
GetDlgItem(IDC_ADD)->EnableWindow(TRUE);
GetDlgItem(IDC_LIST)->EnableWindow(FALSE);
//清空所有控件内容
m_list.DeleteAllItems();//清空附件列表
m_body ="";
m_name ="";
m_password ="";
m_pop3 ="";
m_revad ="";
m_sendad ="";
m_smtp = "";
m_subject ="";
UpdateData(false);
flag=true;//记录是发信状态
}
3、添加附件:
void CJMailDlg::OnAdd()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDC_LIST)->EnableWindow(TRUE);
CFileDialog dlg(true,NULL,NULL,OFN_ALLOWMULTISELECT);
if(dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
while(pos != NULL)
{
long i=m_list.GetItemCount();
m_list.InsertItem(m_list.GetItemCount()+1,dlg.GetNextPathName(pos));//AddString(dlg.GetNextPathName(pos));
CString str;
str=m_list.GetItemText(i,0);
}
}
}
4、删除附件:
void CJMailDlg::OnDblclkList(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
if(flag==true)
if(IDOK==MessageBox("确定删除此项吗?","删除提示",MB_OKCANCEL|MB_ICONQUESTION))
{
int nItem=m_list.GetNextItem (-1,LVNI_SELECTED);
m_list.DeleteItem(nItem);
UpdateData(FALSE);
}
*pResult = 0;
}
5、确定发信操作:
void CJMailDlg::OnOK()
{
// TODO: Add extra validation here
if(flag==true)
{
//参数的获取与判断
UpdateData(true);
m_sendad.TrimRight();//去掉后面多于空格
m_password.TrimRight();
m_smtp.TrimRight();
m_revad.TrimRight();
m_subject.TrimRight();
m_body.TrimRight();
m_name.TrimRight();
//判断参数是否合法
if(m_revad.GetLength()<3)
{
AfxMessageBox("请填写正确的收件人邮箱");
return;
}
if(m_sendad.GetLength()<3)
{
AfxMessageBox("请填写正确的发信人邮箱");
return;
}
if(m_body=="")
{
AfxMessageBox("请填写正文");
return;
}
if(m_subject=="")
{
if(IDCANCEL==MessageBox("确定不填写标题吗?","提示",MB_OKCANCEL|MB_ICONQUESTION))
return;
}
if(m_name=="")
{
if(IDCANCEL==MessageBox("确定不填写昵称吗?","提示",MB_OKCANCEL|MB_ICONQUESTION))
return;
}
//进行发信
try
{
CoInitialize(NULL); // COM的初始化
jmail::IMessagePtr pMessage("JMail.Message");
pMessage->From = (LPCTSTR)m_sendad;// 发件人邮箱
pMessage->FromName =_bstr_t(m_name);// 发件人姓名
pMessage->AddRecipient((LPCTSTR)m_revad, "", "");// 添加收件人
pMessage->Priority = 3;// 优先级设置,1-5逐次降低, 3为中级
pMessage->Charset = "GB2312";// 编码方式设置, 默认是iso-8859-1
pMessage->Subject = (LPCTSTR)m_subject;// 主题
pMessage->Body = (LPCTSTR)m_body;// 正文
pMessage->MailServerUserName=(LPCTSTR)m_sendad;// 开始发送
pMessage->MailServerPassWord=(LPCTSTR)m_password;
if(m_list.GetItemCount()!=0)
for(int i=0;i<=m_list.GetItemCount()-1;i++)
{
CString str;
str=m_list.GetItemText(i,0);
pMessage->AddAttachment(_bstr_t(str), VARIANT_FALSE, "application/octet-stream");
char *p=_bstr_t(str);
}
pMessage->Send((LPCTSTR)m_smtp, VARIANT_FALSE);
pMessage.Release();
AfxMessageBox("发送成功!");
CoUninitialize();
}
catch (_com_error e)
{
CString strErr;
strErr.Format("程序运行错误\r\n错误描述:%s",(LPCTSTR)e.Description());
AfxMessageBox(strErr);
}
}
//进行收信工作
else
{
//参数的获取与判断
UpdateData(true);
m_password.TrimRight();
m_sendad.TrimRight();
m_pop3.TrimRight();
//判断参数是否合法
if(m_sendad.GetLength()<3)
{
AfxMessageBox("请填写正确的收件人邮箱");
return;
}
if(m_password=="")
{
AfxMessageBox("请填写密码");
return;
}
if(m_pop3=="")
{
AfxMessageBox("请填写POP服务器");
return;
}
//开始信件获取
CRec dlg;
dlg.DoModal();
}
//CDialog::OnOK();
}
二、收信模块:
1、点击收信按钮:
void CJMailDlg::OnRev()
{
// TODO: Add your control notification handler code here
GetDlgItem(IDOK)->EnableWindow(TRUE);
GetDlgItem(IDC_NAME)->EnableWindow(FALSE);
GetDlgItem(IDC_SENDAD)->EnableWindow(TRUE);
GetDlgItem(IDC_PASSWORD)->EnableWindow(TRUE);
GetDlgItem(IDC_SMTP)->EnableWindow(FALSE);
GetDlgItem(IDC_POP)->EnableWindow(TRUE);
GetDlgItem(IDC_REVAD)->EnableWindow(FALSE);
GetDlgItem(IDC_SUBJECT)->EnableWindow(FALSE);
GetDlgItem(IDC_BODY)->EnableWindow(FALSE);
GetDlgItem(IDC_ADD)->EnableWindow(FALSE);
GetDlgItem(IDC_LIST)->EnableWindow(FALSE);
//清空所有控件内容
m_list.DeleteAllItems();//清空附件列表
m_body ="";
m_name ="";
m_password ="";
m_pop3 ="";
m_revad ="";
m_sendad ="";
m_smtp = "";
m_subject ="";
UpdateData(false);
flag=false;//记录是收信状态
}
2、添加新的对话框,及变量CString m_body、CListCtrl m_llis、CListCtrl m_llist,和类CRecDlg;
CString m_pop;
CString m_add;
CString m_password;
jmail::IMessagesPtr pMessages;
jmail::IMessagePtr pMessage;
jmail::IAttachmentsPtr pAttachments;
jmail::IAttachmentPtr pAttachment;
CStringArray m_bodyArray;
初始化:
BOOL CRec::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
m_letter.InsertColumn(0, "发件人");
m_letter.InsertColumn(1, "主题");
m_letter.InsertColumn(2, "日期");
//获取列表的宽度
RECT rect;
m_letter.GetWindowRect(&rect);
int wid=rect.right-rect.left;
//为你的每列分宽度
m_letter.SetColumnWidth(0,wid/3);
m_letter.SetColumnWidth(1,wid/3);
m_letter.SetColumnWidth(2,wid/3);
m_att.InsertColumn(0, "附件名");
m_att.InsertColumn(1, "大小");
//获取列表的宽度
m_att.GetWindowRect(&rect);
wid=rect.right-rect.left;
//为你的每列分宽度
m_att.SetColumnWidth(0,wid/2);
m_att.SetColumnWidth(1,wid/2);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
3、单击OK按钮,提交信息:
void CJMailDlg::OnOK()
{
// TODO: Add extra validation here
if(flag==true)
{
//参数的获取与判断
UpdateData(true);
m_sendad.TrimRight();//去掉后面多于空格
m_password.TrimRight();
m_smtp.TrimRight();
m_revad.TrimRight();
m_subject.TrimRight();
m_body.TrimRight();
m_name.TrimRight();
//判断参数是否合法
if(m_revad.GetLength()<3)
{
AfxMessageBox("请填写正确的收件人邮箱");
return;
}
if(m_sendad.GetLength()<3)
{
AfxMessageBox("请填写正确的发信人邮箱");
return;
}
if(m_body=="")
{
AfxMessageBox("请填写正文");
return;
}
if(m_subject=="")
{
if(IDCANCEL==MessageBox("确定不填写标题吗?","提示",MB_OKCANCEL|MB_ICONQUESTION))
return;
}
if(m_name=="")
{
if(IDCANCEL==MessageBox("确定不填写昵称吗?","提示",MB_OKCANCEL|MB_ICONQUESTION))
return;
}
//进行发信
try
{
CoInitialize(NULL); // COM的初始化
jmail::IMessagePtr pMessage("JMail.Message");
pMessage->From = (LPCTSTR)m_sendad;// 发件人邮箱
pMessage->FromName =_bstr_t(m_name);// 发件人姓名
pMessage->AddRecipient((LPCTSTR)m_revad, "", "");// 添加收件人
pMessage->Priority = 3;// 优先级设置,1-5逐次降低, 3为中级
pMessage->Charset = "GB2312";// 编码方式设置, 默认是iso-8859-1
pMessage->Subject = (LPCTSTR)m_subject;// 主题
pMessage->Body = (LPCTSTR)m_body;// 正文
pMessage->MailServerUserName=(LPCTSTR)m_sendad;// 开始发送
pMessage->MailServerPassWord=(LPCTSTR)m_password;
if(m_list.GetItemCount()!=0)
for(int i=0;i<=m_list.GetItemCount()-1;i++)
{
CString str;
str=m_list.GetItemText(i,0);
pMessage->AddAttachment(_bstr_t(str), VARIANT_FALSE, "application/octet-stream");
char *p=_bstr_t(str);
}
pMessage->Send((LPCTSTR)m_smtp, VARIANT_FALSE);
pMessage.Release();
AfxMessageBox("发送成功!");
CoUninitialize();
}
catch (_com_error e)
{
CString strErr;
strErr.Format("程序运行错误\r\n错误描述:%s",(LPCTSTR)e.Description());
AfxMessageBox(strErr);
}
}
//进行收信工作
else
{
//参数的获取与判断
UpdateData(true);
m_password.TrimRight();
m_sendad.TrimRight();
m_pop3.TrimRight();
//判断参数是否合法
if(m_sendad.GetLength()<3)
{
AfxMessageBox("请填写正确的收件人邮箱");
return;
}
if(m_password=="")
{
AfxMessageBox("请填写密码");
return;
}
if(m_pop3=="")
{
AfxMessageBox("请填写POP服务器");
return;
}
//开始信件获取
CRec dlg;
dlg.DoModal();
}
//CDialog::OnOK();
}
5、开始接收邮件:
void CRec::OnStart()
{
// TODO: Add your control notification handler code here
CJMailApp*pApp=(CJMailApp*)AfxGetApp();
CJMailDlg*pDlg=(CJMailDlg*)pApp->m_pMainWnd;
m_add=pDlg->m_sendad;
m_password=pDlg->m_password;
m_pop=pDlg->m_pop3;
try
{
CoInitialize(NULL);
jmail::IPOP3Ptr pPOP3("JMail.POP3");
// 超时 60S
//Connect=true;
pPOP3->Timeout =60;
// 连接邮件服务器
pPOP3->Connect((LPCTSTR)m_add,(LPCTSTR)m_password,(LPCTSTR)m_pop,110);
pMessages = pPOP3->Messages;
long lCount = pMessages->Count - 1;
if(lCount == 0)
AfxMessageBox("信箱为空");
else
{
for(long i= 1; i <= lCount; i++)
{
pMessage=pMessages->Item[i];
//pMessage->Encoding= "quoted-printable ";
pMessage->ContentType= "text/html ";
pMessage->ContentTransferEncoding="Quoted-Printable ";
pMessage->Encoding="uuencode";
pMessage->Charset="gb2312";
/* pMessage->Charset = "GB2312"; //邮件的文字编码为国标
pMessage->ContentTransferEncoding = "base64";
pMessage->Encoding = "uuencode";//"base64";
pMessage->ISOEncodeHeaders = false;
pMessage->ContentType = "text/html";*/
_bstr_t bstrFrom = pMessage->From;
_bstr_t bstrSubject = pMessage->Subject;
_bstr_t bstrBody = pMessage->Body;
COleDateTime oleDate = pMessage->Date;
int nListItem = m_letter.InsertItem(i, (const char*)bstrFrom);
m_letter.SetItem(nListItem, 1, LVIF_TEXT, (const char*)bstrSubject, 0, 0, 0, NULL);
m_letter.SetItem(nListItem, 2, LVIF_TEXT, (const char*)oleDate.Format("%Y-%m-%d"), 0, 0, 0, NULL);
m_bodyArray.Add((const char*)bstrBody);
pMessage.Release();
}
}
}
catch(_com_error e)
{
CString strErr;
strErr.Format("程序运行错误\r\n错误描述:%s",(LPCTSTR)e.Description());
AfxMessageBox(strErr);
}
}
6、添加控件:IDC_LETTER的LVN_ITEMCHANGED事件的消息映射,实现打开邮件的功能:
void CRec::OnItemchangedLetter(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
POSITION pos = m_letter.GetFirstSelectedItemPosition();
if(pos)
{
m_att.DeleteAllItems( );
int nItem = m_letter.GetNextSelectedItem(pos);
m_body = m_bodyArray.GetAt(nItem);
jmail::IPOP3Ptr pPOP3("JMail.POP3");
pMessage=pMessages->Item[nItem+1];
for(int i=0;i<=pMessage->Attachments->Count-1;i++)
{
pAttachment=pMessage->Attachments->Item[i];
_bstr_t bstrname=pAttachment->Name;
_bstr_t bstrsize=pAttachment->Size;
int nListItem = m_att.InsertItem(m_att.GetItemCount()+1,(const char*)bstrname);
m_att.SetItem(nListItem, 1, LVIF_TEXT, (const char*)bstrsize, 0, 0, 0, NULL);
}
UpdateData(FALSE);
pMessage.Release();
}
*pResult = 0;
}
7、IDC_ATT的NM_DBLCLK事件的消息映射,实现下载附件的功能:
void CRec::OnDblclkAtt(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
int i=m_letter.GetNextItem (-1, LVNI_SELECTED);
int nItem =m_att.GetNextItem (-1, LVNI_SELECTED);
jmail::IPOP3Ptr pPOP3("JMail.POP3");
CString str;
str=m_att.GetItemText(nItem,0);
CFileDialog dlg(false,NULL,str,OFN_ALLOWMULTISELECT);
if(dlg.DoModal()==IDOK)
{
POSITION pos=dlg.GetStartPosition();
while(pos!=NULL)
{
pMessage=pMessages->Item[i+1];
pAttachment=pMessage->Attachments->Item[nItem];
pAttachment->SaveToFile(_bstr_t(dlg.GetNextPathName(pos)));
MessageBox("下载完成!",NULL,MB_OK);
pMessage.Release();
}
}
*pResult = 0;
}