使用JavaMail收发邮件
jsorder
9年前
<p>已使用 163 邮箱测试通过,且支持 SSL 连接。</p> <p><strong>发送邮件</strong></p> <p>示例:Jack 发送一封邮件给 Rose。</p> <pre> <code>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("发送完毕!"); } }</code></pre> <p><strong>收取邮件</strong></p> <p>示例:Rose 收取最近一封邮件。</p> <pre> <code>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("接收完毕!"); } }</code></pre> <p><strong>常用邮件协议</strong></p> <p>发送邮件:SMTP</p> <p>收取邮件:POP3、IMAP</p> <p><strong>常用邮件配置项</strong></p> <table border="1" cellpadding="2" cellspacing="0"> <tbody> <tr> <td>配置项</td> <td>说明</td> </tr> <tr> <td>mail.xxx.ssl.enable</td> <td>是否支持 SSL 连接</td> </tr> <tr> <td>mail.xxx.host</td> <td>邮件服务器主机名</td> </tr> <tr> <td>mail.xxx.port</td> <td>邮件服务器端口号</td> </tr> <tr> <td>mail.xxx.auth</td> <td>是否进行身份认证</td> </tr> </tbody> </table> <p><br> 说明:xxx 表示协议名称,例如:smtp、pop3 等。</p> <p><strong>默认端口号</strong></p> <table border="1" cellpadding="2" cellspacing="0"> <tbody> <tr> <td> </td> <td>SMTP</td> <td>POP3</td> <td>IMAP</td> </tr> <tr> <td>普通方式</td> <td>25</td> <td>110</td> <td>143</td> </tr> <tr> <td>SSL 方式</td> <td>465</td> <td>995</td> <td>993</td> </tr> </tbody> </table> <p>使用 Apache Commons Email 发送邮件</p> <p> </p> <pre> <code>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("发送完毕!"); } }</code></pre> <p>参考:<a href="/misc/goto?guid=4959614896907685474" rel="nofollow">http://commons.apache.org/proper/commons-email/userguide.html</a></p> <p>via http://my.oschina.net/huangyong/blog/178641</p>