使用JavaMail收發郵件
已使用 163 郵箱測試通過,且支持 SSL 連接。
發送郵件
示例:Jack 發送一封郵件給 Rose。
public class SendMail {
public static void main(String[] args) {
boolean isSSL = true;
String host = "smtp.163.com";
int port = 465;
String from = "jack@163.com";
String to = "rose@163.com";
boolean isAuth = true;
final String username = "jack@163.com";
final String password = "jack";
Properties props = new Properties();
props.put("mail.smtp.ssl.enable", isSSL);
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", port);
props.put("mail.smtp.auth", isAuth);
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("主題");
message.setText("內容");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("發送完畢!");
}
}
收取郵件
示例:Rose 收取最近一封郵件。
import java.util.Date;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
public class FetchMail {
public static void main(String[] args) {
String protocol = "pop3";
boolean isSSL = true;
String host = "pop.163.com";
int port = 995;
String username = "rose@163.com";
String password = "rose";
Properties props = new Properties();
props.put("mail.pop3.ssl.enable", isSSL);
props.put("mail.pop3.host", host);
props.put("mail.pop3.port", port);
Session session = Session.getDefaultInstance(props);
Store store = null;
Folder folder = null;
try {
store = session.getStore(protocol);
store.connect(username, password);
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
int size = folder.getMessageCount();
Message message = folder.getMessage(size);
String from = message.getFrom()[0].toString();
String subject = message.getSubject();
Date date = message.getSentDate();
System.out.println("From: " + from);
System.out.println("Subject: " + subject);
System.out.println("Date: " + date);
} catch (NoSuchProviderException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} finally {
try {
if (folder != null) {
folder.close(false);
}
if (store != null) {
store.close();
}
} catch (MessagingException e) {
e.printStackTrace();
}
}
System.out.println("接收完畢!");
}
}
常用郵件協議
發送郵件:SMTP
收取郵件:POP3、IMAP
常用郵件配置項
配置項 | 說明 |
mail.xxx.ssl.enable | 是否支持 SSL 連接 |
mail.xxx.host | 郵件服務器主機名 |
mail.xxx.port | 郵件服務器端口號 |
mail.xxx.auth | 是否進行身份認證 |
說明:xxx 表示協議名稱,例如:smtp、pop3 等。
默認端口號
SMTP | POP3 | IMAP | |
普通方式 | 25 | 110 | 143 |
SSL 方式 | 465 | 995 | 993 |
使用 Apache Commons Email 發送郵件
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;
public class SendMail {
public static void main(String[] args) {
boolean isSSL = true;
String host = "smtp.163.com";
int port = 465;
String from = "jack@163.com";
String to = "rose@163.com";
String username = "jack@163.com";
String password = "jack";
try {
Email email = new SimpleEmail();
email.setSSLOnConnect(isSSL);
email.setHostName(host);
email.setSmtpPort(port);
email.setAuthentication(username, password);
email.setFrom(from);
email.addTo(to);
email.setSubject("主題");
email.setMsg("內容");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("發送完畢!");
}
}
參考:http://commons.apache.org/proper/commons-email/userguide.html
via http://my.oschina.net/huangyong/blog/178641
本文由用戶 jsorder 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!