本文主要是介绍java程序收取qq邮箱的邮件_关于JavaMail实现QQ邮箱发送邮件的实现(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一 确认QQ是否开启了POP3/SMPT协议
1.登陆QQ,打开QQ邮箱,点击“设置”
2.点击“账户”,拉到下面“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”
3.点击“开启”,会看见几种验证方式,选择任意一种觉得方便的方式
4.在验证完之后会得到一个授权码,这个授权码先拷贝下来,等一下会作为系统邮箱的密码,要是授权码忘记了也没有关系,可以再重新生成
二 测试代码
1.在成功开启QQ的POP3/SMTP协议之后,就可以测试发送了
1 importjava.util.Properties;2
3 importjavax.mail.Message;4 importjavax.mail.MessagingException;5 importjavax.mail.Session;6 importjavax.mail.Transport;7 importjavax.mail.internet.AddressException;8 importjavax.mail.internet.InternetAddress;9 importjavax.mail.internet.MimeMessage;10 importjavax.mail.internet.MimeMessage.RecipientType;11
12 public classSendmailUtil {13 public static void main(String[] args) throwsAddressException, MessagingException {14 Properties properties = newProperties();15 properties.put("mail.transport.protocol", "smtp"); //连接协议
16 properties.put("mail.smtp.host", "smtp.qq.com"); //主机名
17 properties.put("mail.smtp.port", 465); //端口号
18 properties.put("mail.smtp.auth", "true");19 properties.put("mail.smtp.ssl.enable", "true"); //设置是否使用ssl安全连接 ---一般都使用
20 properties.put("mail.debug", "true"); //设置是否显示debug信息 true 会在控制台显示相关信息21 //得到回话对象
22 Session session =Session.getInstance(properties);23 //获取邮件对象
24 Message message = newMimeMessage(session);25 //设置发件人邮箱地址
26 message.setFrom(new InternetAddress("发件人邮箱@qq.com"));27 //设置收件人地址
28 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("收件人邮箱@qq.com") });29 //设置邮件标题
30 message.setSubject("由JavaMail发出的测试邮件");31 //设置邮件内容
32 message.setText("内容为: 这是第一封java发送来的邮件。");33 //得到邮差对象
34 Transport transport =session.getTransport();35 //连接自己的邮箱账户
36 transport.connect("发件人邮箱@qq.com", "授权码");//密码为刚才得到的授权码37 //发送邮件
38 transport.sendMessage(message, message.getAllRecipients());39 }40 }
2.在运行代码之后,要是控制台输出为以下内容,则测试成功
DEBUG: JavaMail version 1.4.3DEBUG: successfully loaded resource:/META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource:/META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlotrue, useAuth trueDEBUG SMTP: trying to connect to host"smtp.qq.com", port 465, isSSL true
220smtp.qq.com Esmtp QQ Mail Server
DEBUG SMTP: connected to host"smtp.qq.com", port: 465EHLO DESKTOP-P7GG6RA250-smtp.qq.com250-PIPELINING250-SIZE 73400320
250-AUTH LOGIN PLAIN250-AUTH=LOGIN250-MAILCOMPRESS2508BITMIME
DEBUG SMTP: Found extension"PIPELINING", arg ""DEBUG SMTP: Found extension"SIZE", arg "73400320"DEBUG SMTP: Found extension"AUTH", arg "LOGIN PLAIN"DEBUG SMTP: Found extension"AUTH=LOGIN", arg ""DEBUG SMTP: Found extension"MAILCOMPRESS", arg ""DEBUG SMTP: Found extension"8BITMIME", arg ""DEBUG SMTP: Attempt to authenticate
DEBUG SMTP: check mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
AUTH LOGIN334VXNlcm5hbWU6
NDQ3ODQzMDk4QHFxLmNvbQ==
334UGFzc3dvcmQ6
bXZ6dHJicHpxeGt5YmllYw==
235Authentication successful
DEBUG SMTP: use8bitfalseMAIL FROM:
250Ok
RCPT TO:
250Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: 收件人@qq.com
DATA354 End data with .From: 发件人@qq.com
To: 收件人@qq.com
Message-ID: <696865876.0.1500865241924.JavaMail.SG@DESKTOP-P7GG6RA>Subject:=?GBK?B?08lKYXZhTWFpbLeis/a1xLLiytTTyrz+?=MIME-Version: 1.0Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
xNrI3c6qo7og1eLKx7Xa0ru34mphdmG3osvNwLS1xNPKvP6how==.250 Ok: queued as
3.再去收件人邮箱里面去查看是否收到邮件
三 注意:
只需要发件人邮箱(也就是自己设定的系统邮箱)开通POP3/SMTP协议,收件人的邮箱是不需要开通POP3/SMTP协议的
希望整理出来的这些内容对大家有帮助,祝大家每天都开开心心!^-^
这篇关于java程序收取qq邮箱的邮件_关于JavaMail实现QQ邮箱发送邮件的实现(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!