java javaMail tools used to send mail

1. dependence introduction

<!--javaMail-->
<dependency>
    <groupId>javax.mail</groupId>
    <artifactId>javax.mail-api</artifactId>
    <version>1.5.6</version>
</dependency>
<dependency>
    <groupId>com.sun.mail</groupId>
    <artifactId>javax.mail</artifactId>
    <version>1.5.3</version>
</dependency>

2. Specific Code

/**
 * E-mail tools
 * / 
Public  Final  class mailutils {
     Private  static  Final String the USER = "[email protected]"; // sender's name, e-mail address with 
    Private  static  Final String PASSWORD = "xxxxxxx"; // If the customer can use the mailbox qq end authorization code, or password

    /**
     *
     * @Param to the recipient's mailbox
     * @Param text message body
     * @Param title title
      * / 
    / * verification email message * / 
    public  static  Boolean the sendMail (String to, text String, String title) {
         the try {
             Final the Properties The props = new new the Properties ();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.host", "smtp.qq.com");

            // sender's account 
            props.put ( "mail.user" , the USER);
             // sender's password 
            props.put ( "mail.password" , PASSWORD);

            // build authorization information, SMTP authentication for 
            the Authenticator Authenticator = new new the Authenticator () {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    // 用户名、密码
                    String userName = props.getProperty("mail.user");
                    String password = props.getProperty("mail.password");
                    return new PasswordAuthentication(userName, password);
                }
            };
            // environment attribute and authorization information, create a mail session 
            the Session mailSession = Session.getInstance (props, Authenticator);
             // create a mail message 
            the MimeMessage the Message = new new the MimeMessage (mailSession);
             // set the sender 
            String username = props.getProperty ( "mail.user" );
            InternetAddress form = new InternetAddress(username);
            message.setFrom(form);

            // set the recipient 
            InternetAddress toAddress = new new InternetAddress (to);
            message.setRecipient(Message.RecipientType.TO, toAddress);

            // set the message header 
            message.setSubject (title);

            // set the content of the message body 
            message.setContent (text, "text / HTML; charset = UTF-. 8" );
             // send a message 
            the Transport.send (Message);
             return  to true ;
        }catch (Exception e){
            e.printStackTrace ();
        }
        return false;
    }

    public  static  void main (String [] args) throws Exception { // do test 
        MailUtils.sendMail ( "[email protected]", "This is a test message", "test message" );
    }
}

3. Get email authorization code

3.1 163 E-mail authorization code

Open the Settings

 

Open client authorization code and set authorization code

 

 

 3.2 qq-mail authorization code

Click Settings -> Accounts -> turn on service -> Get authorization code

 

 

 

 4. Test results

Guess you like

Origin www.cnblogs.com/Code-Handling/p/12123359.html