项目上加的功能,刚做好,亲测,也是吃了个大亏;值得注意的几点:
1、检查smpt、pop3和imap服务是否开启,我用的163免费企业邮箱默认是开启;
2、检查包,不能冲突,再就是javax.mail-xxx.jar,不是那种javax.mail-api-xxx.jar;
3、其他就是验证类一定要加,然后“mail.smtp.auth","true",默认要设置使用验证。
废话不多说,上代码
第一个类(验证类)
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; public class MyAuthenticator extends Authenticator{ String userName = null; String password = null; public MyAuthenticator() {} public PasswordAuthentication performCheck(String user, String pass){ userName = user; password = pass; return getPasswordAuthentication(); } protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName, password); } }------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
第二个类(邮件信息类)
import java.util.Properties; public class MailSenderInfo{ //发送邮件的服务器和端口 private String mailServerHost; private String mailServerPort = "25"; //发送人的地址 private String fromAddress; //接受者的地址 private String toAddress;