javaMail learning (4) - use javaMail to send simple emails to NetEase accounts

The code is similar to the previous one, just change the server address and port number of smpt.

package com.wjl.mail.utils;

import java.security.GeneralSecurityException;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 * Tools for sending emails
 * 1 6 3 mailboxes send messages to each other
 */
public class MailUtils3 {
	private static String userName = "xxxx";//1 6 3 email account for sending emails
	private static String password = "xxxx";//1 6 3 The authorization code of the mailbox, if not, use the password
	private static String userName2 = "xxxx";//1 6 3 mailbox account for receiving mail
	private static String port = "25";//Port number
	/**
	 * This method is used to send mail
	 * @param to: who to email
	 * **/
	public static void sendMain(String to) throws AddressException, MessagingException, GeneralSecurityException{
		//1. Create a connection object and connect to the mailbox server
		Properties props = new Properties();
		//Enable debug debugging  
        props.setProperty("mail.debug", "true");  
        //stmp server needs to be authenticated, that is, the verification of username and password, so that it can pass the verification
        props.setProperty("mail.smtp.auth", "true");
        // send mail protocol name  
        props.setProperty("mail.transport.protocol", "smtp");  
        //Set the mail server hostname  
        props.setProperty("mail.host", "smtp.163.com");//Set as the outgoing server of 163
        
        //Set the port number (this configuration can be written or not)
        props.setProperty("mail.smtp.port", port);
        
        //password, authorization code
        props.setProperty("mail.smtp.password",password);
		
		//Authenticator: Authentication information
		Session session = Session.getInstance(props, new Authenticator(){
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(userName,password);//Use it to send emails to other accounts
			}
		});
		
		//2, create the mail object
		Message message = new MimeMessage(session);
		//2.1 Set the sender
		message.setFrom(new InternetAddress(userName));
		//2, 2 set the recipient
		message.setRecipient(RecipientType.TO, new InternetAddress(to));
		//2.3 Subject of the email
		message.setSubject("Test message");
		//2.4 The body of the email (ie the content of the email)
		message.setContent("Test Mail:javaMail-NetEase Mailbox Test","text/html;charset=utf-8");
		
		//3. Send email
		Transport trans = session.getTransport();
		//connect to mail server
        trans.connect(userName, password);  
		//send email
        trans.sendMessage(message, message.getAllRecipients());  
        //close the connection
        trans.close();
        
        //Transport.send(message); (both ways are possible)
		System.out.println("Successfully sent");
	}
	
	public static void main(String[] args) {
		try {
			sendMain(userName2);
		} catch (Exception e) {
			e.printStackTrace ();
		}
	}
}

 

Add a NetEase email account in Foxmail to view emails.

 

Note: The smtp must be turned on.

Check if it is turned on:

Log in to NetEase mailbox through the web page---->Settings---->POP9/SMTP/IMAP---->Set POP9/SMTP/IMAP.

By default, SMTP and POP3 services are turned on. If it is closed, the execution code will report an error: javax.mail.AuthenticationFailedException: 550 User has no permission

 

It is worth mentioning that the password and authorization code of NetEase mailbox.

As mentioned above, the smtp service of NetEase mailbox is enabled by default. At this time, the password in the above code can use the password of the mailbox account .

If you manually turn off the smtp service, and then manually turn it on , NetEase will ask to set the authorization code . It is relatively simple to set the authorization code. You can set it according to the prompts (hard requirement: the authorization code can only be numbers and letters, and cannot be combined with the password). consistent). After setting, the password in the above code must use the authorization code , otherwise an error will be reported: javax.mail.AuthenticationFailedException: 535 Error: authentication failed

 

PPS: Sensitive words should be open, close, email, password, authorization code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326384200&siteId=291194637