QQ接收邮箱:
package com.sam.mail;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Enumeration;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import org.junit.Test;
import com.sun.mail.imap.IMAPMessage;
/**
*
* @author 1372940767@qq.com
*
*/
public class EmailClient {
private static final String IMAP = "imap";
@Test
public void testRece() throws Exception{
reviceQQEmail(System.getProperty("email"), System.getProperty("password"));
}
public void reviceQQEmail(String username, String password) throws Exception{
String host = "imap.qq.com";
String port = "143";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
//props.setProperty("mail.imap.socketFactory.fallback", "false");
// props.setProperty("mail.imap.port", port);
props.setProperty("mail.imap.socketFactory.port", port);
props.setProperty("mail.store.protocol","imap");
props.setProperty("mail.imap.host", host);
props.setProperty("mail.imap.port", port);
props.setProperty("mail.imap.auth.login.disable", "true");
Session session = Session.getDefaultInstance(props,null);
session.setDebug(false);
Store store = session.getStore("imap");
store.connect(host, username, password);
Folder inbox = null;
try {
inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
System.out.println("收件箱的邮件数:" + messages.length);
IMAPMessage msg;
for (int i = 0; i < messages.length; i++) {
msg = (IMAPMessage)messages[i];
String from = decodeText(msg.getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
System.out.println("TITLE:" + msg.getSubject());
System.out.println("SIZE:" + msg.getSize());
System.out.println("DATE:" + msg.getSentDate());
Enumeration headers = msg.getAllHeaders();
System.out.println("----------------------allHeaders-----------------------------");
while (headers.hasMoreElements()) {
Header header = (Header)headers.nextElement();
System.out.println(header.getName()+" ======= "+header.getValue());
}
parseMultipart((Multipart) msg.getContent());
}
} finally {
try {
inbox.close(false);
} catch (Exception e) {
}
try {
store.close();
} catch (Exception e) {
}
}
}
protected String decodeText(String text) throws UnsupportedEncodingException {
if (text == null)
return null;
if (text.startsWith("=?GB") || text.startsWith("=?gb"))
text = MimeUtility.decodeText(text);
else
text = new String(text.getBytes("ISO8859_1"));
return text;
}
/**
* 对复杂邮件的解析
* @param multipart
* @throws MessagingException
* @throws IOException
*/
public static void parseMultipart(Multipart multipart) throws MessagingException, IOException {
int count = multipart.getCount();
System.out.println("couont = "+count);
for (int idx=0;idx<count;idx++) {
BodyPart bodyPart = multipart.getBodyPart(idx);
System.out.println(bodyPart.getContentType());
if (bodyPart.isMimeType("text/plain")) {
System.out.println("plain................."+bodyPart.getContent());
} else if(bodyPart.isMimeType("text/html")) {
System.out.println("html..................."+bodyPart.getContent());
} else if(bodyPart.isMimeType("multipart/*")) {
Multipart mpart = (Multipart)bodyPart.getContent();
parseMultipart(mpart);
} else if (bodyPart.isMimeType("application/octet-stream")) {
String disposition = bodyPart.getDisposition();
System.out.println(disposition);
if (disposition.equalsIgnoreCase(BodyPart.ATTACHMENT)) {
String fileName = bodyPart.getFileName();
InputStream is = bodyPart.getInputStream();
copy(is, new FileOutputStream("D:\\"+fileName));
}
}
}
}
/**
* 文件拷贝,在用户进行附件下载的时候,可以把附件的InputStream传给用户进行下载
* @param is
* @param os
* @throws IOException
*/
public static void copy(InputStream is, OutputStream os) throws IOException {
byte[] bytes = new byte[1024];
int len = 0;
while ((len=is.read(bytes)) != -1 ) {
os.write(bytes, 0, len);
}
if (os != null)
os.close();
if (is != null)
is.close();
}
}
分享到:
相关推荐
可以利用JavaMail收发Gmail邮件,ssl;写得比较详细,
NULL 博文链接:https://zhaoshijie.iteye.com/blog/804788
javaMail源代码,可以使用QQ邮箱等发送邮件,可以用于项目用户模块邮箱验证上,提供源代码做交流!
基于javamail的接收邮件示例!!!可以作为参考!!!
javamail发送、接收邮件详细教程
javamail接收邮件例子,我稍微改动了一下,有些错误和异常抛出了
javamail 发送邮件的例子(通过了qq 和gmail的邮箱)有SSL 安全的方式
javamail实现的163邮箱发送到腾讯邮件的示例、 已经javamail的详细API级demo!
新邮件功能没实现新邮件功能没实现新邮件功能没实现新邮件功能没实现
JAVA100例之实例48 使用JavaMail接收邮件
使用JavaMail对用户注册,重置密码实现邮箱验证.
使用Java完成的简单邮箱系统,界面由Java Swing实现,具有以下功能: 1、邮件收发等邮箱基本功能 2、可设置黑名单、白名单,并过滤邮件 3、使用朴素贝叶斯算法检测评估邮件是否为垃圾邮件
JavaMail - 注册邮箱激活源码 - 详情:https://blog.csdn.net/Dream_Weave/article/details/85215805
JavaMail发送和接收,明细介绍具体使用方式等等
很好的邮件处理代码,1.5版本,作为build path放在eclipse里面,就可以用啦! oracle官方推的,很好!jdk1.6版本以上用起来很方便。
利用JavaMail发送邮件的实例,可以直接运行
利用JavaMail for Android和MailSender实现发送邮件
对javaMail发送邮件进行了详细的说明,具体代码可以实现相应功能;同时对接收邮箱进行了测试连接。
利用JavaMail API来发送Email的工具类 支持单发,群发及无限制数量的附件功能,并同时支持简单邮件和HTML两种格式的邮件内容 支持抄送,密送功能 具体使用参见其mail方法的使用示例
使用jsp实现javaMail邮箱功能开发