This is the fifth day of my participation in the Gwen Challenge.More article challenges

1. POP3, SMTP and IMAP

1.1 What is POP3

POP3 is short for Post Office Protocol 3, version 3 of the Post Office Protocol, which specifies how to connect a personal computer to a mail server on the Internet and an electronic Protocol for downloading E-mail. It was the first offline protocol standard for Internet E-mail. POP3 allowed users to store mail from a server to a local host (that is, their own computer) while deleting the mail stored on the mail server, which was a POP3 protocol receiving mail server used to receive E-mail. (How is it different from IMAP?)

1.2 What is SMTP

SMTP is the Simple Mail Transfer Protocol. It is a set of specifications used to transport messages from source addresses to destination addresses to control how messages are forwarded. SMTP belongs to the TCP/IP protocol family, which helps each computer find the next destination when sending or forwarding a letter. The SMTP server is a mail sending server that complies with THE SMTP protocol.

SMTP authentication simply means that you must provide an account name and password to log in to an SMTP server, which makes it impossible for spammers to spread spam.

SMTP authentication is added to protect users from spam.

1.3 What is IMAP

IMAP stands for Internet Mail Access Protocol, or INTERACTIVE Mail Access Protocol. It is a standard Mail Access Protocol similar to POP3. However, after IMAP is enabled, the emails you receive from the email client are still stored on the server, and all the operations on the client are reported to the server, such as deleting emails or marking the emails as read. The emails on the server will also take corresponding actions. Therefore, whether you log in to the mailbox from the browser or the client software, the mail and status are the same. (How is it different from POP3?)

Ii. Server information related to netease 163 Free mailbox

2.1 163 POP3, SMTP, and IMAP addresses set on the free Mail client

2.2 163 Mailbox Configuration

The POP3, SMTP, and IMAP services are enabled on the netease mailbox by default, so that you can better receive and send emails through the PC client software.

If disabled, you can enable it in the following ways:

  1. Please log on to email 163,
  2. Click “Settings” at the top of the page,
  3. Click “POP3/SMTP/IMAP” on the left,
  4. Enable SMTP service is selected by default.

You can check the other two options in the picture and click OK to enable it successfully. Uncheck the two options in the figure and click OK.

Let’s start coding.

Iii. 163 Mailbox implementation

3.1 Development Environment

JavaMail version:1.6. 0JDK version: JDK1.8IDEA This test is based on163emailCopy the code

3.2 Introducing dependencies required by mail

<! -- Dependency > <groupId >javax.mail </groupId > <artifactId > <version >1.4.5 </version > </dependency > <dependency > <groupId >com.sun.mail </groupId > <artifactId >javax.mail </artifactId > <version >1.5.4 </version > </dependency >Copy the code

3.3 write EmailUtils


package pro.demo.SpringBootdemo.util;

import java.util.Date;
import java.util.Properties;

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

 /* * This test is based on 163 mailbox */
public class EmailUtil  {
	//发件人邮箱 (开通 POP3/SMTP/IMAP服务的邮箱)
	private static final String myEmailAccount = "******@163.com";
	//POP3/SMTP/IMAP client authorization password or email password
	private static final String myEmailPassword = "* * * * * *";
   
	/** * One to one email **@param FromName Indicates the sender's name *@param ReceiveMailAccount Recipient email address *@param Title Mail title *@param Content Email content * *@return True Successful false failed * */
    public static boolean sendEmail(String fromName, String receiveMailAccount, String title, String content){
    	// 1. Create parameters for connecting to the mail server
        Properties props = new Properties();                    // Parameter configuration
        props.setProperty("mail.transport.protocol"."smtp");   // The protocol to use (required by the JavaMail specification)
        props.setProperty("mail.smtp.host"."smtp.163.com");   	// The SMTP server address of the sender's email box 163 is fixed to smtp.163.com
        props.setProperty("mail.smtp.auth"."true");            // Request authentication
        props.setProperty("mail.smtp.port"."25");				// Email server port 163 Email SMTP the default value is 25
        try {
	        // 2. Create a session object based on the configuration to interact with the mail server
	        Session session = Session.getInstance(props);
	        session.setDebug(true);                             // Set it to debug mode to view detailed sent logs
	        
	        // 3. Create an email
	        MimeMessage message = new MimeMessage(session);
	        // 3.1 From: sender
	        message.setFrom(new InternetAddress(myEmailAccount, fromName, "UTF-8"));
	        // 3.2 To: recipient (multiple recipients, cc, BCC can be added)
	        message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiveMailAccount, ""."UTF-8"));
	        // 3.3 Subject: email Subject (Subject is suspected to be advertising, so as to avoid being mistaken by mail server as spam and return failure, please modify the Subject)
	        message.setSubject(title, "UTF-8");
	        // 3.4 Content: The body of the email (HTML tag can be used) (The Content is suspected of being advertising, so as to avoid being mistaken by the mail server as spam and fail to return, please modify the sent Content)
	        message.setContent(content, "text/html; charset=UTF-8");
	        3.5 Set the delivery time
	        message.setSentDate(new Date());
	        // 3.6. Save Settings
	        message.saveChanges();
        
	        // 4. Obtain the mail transfer object according to Session
	        Transport transport = session.getTransport();
	        
	        Use the email account and password to connect to the mail server. The authenticated email address must be the same as that of the sender in message; otherwise, an error message will be reported
	        transport.connect(myEmailAccount, myEmailPassword);
	        
	        // 6. Message.getallrecipients () was obtained during the creation of the email object, both recipients, cc recipients and recipients
	        transport.sendMessage(message, message.getAllRecipients());
	        
	        // close the connection
	        transport.close();
	        
	        return true;
        } catch (Exception e) {
        	
			return false; }}Copy the code

3.4 Debugging Email Sending

    /** * debug emailing function ** /
    public static void main(String[] args) {
    	boolean bl = sendEmail("Tester"."********@qq.com"."Test data"."Hello, this is a test email, please ignore it.");
    	if(bl){
    		System.out.println("success");	// The email was sent successfully
    	}else{
    		System.out.println("failed");	// Failed to send mail}}Copy the code

3.5 Receiving a Test Email

The screenshot below shows the received test email. Ok, a send function based on mailbox 163 is complete.

Fourth, concluding remarks

Well, this content is shared here, thank you for reading, welcome to focus on praise, learning progress together.