所需jar包 : mail.jar activation.jar additionnal.jar
发送简单的文本邮件
参考文章:https://blog.csdn.net/jianggujin/article/details/51253129
public void sendTextEmail() { String port = "25"; final String userName ="test@163.com"; final String password ="test123";(此处是开通smtp/pop3/imap服务的验证密码,不是登录密码) Properties pro = System.getProperties(); pro.put("mail.smtp.host", "smtp.163.com"); pro.put("mail.smtp.port", port); pro.put("mail.smtp.auth", "true"); // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session.getDefaultInstance(pro, new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } }); // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); // 设置邮件消息的发送者 try { mailMessage.setFrom(new InternetAddress(userName)); // 创建邮件的接收者地址,并设置到邮件消息中 mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("test@163.com")); // 设置邮件消息的主题 mailMessage.setSubject("Test Email"); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // 设置邮件消息的主要内容 mailMessage.setText("this is a test Text mail"); // 发送邮件 由于无法准备判断邮件是否发送成功,所以如果没有抛异常则表示发送成功 Transport.send(mailMessage); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); }
}
参考文章:https://www.cnblogs.com/zhujiabin/p/6254998.html
以POP3协议接收邮件
private void getPopEmail() { try { Properties props = System.getProperties(); props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props, null); URLName url = new URLName("pop3", "pop3.163.com", 110, null, "test", "test123"); // 邮件协议为pop3,邮件服务器是pop3.163.com,端口为110 Store store = session.getStore(url); store.connect(); Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE); Messag [] messages= folder.getMessages();
}if(messages.length > 0 ){
String from = messages[messages.length - 1].getFrom()[0].toString();
String subject = messages[messages.length - 1].getSubject();
Date date = messages[messages.length - 1].getSentDate();
Log.d("Mail","From: " + from);
Log.d("Mail","Subject: " + subject);
Log.d("Mail","Date: " + date);
}
}
} catch (Exception e) {
e.printStackTrace();
}
以imap协议接收邮件
public void getImapEmail()
{
String host = "imap.163.com";
int port = 993;
String username = "test@163.com";
String password = "test123";
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
//下面这行代码用的时jre里面的jar包,虽然编译的时候可以通过,但是在手机上运行时会报找不到Provider导致崩溃
// 这行代码的具体作用本人也没搞明白,但是注掉之后不影响功能实现
// Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
/* Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
这里有一个错我是这么解决的(Windows -> Preferences,Java/Compiler/Errors/Warnings->
Deprecated and restricted API, Forbidden reference (access rules),原始设定为Error修改为Warning)
这个是网上的解决方法,但是我试过之后并没有解决问题,我的方法很暴力,直接注掉不用*/
Properties props = System.getProperties();
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imap.socketFactory.port","993");
props.setProperty("mail.store.protocol","imap");
props.setProperty("mail.imap.host", host);
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.auth.login.disable", "true");
Session session = Session.getDefaultInstance(props,null);
session.setDebug(false);
IMAPFolder folder= null;
IMAPStore store=null;
try {
store=(IMAPStore)session.getStore("imap"); // 使用imap会话机制,连接服务器
store.connect(host,port,username,password);
folder=(IMAPFolder)store.getFolder("INBOX"); //收件箱
// 使用只读方式打开收件箱
folder.open(Folder.READ_WRITE);
Message[] messages=folder.getMessages();
if(messages.length > 0 ){
String from = messages[messages.length - 1].getFrom()[0].toString();
String subject = messages[messages.length - 1].getSubject();
Date date = messages[messages.length - 1].getSentDate();
Log.d("Mail","From: " + from);
Log.d("Mail","Subject: " + subject);
Log.d("Mail","Date: " + date);
}
} catch (Exception e) {
e.printStackTrace();
}
}
注意:
这里有一个坑,如果你在读取收件箱时报错,进入邮箱后发现这么一条邮件,这里是参考网上解决方法 “网易邮箱提醒:阻止了一次不安全的收信”的问题解决