javax.mail 调用企业邮箱发送邮件

项目上加的功能,刚做好,亲测,也是吃了个大亏;值得注意的几点:

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;
    //登陆邮件发送服务器的用户名和地址
    private String userName;
    private String passsword;
    //是否需要身份验证
    private boolean validate;
    //邮件主题
    private String subject;
    //邮件文本内容
    private String content;
    //邮件附件的文件名
    private String[] attachFileNames;

    /**
     * 配置邮件的会话属性
     * */
    public Properties getProperties(){
        Properties pro = new Properties();
        pro.put("mail.smtp.host", this.mailServerHost);
        pro.put("mail.smtp.port", this.mailServerPort);
        pro.put("mail.smtp.auth", "true");
        return pro;
    }

    public String getMailServerHost() {
        return mailServerHost;
    }

    public void setMailServerHost(String mailServerHost) {
        this.mailServerHost = mailServerHost;
    }

    public String[] getAttachFileNames() {
        return attachFileNames;
    }

    public void setAttachFileNames(String[] attachFileNames) {
        this.attachFileNames = attachFileNames;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public boolean isValidate() {
        return validate;
    }

    public void setValidate(boolean validate) {
        this.validate = validate;
    }

    public String getPasssword() {
        return passsword;
    }

    public void setPasssword(String passsword) {
        this.passsword = passsword;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getToAddress() {
        return toAddress;
    }

    public void setToAddress(String toAddress) {
        this.toAddress = toAddress;
    }

    public String getFromAddress() {
        return fromAddress;
    }

    public void setFromAddress(String fromAddress) {
        this.fromAddress = fromAddress;
    }

    public String getMailServerPort() {
        return mailServerPort;
    }

    public void setMailServerPort(String mailServerPort) {
        this.mailServerPort = mailServerPort;
    }
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

第三个类(邮件发送类)

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;
import java.util.Properties;

public class SimpleMailSender {
    public boolean sendTextMail(MailSenderInfo mailInfo){
        
        Properties pro = mailInfo.getProperties();
        //需要身份验证,则创建一个密码验证器(认证类)
        MyAuthenticator authenticator = new MyAuthenticator();
        authenticator.performCheck(mailInfo.getUserName(), mailInfo.getPasssword());
        //根据邮件会话属性和密码验证器构造一个发送邮件的session
        Session sendMailSession = Session.getInstance(pro, authenticator);
	//打印出验证信息
        sendMailSession.setDebug(true);

        try {
            //根据session创建一个邮件消息对象
            Message mailMessage = new MimeMessage(sendMailSession);
            //创建发送人的地址
            Address from = new InternetAddress(mailInfo.getFromAddress());
            //设置邮件的发送者到邮件消息对象
            mailMessage.setFrom(from);
            //创建接收人的地址,并设置到邮件消息对象中
            Address to = new InternetAddress(mailInfo.getToAddress());
            mailMessage.setRecipient(Message.RecipientType.TO, to);
            //设置邮件对象的主题
            mailMessage.setSubject(mailInfo.getSubject());
            //设置邮件的发送时间
            mailMessage.setSentDate(new Date());
            //设置邮件的正文
            String mailContent = mailInfo.getContent();
            mailMessage.setText(mailContent);
            //发送邮件
            Transport.send(mailMessage);
            return true;
        }catch (MessagingException ex){
            ex.printStackTrace();
        }
            return false;
    }

}
-----------------------------------------------------------------------------
测试类
 
  
import com.fndsoft.assistor.holiday.util.MailSenderInfo;
import com.fndsoft.assistor.holiday.util.SimpleMailSender;

public class TestSendEmail {
    public static void main(String[] args){
        //这个类主要是设置邮件
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost("smtp.qiye.163.com");//注意服务器名称
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
        mailInfo.setUserName("[email protected]");
        mailInfo.setPasssword("***************");
        mailInfo.setFromAddress("[email protected]");
        mailInfo.setToAddress("[email protected]");
        mailInfo.setSubject("验证用户,发送验证码");
        mailInfo.setContent("【确认绑定】验证码:"+"112233");
        //这个类主要来发送邮件
        SimpleMailSender sms = new SimpleMailSender();
        sms.sendTextMail(mailInfo);
    }
}

出了那个 Authenticator failed, 查了很多资料,基本上按上面设置好就没啥大问题了

这里有几篇博客可以看一下,学到了很多:

http://blog.csdn.net/wangyonglin1123/article/details/47446919

http://blog.csdn.net/karem/article/details/4646071

欢迎各位提问和指点。

你可能感兴趣的:(java)