明天要做关于发送邮件的接口,虽然我之前已用Java Mail做过许多关于邮件的发送。但同事说有点难点,虽我还不知难点在哪,还是要复习下。凡事预则立,不预则废嘛~
所需的包:
Java Mail : 目前,可从如下地址下载:JavaMail API
Activation : 目前,可从如下地址下载:JavaBeans Activation Framework (JAF)
参考的好文章:
JavaMail:用Authenticator的子类进行身份验证及策略模式
代码托管于:https://github.com/nicchagil/multi-project的MailSender文件夹。
基于SMTP发送一个简单的邮件
首先,需要一个认证器:
package No001_基于SMTP的文本邮件; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class SimpleAuthenticator extends Authenticator { private String username; private String password; public SimpleAuthenticator(String username, String password) { super(); this.username = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }