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");
}
实验结果: