vc++ 利用jmail组件收发邮件
A:收郵件步驟:
一:下載jmail.dll文件放到C:\Windows\System32文件夾中
二:以管理員的身份注冊jmail.dll組件,即執行命令:regsvr32 jmail.dll
三:創建控制臺應用程序,源碼為:
??? //將jmail.dll直接拷貝到工程中可用#import "jmail.dll"或者使用絕對路徑,如下
??? #import "E:\\SRC\\jmail.dll"
??? ?
??? void main()
??? {
?? ??? ?CoInitialize(NULL);
?? ??? ?jmail::IMessagePtr pMessage("JMail.Message");
?? ??? ?pMessage->From = "uutkuu@163.com"; //發送郵箱
?? ??? ?pMessage->FromName = "辰南"; //not must
?? ??? ?pMessage->AddRecipient("abc@qq.com","",""); //請輸入你的接收郵箱
?? ??? ?pMessage->Priority = 3;
?? ??? ?pMessage->Charset = "GB2312";
?? ??? ?pMessage->Subject = "happy birthday"; //not must
?? ??? ?pMessage->Body = "終于有一天,辰南從神魔陵園復活而出,\
?? ??? ??? ?悠悠萬載,滄海桑田,當年的紅顏,親人已不復在,\
?? ??? ??? ?為了生存和追尋心中的摯愛,逐漸走上了一條逆天之路。";//not must
?? ??? ?//發送郵箱賬號uutkuu@163.com與uutkuu均可
?? ??? ?pMessage->MailServerUserName = "uutkuu";
?? ??? ?pMessage->MailServerPassWord = "123456";//發送郵箱密碼
?? ??? ?pMessage->Send("smtp.163.com", VARIANT_FALSE);
?? ??? ?pMessage.Release();
?? ??? ?CoUninitialize();
?? ??? ?system("pause");
??? }
注意:若發送郵箱為qq郵箱,需要對qq郵箱進行設置,開啟POP3/SMTP/IMAP服務
因為默認這些服務是關閉的,郵箱和qq賬號綁定了。但開啟這些服務后,就需要設定單獨的郵箱密碼,此時開啟qq郵箱需要兩個密碼,一個是qq賬號的登錄密碼,一個是qq郵箱的單獨密碼。而在程序中需要使用郵箱的密碼,而不是qq密碼。(這個費了我一段時間才弄明白,開始一直出錯)
qq郵箱發送郵件源碼:
??? //將jmail.dll直接拷貝到工程中可用#import "jmail.dll"或者使用絕對路徑,如下
??? #import "E:\\SRC\\jmail.dll"
??? void main()
??? {
?? ??? ?CoInitialize(NULL);
?? ??? ?jmail::IMessagePtr pMessage("JMail.Message");
?? ??? ?pMessage->From = "abc@qq.com"; //請輸入你的qq郵箱
?? ??? ?pMessage->FromName = "辰南"; //not must
?? ??? ?pMessage->AddRecipient("uutkuu@163.com","","");
?? ??? ?pMessage->Priority = 3;
?? ??? ?pMessage->Charset = "GB2312";
?? ??? ?pMessage->Subject = "happy birthday"; //not must
?? ??? ?pMessage->Body = "Deer Tom:\n??? You are the best boy, Happy Birthday To You!";//not must?? ?
?? ??? ?pMessage->MailServerUserName = "1614651669@qq.com";//發送郵箱賬號
?? ??? ?pMessage->MailServerPassWord = "******";//請輸入郵箱密碼,不是qq賬號密碼
?? ??? ?pMessage->Send("smtp.qq.com", VARIANT_FALSE);
?? ??? ?pMessage.Release();
?? ??? ?CoUninitialize();
?? ??? ?system("pause");
??? }
運行結果,打開uutkuu@163.com郵箱可以看到:
B:發郵件步驟,前一二步相同
三、創建控制臺程序,源碼為:
??? //將jmail.dll直接拷貝到工程中可用#import "jmail.dll"或者使用絕對路徑,如下
??? #import "E:\\SRC\\jmail.dll"
??? ?
??? void main()
??? {
?? ??? ?CoInitialize(NULL);
?? ??? ?{
?? ??? ??? ?jmail::IPOP3Ptr pPOP3("JMail.POP3");?? ??? ?
?? ??? ??? ?pPOP3->Timeout = 60;//非必須,設置超時為60秒,默認為120秒
?? ??? ??? ?// 連接服務器,郵箱、密碼、服務器、端口
?? ??? ??? ?pPOP3->Connect("uutkuu@163.com","123456","pop3.163.com",110);
?? ??? ??? ?jmail::IMessagesPtr pMessages;
?? ??? ??? ?jmail::IMessagePtr pMessage;
?? ??? ??? ?pMessages = pPOP3->Messages;
?? ??? ??? ?// 獲取郵件數目(因為第0個ITEM是未用的,所以減1)
?? ??? ??? ?long lCount = pMessages->Count - 1;?? ?
?? ??? ??? ?for(long i = 1; i <= lCount; i++)// 遍歷每封信
?? ??? ??? ?{
?? ??? ??? ??? ?pMessage = pMessages->Item[i];
?? ??? ??? ??? ?bstr_t bstrSubject = pMessage->Subject;
?? ??? ??? ??? ?_bstr_t bstrFrom = pMessage->From;
?? ??? ??? ??? ?_bstr_t bstrBody = pMessage->Body;
?? ??? ??? ??? ?printf("(%d)Subject:%s, From:%s, Body:%s\r\n",i,(const char*)bstrSubject,(const char*)bstrFrom,(const char*)bstrBody);?? ??? ??? ?
?? ??? ??? ??? ?pMessage.Release();
?? ??? ??? ?}
?? ??? ??? ?pMessages->Clear();// 這里的Clear并不是清除郵件服務器上的郵件
?? ??? ??? ?pMessages.Release();
?? ??? ??? ?pPOP3->Disconnect();// 斷開連接?? ?
?? ??? ?}
?? ??? ?::CoUninitialize();
?? ??? ?system("pause");
??? }
實驗結果:
?
參考鏈接:
https://blog.csdn.net/piaopiaopiaopiaopiao/article/details/41927279
https://blog.csdn.net/zcj331/article/details/23438615
http://www.cppblog.com/Zezese/archive/2012/07/21/129187.html
總結
以上是生活随笔為你收集整理的vc++ 利用jmail组件收发邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 深入浅出SNMP及其应用实例分析
- 下一篇: Ubuntu上snmp安装、配置、启动及