Java mail USES qq mailbox to send mail configuration method

  • 2020-04-01 02:42:48
  • OfStack

Program entry:
Test_Email_N. Java


import java.io.IOException;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test_Email_N {
    public static void  main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1219999@qq.com";
        String subject = "subject";
        String content = "content";
        Properties properties = new Properties();
        properties.put("mail.smtp.host", "smtp.qq.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator("1219999@qq.com", "password");
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress("1219999@qq.com"));
        //Message. RecipientType. Said TO attribute of type TO the receiver
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        //The MiniMultipart class is a container class that contains objects of type MimeBodyPart
        Multipart mainPart = new MimeMultipart();
        //Create a MimeBodyPart that contains HTML content
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

Which depend on the jar package for javax.mail. Mail, I here is the administration of maven directly use maven to download jars, also can download the jar package to https://java.net/projects/javamail/pages/Home directly.


<dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>


Email_authenticator.java, where the Authenticator class is inherited to encapsulate the name, and the password:


package com.infomorrow.webtest.JuxinliTest.restdetect;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Authenticator extends Authenticator {
    String userName = null;
    String password = null;
    public Email_Authenticator() {
    }
    public Email_Authenticator(String username, String password) {
        this.userName = username;
        this.password = password;
    }
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password);
    }
}

Configuration so much, the mailbox password to their own can be changed, or you will report an error. The program is ready to run!

Here's how to configure the properties file to manage the password:

Create a new email.propertis file.

Email. Propertis:


mail.smtp.host=smtp.qq.com
mail.smtp.port=25
username=1219999@qq.com
password=password

The test_email.java code is changed to read:


package com.infomorrow.webtest.JuxinliTest.restdetect;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class Test_Email {
  public static void main(String args[]){
        try {
            send_email();
        }catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void send_email() throws IOException, AddressException, MessagingException{

        String to = "1215186706@qq.com";
        String subject = "subject";//Email subject
        String content = "content";//Email content
        Properties properties = new Properties();
        InputStream resourceAsStream = null;
        try {
             resourceAsStream = Object.class.getResourceAsStream("/email.properties");
            properties.load(resourceAsStream);
        } finally{
            if (resourceAsStream!=null) {
                resourceAsStream.close();
            }
        }
        System.err.println("properties:"+properties);
        properties.put("mail.smtp.host", properties.get("mail.smtp.host"));
        properties.put("mail.smtp.port", properties.get("mail.smtp.port"));
        properties.put("mail.smtp.auth", "true");
        Authenticator authenticator = new Email_Authenticator(properties.get("username").toString(), properties.get("password").toString());
        javax.mail.Session sendMailSession = javax.mail.Session.getDefaultInstance(properties, authenticator);
        MimeMessage mailMessage = new MimeMessage(sendMailSession);
        mailMessage.setFrom(new InternetAddress(properties.get("username").toString()));
        //Message. RecipientType. Said TO attribute of type TO the receiver
        mailMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
        mailMessage.setSubject(subject, "UTF-8");
        mailMessage.setSentDate(new Date());
        //The MiniMultipart class is a container class that contains objects of type MimeBodyPart
        Multipart mainPart = new MimeMultipart();
        //Create a MimeBodyPart that contains HTML content
        BodyPart html = new MimeBodyPart();
        html.setContent(content.trim(), "text/html; charset=utf-8");
        mainPart.addBodyPart(html);
        mailMessage.setContent(mainPart);
        Transport.send(mailMessage);
    }
}

Ok, that's it.


Related articles: