Java MailSender Spring自带的邮件推送功能实现

import com.goldcard.sysManagement.common.config.Constant;
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailSender {

    public static boolean send(Address[] to, Address[] cc, Address[] bcc,String subject, String body) {

        try {
            Properties props = System.getProperties();
            props.put("mail.host", Constant.smtpServer);
            props.put("mail.smtp.auth", Boolean.valueOf(true));
            Authenticator authenticator = new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(Constant.account, Constant.password);
                    }
                };

            Session session = Session.getInstance(props, authenticator);

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(Constant.account));

            message.setSubject(subject);

            message.setRecipients(Message.RecipientType.TO, to);

            message.setRecipients(Message.RecipientType.CC, cc);

            message.setRecipients(Message.RecipientType.BCC, bcc);

            message.setSentDate(new Date());

            message.setText(body);

            Transport.send(message);

            return true;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

    public static boolean send(Address to, Address cc, Address bcc, String subject, String body) {

        try {

            Properties props = System.getProperties();

            props.put("mail.host", Constant.smtpServer);

            props.put("mail.smtp.auth", Boolean.valueOf(true));

            Authenticator authenticator = new Authenticator() {

                    protected PasswordAuthentication getPasswordAuthentication() {

                        return new PasswordAuthentication(Constant.account,Constant.password);

                    }

                };

            Session session = Session.getInstance(props, authenticator);

            Message message = new MimeMessage(session);

            message.setFrom(new InternetAddress(Constant.account));

            message.setSubject(subject);

            message.setRecipient(Message.RecipientType.TO, to);

            message.setRecipient(Message.RecipientType.CC, cc);

            message.setRecipient(Message.RecipientType.BCC, bcc);

            message.setSentDate(new Date());

            message.setText(body);

            Transport.send(message);

            return true;

        } catch (Exception e) {

            e.printStackTrace();

        }

        return false;

    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容