Jmail and jmail tools
I haven't updated my blog for a long time. It's really a serious procrastination. It's terrible. Update a tool class first. I wrote a tool for sending emails. I don't need to 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> email sending tool </B> <br/> * <B> Create on: </B> 2016/10/9 16:14 <br/> ** @ author: liud <br/> */@ Slf4jpublic class SendMailUtil {/*** send email * @ 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 ); // sender InternetAddress from = new InternetAddress (fromUser); message. setFrom (from); // recipient InternetAddress to = new InternetAddress (toUser); message. setRecipient (Message. recipientType. TO, to); // mail title message. setSubject (subject); // The Mail content, you can also make the plain text "text/plain" message. setContent (messageText, "text/html; charset = GBK"); message. saveChanges (); Transport transport = session. getTransport ("smtp"); // smtp authentication, that is, the user name and password transport you use to send emails. connect (smtpHost, userName, fromUserPassword); // send transport. sendMessage (message, message. getAllRecipients (); transport. close (); flag = true;} catch (AddressException e) {log. error ("address exception: {}", e); throw e;} catch (MessagingException e) {log. error ("message Exception: {}", e); throw e;} catch (Exception e) {log. error ("system exception: {}", e); throw e ;}return flag ;}}