美文网首页
Java MailSender Spring自带的邮件推送功能实

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

作者: 你笑时很美丶 | 来源:发表于2018-11-02 14:04 被阅读0次
    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;
    
        }
    
    }
    

    相关文章

      网友评论

          本文标题:Java MailSender Spring自带的邮件推送功能实

          本文链接:https://www.haomeiwen.com/subject/yqxcxqtx.html