Home > Java > javaTutorial > Share Jmail sending email tool class

Share Jmail sending email tool class

怪我咯
Release: 2017-06-23 11:57:55
Original
1567 people have browsed it

article mainly shares the Jmail email sending tool class with everyone, which has certain reference value. Interested friends can refer to it. I won’t talk much about the code

import lombok.extern.slf4j.Slf4j;import java.util.Properties;import javax.mail.*;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;/**
 * <b>发送邮件工具类</b><br/>
 * <b>Create on:</b>2016/10/9 16:14<br/>
 *
 * @author : liud<br/> */@Slf4jpublic class SendMailUtil {/** *  发送邮件
     * @param smtpHost
     * @param fromUser
     * @param fromUserPassword
     * @param emailPort
     * @param toUser
     * @param subject
     * @param messageText     */public static boolean doSendEmail(String smtpHost, final String fromUser,final String fromUserPassword, String userName, String emailPort, String toUser, String subject,
                                String messageText) throws Exception {boolean flag = false;try {
            Properties props = new Properties();
            props.put("mail.smtp.host", smtpHost);
            props.put("mail.smtp.port", emailPort);
            props.put("mail.smtp.auth", "true");
            Session session =  Session.getDefaultInstance(props, new Authenticator(){protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(fromUser, fromUserPassword);
                }});
            MimeMessage message = new MimeMessage(session);// 发件人InternetAddress from = new InternetAddress(fromUser);
            message.setFrom(from);// 收件人InternetAddress to = new InternetAddress(toUser);
            message.setRecipient(Message.RecipientType.TO, to);// 邮件标题            message.setSubject(subject);// 邮件内容,也可以使纯文本"text/plain"message.setContent(messageText, "text/html;charset=GBK");
            message.saveChanges();
            Transport transport = session.getTransport("smtp");// smtp验证,就是你用来发邮件的邮箱用户名密码            transport.connect(smtpHost, userName, fromUserPassword);// 发送            transport.sendMessage(message, message.getAllRecipients());
            transport.close();
            flag = true;
        } catch (AddressException e) {
            log.error("地址异常:{}", e);throw e;
        } catch (MessagingException e) {
            log.error("消息异常:{}", e);throw e;
        } catch (Exception e) {
            log.error("系统异常:{}", e);throw e;
        }return flag;
    }
}
Copy after login

The above is the detailed content of Share Jmail sending email tool class. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template