项目上加的功能,刚做好,亲测,也是吃了个大亏;值得注意的几点:
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; //登陆邮件发送服务器的用户名和地址

本文介绍如何通过javax.mail库调用企业邮箱发送邮件,重点强调了检查SMTP服务、避免包冲突、使用验证类以及配置邮件参数的重要性,并提供了相关代码示例和学习资源链接。
最低0.47元/天 解锁文章
750

被折叠的 条评论
为什么被折叠?



