关于JavaMail实现QQ邮箱发送邮件的简单实现1
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于JavaMail实现QQ邮箱发送邮件的简单实现1相关的知识,希望对你有一定的参考价值。
一 确认QQ是否开启了POP3/SMPT协议
1.登陆QQ,打开QQ邮箱,点击“设置”
2.点击“账户”,拉到下面“POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务”
3.点击“开启”,会看见几种验证方式,选择任意一种觉得方便的方式
4.在验证完之后会得到一个授权码,这个授权码先拷贝下来,等一下会作为系统邮箱的密码,要是授权码忘记了也没有关系,可以再重新生成
二 测试代码
1.在成功开启QQ的POP3/SMTP协议之后,就可以测试发送了
1 import java.util.Properties; 2 3 import javax.mail.Message; 4 import javax.mail.MessagingException; 5 import javax.mail.Session; 6 import javax.mail.Transport; 7 import javax.mail.internet.AddressException; 8 import javax.mail.internet.InternetAddress; 9 import javax.mail.internet.MimeMessage; 10 import javax.mail.internet.MimeMessage.RecipientType; 11 12 public class SendmailUtil { 13 public static void main(String[] args) throws AddressException, MessagingException { 14 Properties properties = new Properties(); 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 = new MimeMessage(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.3 DEBUG: 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: useEhlo true, useAuth true DEBUG SMTP: trying to connect to host "smtp.qq.com", port 465, isSSL true 220 smtp.qq.com Esmtp QQ Mail Server DEBUG SMTP: connected to host "smtp.qq.com", port: 465 EHLO DESKTOP-P7GG6RA 250-smtp.qq.com 250-PIPELINING 250-SIZE 73400320 250-AUTH LOGIN PLAIN 250-AUTH=LOGIN 250-MAILCOMPRESS 250 8BITMIME 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 LOGIN 334 VXNlcm5hbWU6 NDQ3ODQzMDk4QHFxLmNvbQ== 334 UGFzc3dvcmQ6 bXZ6dHJicHpxeGt5YmllYw== 235 Authentication successful DEBUG SMTP: use8bit false MAIL FROM:<发件人@qq.com> 250 Ok RCPT TO:<收件人@qq.com> 250 Ok DEBUG SMTP: Verified Addresses DEBUG SMTP: 收件人@qq.com DATA 354 End data with <CR><LF>.<CR><LF> From: 发件人@qq.com To: 收件人@qq.com Message-ID: <[email protected]> Subject: =?GBK?B?08lKYXZhTWFpbLeis/a1xLLiytTTyrz+?= MIME-Version: 1.0 Content-Type: text/plain; charset=GBK Content-Transfer-Encoding: base64 xNrI3c6qo7og1eLKx7Xa0ru34mphdmG3osvNwLS1xNPKvP6how== . 250 Ok: queued as
3.再去收件人邮箱里面去查看是否收到邮件
三 注意:
只需要发件人邮箱(也就是自己设定的系统邮箱)开通POP3/SMTP协议,收件人的邮箱是不需要开通POP3/SMTP协议的
希望整理出来的这些内容对大家有帮助,祝大家每天都开开心心!^-^
以上是关于关于JavaMail实现QQ邮箱发送邮件的简单实现1的主要内容,如果未能解决你的问题,请参考以下文章