SendMail发送回执及读取收件箱
Posted dream2hc
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SendMail发送回执及读取收件箱相关的知识,希望对你有一定的参考价值。
一、SendMail发送有回执提示
1.邮件发送配置
Properties props = new Properties(); String smtp = "smtp.qq.com"; //使用域名地址,建议不要使用ip地址,因为IP有可能会换掉。 String servername = "******@qq.com" ; String serverpaswd ="*******";//QQ邮箱的SMTP的授权码 javax.mail.Session mailSession; //邮件会话对象 javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象 props = java.lang.System.getProperties(); //获得系统属性对象 props.setProperty("mail.smtp.timeout", "120000");//2分钟 props.put("mail.smtp.host", smtp); //设置SMTP主机 props.put("mail.smtp.auth", "true"); //是否到服务器用户名和密码验证 //到服务器验证发送的用户名和密码是否正确 Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(servername, serverpaswd); //设置邮件会话 mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther); //设置传输协议 javax.mail.Transport transport = mailSession.getTransport("smtp");
2.邮件回执设置
// 要求阅读回执(收件人阅读邮件时会提示回复发件人,表明邮件已收到,并已阅读) mimeMsg.setHeader("Disposition-Notification-To", from); mimeMsg.reply(true); mimeMsg.saveChanges();
3.到服务器验证发送的用户名和密码是否正确
Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(servername, serverpaswd);
//设置邮件会话
mailSession = javax.mail.Session.getInstance(props, (Authenticator) myEmailAuther);
Email_Autherticatorbean 类:
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class Email_Autherticatorbean extends Authenticator { private String m_username = null; private String m_userpass = null; public void setUsername(String username) { m_username = username; } public void setUserpass(String userpass) { m_userpass = userpass; } public Email_Autherticatorbean(String username, String userpass) { super(); setUsername(username); setUserpass(userpass); } public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(m_username, m_userpass); } }
二、读取QQ邮箱的邮件
注意:1. 邮箱验证码,如何获取? 2.读取QQ邮箱时,需要设置读取‘全部’,否则只能获取30天内的邮件。
import java.security.Security; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.mail.Address; import javax.mail.Folder; import javax.mail.Message; import javax.mail.Session; import javax.mail.Store; import javax.mail.search.AndTerm; import javax.mail.search.BodyTerm; import javax.mail.search.FromStringTerm; import javax.mail.search.SearchTerm; import javax.mail.search.SubjectTerm; class MailReceives { public static void main(String[] args) throws Exception { // 定义连接POP3服务器的属性信息 String protocol = "pop3"; String pop3Port = "995"; //SSl 加密使用 端口:995;139/163邮件使用端口:110 String pop3Server = "pop.qq.com"; String username = "****@qq.com"; String password = "*****"; // QQ邮箱的SMTP的授权码 //设置SSL连接、邮件环境 Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; Properties props = new Properties(); props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY); props.setProperty("mail.store.protocol", protocol); // 使用的协议(JavaMail规范要求) props.setProperty("mail.smtp.host", pop3Server); // 发件人的邮箱的 SMTP服务器地址 props.setProperty("mail.pop3.port", pop3Port); //到服务器验证发送的用户名和密码是否正确 Email_Autherticatorbean myEmailAuther = new Email_Autherticatorbean(username, password); // 获取连接 Session session = Session.getInstance(props, myEmailAuther); session.setDebug(false); // 获取Store对象 Store store = session.getStore(protocol); store.connect(pop3Server, username, password); // POP3服务器的登陆认证 // 通过POP3协议获得Store对象调用这个方法时,邮件夹名称只能指定为"INBOX" Folder folder = store.getFolder("INBOX");// 获得用户的邮件帐户 folder.open(Folder.READ_WRITE); // 设置对邮件帐户的访问权限 //folder.getMessages(); //获取所有的邮件 //搜索发件人包含***的邮件 SearchTerm term = new FromStringTerm("@139.com"); // 搜索主题包含***的邮件 SearchTerm subjectTerm = new SubjectTerm("已读:"); SearchTerm andTerm=new AndTerm(term, subjectTerm); Message[] messages = folder.search(andTerm,folder.getMessages());// 得到邮箱帐户中的所有邮件 System.err.println(messages.length); String body = "报表配置县市"; String sendDate = "2018-06-22 16:59:36"; String condition = "这是邮件收条, "+sendDate+" 发给 ****@139.com, 主题为 "+body+" 的信件已被接收"; SearchTerm contentTerm = new BodyTerm(condition); Message[] msg = folder.search(contentTerm, messages); // 打印不同状态的邮件数量 System.out.println("收件箱中共" + msg.length + "封邮件!"); for (Message message : msg) { String subject = message.getSubject();// 获得邮件主题 Address from = (Address) message.getFrom()[0];// 获得发送者地址 System.out.println("邮件的主题为: " + subject + " 发件人地址为: " + from); } folder.close(false);// 关闭邮件夹对象 store.close(); // 关闭连接对象 } }
三、资源引用
以上是关于SendMail发送回执及读取收件箱的主要内容,如果未能解决你的问题,请参考以下文章
邮件发送失败,Sendmail Headers too large