maven之javamail操作qq的smtp与imap
来源:互联网 发布:python 数据接口 编辑:程序博客网 时间:2024/06/07 06:05
参考:
java mail 官网 https://java.net/projects/javamail/pages/Home#Samples
maven 添加javamail支持 http://blog.csdn.net/dai_richard/article/details/7070713
qq的imap需要使用ssl 的993端口进行通信 http://blog.csdn.net/superstorm5/article/details/2157022
书籍-- java 网络编程精解(孙卫琴)
先看一些理论的东西
qq的邮箱账号 先要开通imap/smtp 在邮箱的 设置--账号 里面 如下
最后再来个测试代码 代码上都加了注释 不废话
package com.undergrowth;import java.io.IOException;import java.util.Date;import java.util.Enumeration;import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Folder;import javax.mail.Header;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Part;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Store;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;/** * 简单发送和接收邮件 * @author u1 * */public class SimpleSendReceiveMessage {//邮件通信会话Session session;//邮件接收处理对象Store store;//连接邮件发送的账号与密码String username="xxxxxxx";private String passwd="xxxxxx";private String receiveHost="imap.qq.com";private String sendHost="smtp.qq.com";/** * 邮件配置参数和连接接收邮件服务器 * @throws MessagingException */private void init() throws MessagingException{Properties properties=new Properties();//设置发送和接收协议properties.put("mail.transport.protocal", "smtp");properties.put("mail.store.protocal", "imap");//设置ssl的端口properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");properties.setProperty("mail.imap.socketFactory.fallback", "false");properties.setProperty("mail.imap.port", "993");properties.setProperty("mail.imap.socketFactory.port", "993");/*properties.put("mail.imap.port", "993");properties.put("mail.smtp.port", "465");*///smtp认证properties.put("mail.smtp.auth", "true");//设置发送和接收处理类properties.put("mail.transport.class", "com.sun.mail.smtp.SMTPTransport");properties.put("mail.imap.class", "com.sun.mail.imap.IMAPStore");//设置发送邮件服务器properties.put("mail.smtp.host",sendHost);//获取邮件通信会话Authenticator auth=new MailAuthenticator();session=Session.getDefaultInstance(properties,auth);session.setDebug(true);//获取接收邮件对象store=session.getStore("imap");//连接接收邮件服务器store.connect(receiveHost, null, null);}/** * 关闭邮件接收服务器 * @throws MessagingException * @throws IOException */public void close() throws MessagingException, IOException{store.close();}/** * 创建一封简单的邮件 * @param fromAddr * @param toAddr * @return * @throws AddressException * @throws MessagingException */public Message createSimpleMessage(String fromAddr,String toAddr) throws AddressException, MessagingException{//建立一封邮件MimeMessage message=new MimeMessage(session);//设置发送者和接收者message.setFrom(new InternetAddress(fromAddr));message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddr));//设置主题message.setSubject("使用JAVAMAIL发送邮件");//设置日期message.setSentDate(new Date(System.currentTimeMillis()));//设置正文message.setText("今天是2015-6-12,离职一个周,准备下一份工作");return message;}public Message createComplexMessage(String fromAddr,String toAddr) throws AddressException, MessagingException{//建立一封邮件MimeMessage message=new MimeMessage(session);//设置发送者和接收者message.setFrom(new InternetAddress(fromAddr));message.setRecipient(Message.RecipientType.TO, new InternetAddress(toAddr));//设置主题message.setSubject("使用JAVAMAIL发送邮件");//设置日期message.setSentDate(new Date(System.currentTimeMillis()));//设置正文Multipart mp=createMultiPart();message.setContent(mp);return message;}/** * 创建复杂的正文 * @return * @throws MessagingException */private Multipart createMultiPart() throws MessagingException {// TODO Auto-generated method stubMultipart multipart=new MimeMultipart();//第一块BodyPart bodyPart1=new MimeBodyPart();bodyPart1.setText("创建复杂的邮件,此为正文部分");multipart.addBodyPart(bodyPart1);//第二块 以附件形式存在MimeBodyPart bodyPart2=new MimeBodyPart();//设置附件的处理器FileDataSource attachFile=new FileDataSource(ClassLoader.getSystemResource("attach.txt").getFile());DataHandler dh=new DataHandler(attachFile);bodyPart2.setDataHandler(dh);bodyPart2.setDisposition(Part.ATTACHMENT);bodyPart2.setFileName("test");multipart.addBodyPart(bodyPart2);return multipart;}/** * 发送邮件 * @param message * @throws MessagingException */public void send(Message message) throws MessagingException{Transport.send(message);}/** * 接收邮件 * @throws Exception */public void receive() throws Exception{browseMessageFromFolder("INBOX");}/** * 根据邮件夹名称浏览邮件 * @param folderName * @throws Exception */private void browseMessageFromFolder(String folderName) throws Exception {// TODO Auto-generated method stubFolder folder=store.getFolder(folderName);if(folder==null) throw new Exception(folderName+"邮件夹不存在");browseMessageFromFolder(folder);}/** * 根据邮件夹对象浏览邮件 * @param folder * @throws MessagingException * @throws IOException */private void browseMessageFromFolder(Folder folder) throws MessagingException, IOException {// TODO Auto-generated method stubfolder.open(Folder.READ_ONLY);System.out.println("总共有"+folder.getMessageCount()+"封邮件");System.out.println("总共有"+folder.getUnreadMessageCount()+"封未读邮件");Message[] messages=folder.getMessages();for (int i = 1; i <=messages.length; i++) {System.out.println("这是第"+i+"封邮件");getMessageHeader(folder.getMessage(i));writeSubjectToOutPutStream(folder.getMessage(i));;}folder.close(false);}/** * 遍历每封邮件的头部部分 * @param message * @throws MessagingException */private void getMessageHeader(Message message) throws MessagingException {// TODO Auto-generated method stub@SuppressWarnings("unchecked")Enumeration<Header> allHeader=message.getAllHeaders();for(;allHeader.hasMoreElements();){Header header=allHeader.nextElement();System.out.println(header.getName()+":"+header.getValue());}}/** * 将每封邮件的主题写入输出流中 * @param message * @throws MessagingException */private void writeSubjectToOutPutStream(Message message) throws MessagingException {// TODO Auto-generated method stubSystem.out.println("邮件主题为:"+message.getSubject());}public static void main(String[] args){SimpleSendReceiveMessage sendReceiveMessage=new SimpleSendReceiveMessage();try {sendReceiveMessage.init();Message message=sendReceiveMessage.createSimpleMessage(sendReceiveMessage.username, sendReceiveMessage.username);sendReceiveMessage.send(message);message=sendReceiveMessage.createComplexMessage(sendReceiveMessage.username, sendReceiveMessage.username);sendReceiveMessage.send(message);sendReceiveMessage.receive();sendReceiveMessage.close();} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}}/** * 登陆认证 * @author u1 * */private class MailAuthenticator extends Authenticator{@Overrideprotected PasswordAuthentication getPasswordAuthentication() {// TODO Auto-generated method stubreturn new PasswordAuthentication(username, passwd);}}}
maven的javamail依赖
<dependencies><dependency><groupId>javax.activation</groupId><artifactId>activation</artifactId><version>1.1</version></dependency><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4</version></dependency></dependencies>
0 1
- maven之javamail操作qq的smtp与imap
- maven之javamail操作qq的smtp与imap
- SMTP,POP3.IMAP的区别与联系
- SMTP,POP3.IMAP的区别与联系
- 【计算机网络】:pop,IMAP,SMTP协议的区别与联系
- 什么是POP3、SMTP和IMAP以及POP3与IMAP的区别
- javamail发信和收信机制(smtp、pop3、imap)
- PHP IMAP收QQ邮件,SMTP存入另外QQ邮箱
- javamail+imap
- 电子邮件协议之SMTP、POP3和IMAP
- QQ邮箱IMAP/SMTP服务,设置 未成功原因
- QQ企业邮箱设置IMAP、POP3/SMTP及其SSL加密方式
- SMTP协议与JavaMail相关类SMTPTransport的学习
- POP3 SMTP IMAP SMTP
- Javamail使用IMAP同步QQ自定义文件夹问题
- 电子邮件的POP3/SMTP/IMAP服务
- POP3、SMTP、IMAP 的作用和联系
- 对POP3,IMAP,SMTP的认识。
- 获取主流浏览器滚动条的位置
- 浅谈大数据与智能电厂
- KVO
- 如何在十天内掌握线性代数?
- 黑马程序员----JAVA基础语法
- maven之javamail操作qq的smtp与imap
- EmguCV在C#中的应用
- 记录property中的所有属性特性
- 2015061201 - 马王堆汉墓(上)
- 单元测试系列之1:开发测试的那些事儿
- C++常成员函数 - const 关键字
- 好玩的WPF第三弹:颤抖吧,地球!消失吧,地球!
- iOS学习笔记
- 栈学习之括号匹配