Tool class for sending mail in java

Share with you a very useful tool class for sending mail, which is also a tool class I have been using.
1. source code

public class MailUtils {
	public static void sendMail(String email, String emailMsg)
			throws AddressException, MessagingException {
		// 1. Create a program and mail server session object Session
		Properties props = new Properties();
		props.setProperty("mail.transport.protocol", "SMTP");
		props.setProperty("mail.host", "smtp.163.com");
		props.setProperty("mail.smtp.auth", "true");// Specify validation as true
		// Create a validator
		Authenticator auth = new Authenticator() {
			public PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication("Your 163 mailbox account@163.com", "Authorization code");
			}
		};
		Session session = Session.getInstance(props, auth);
		// 2. Create a Message, which is equivalent to mail content
		Message message = new MimeMessage(session);
		message.setFrom(new InternetAddress("Your 163 mailbox account@163.com")); // Setting sender
		message.setRecipient(RecipientType.TO, new InternetAddress(email)); // Setting the sending mode and receiver
		message.setSubject("User activation");//Sending Title Information
		message.setContent(emailMsg, "text/html;charset=utf-8");
		// 3. Create Transport to send mail
		Transport.send(message);
	}

	public static void main(String[] args) throws MessagingException {
		MailUtils.sendMail("You want to send it to that user@qq.com","What you want to send");
	}
}

maven coordinates

		<!--Dependence on email-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
       	</dependency>
        <!--Dependence on email-->

2. Notes
This is implemented in the springboot environment. If you want to introduce the jar package, you can go and laugh.

I use 163 mailbox as a server to send mail to users, here is a key point to introduce:!!!
"Authorization Code". First of all, we use our 163 mailbox as a similar server to send mail to others. It is not recommended to write our own mailbox password directly in the code, so we need the authorization code provided by 163 mailbox to send mail instead of our password. Here's how to get the authorization code.

First login to your 163 mailbox



According to the prompt, you can set your own authorization code, you can use this tool class very well. When calling, the first parameter is the target mailbox address, and the second is the message sent. As you can see, I'm lazy here and write the main method directly in the tool class. The real application is not to use this way.
3. Use screenshots

In this way, you can send email. When the user registers and the password is changed, you can send the verification code to check in the past.
4. Hope it will be useful to you. If you have any questions, you can leave a message for us to communicate with each other.

Keywords: Session Maven Spring SpringBoot

Added by algy on Thu, 03 Oct 2019 22:46:14 +0300