//接收gmail邮件
public void receiveGmail() throws MessagingException, UnsupportedEncodingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
// 以下步骤跟一般的JavaMail操作相同
Session session = Session.getInstance(props, null);
// 请将红色部分对应替换成你的邮箱帐号和密码
// URLName urln = new URLName("pop3", "pop.gmail.com", 995, null, "
[email protected]", "1111111");
URLName urln = new URLName("pop3", "pop3.live.com", 995, null, "
[email protected]", "1111111");
Store store = session.getStore(urln);
Folder inbox = null;
try {
System.out.println("start connect mail server");
store.connect();
System.out.println("end connect mail server");
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);
for (int i = 0; i < messages.length; i++) {
// 邮件发送者
String from = decodeText(messages[i].getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:" + ia.getPersonal() + '(' + ia.getAddress() + ')');
// 邮件标题
System.out.println("TITLE:" + messages[i].getSubject());
// 邮件大小
System.out.println("SIZE:" + messages[i].getSize());
// 邮件发送时间
System.out.println("DATE:" + messages[i].getSentDate());
}
} finally {
try {
inbox.close(false);
} catch (Exception e) {
}
try {
store.close();
} catch (Exception e) {
}
}
}
public void sendGmail() throws AddressException, MessagingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
final String username = "[邮箱帐号]";
final String password = "[邮箱密码]";
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress(username + "@mo168.com"));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[收件人地址]", false));
msg.setSubject("Hello");
msg.setText("How are you");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent.");
}