Jmail and jmail tools

Source: Internet
Author: User

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 ;}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.