Send qq mail using java code

First import the required jar package

Mail jar package download

Create a new test class

package cn.z;


public class Test {
	public static void main(String[] args) {   
        Mail mail = new Mail();   
        System.out.println("sending");
        mail.send();
        System.out.println("finished!");   
    }   
}

Create a new mail class

package cn.z;

import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Mail {   
    private String mailServer, from, to, mailSubject, mailContent;   
    private String username, password;   
  
    public Mail() {   
        //set email information   
        username = "your qq [email protected]";   
        password = "password"; //How to use it will be discussed later, if possible, use the qq password
        mailServer = "smtp.qq.com";   
        from = "Who sent it, usually your qq [email protected]";   
        to = "To [email protected]";
        mailSubject = "Hello";   
        mailContent = "Long time no see, how are you?";
    }   
    public void send(){   
        //Set up the mail server
        Properties prop = System.getProperties();
        prop.put("mail.smtp.host", mailServer);
        prop.put("mail.smtp.port", "25");
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.auth", "true");
        //Generate Session service
        EmailAuthenticator mailauth =   new EmailAuthenticator(username, password);
        Session mailSession = Session.getInstance(prop, (Authenticator)mailauth);
        try {   
        	//Encapsulate the Message object
            Message message = new MimeMessage(mailSession);
            message.setFrom(new InternetAddress(from)); //Set the sender
        	message.setRecipient(Message.RecipientType.TO,
        			             new InternetAddress(to));//Set the recipient   
        	message.setSubject(mailSubject);//Set the subject
        	message.setContent(mailContent, "text/html;charset=utf8");//Set the content (set the character set to deal with garbled characters)
        	message.setSentDate(new Date());//Set the date
        	//Create a Transport instance and send emails
        	Transport tran = mailSession.getTransport("smtp");   
        	tran.send(message, message.getAllRecipients());   
        	tran.close();
        } catch (Exception e) {   
            e.printStackTrace();   
        }
    }   
}   

Create a new EmailAuthenticator class

package cn.z;
import javax.mail.Authenticator;     
import javax.mail.PasswordAuthentication;
/**
 * EmailAuthenticator
 * Inherited from Authenticator
 */
public class EmailAuthenticator extends Authenticator{
	private String username = null;   
	  
    private String userpass = null;   
  
    void setUsername(String username) {   
        this.username = username;   
    }   
  
    void setUserpass(String userpass) {   
    	this.userpass = userpass;   
    }   
  
    public EmailAuthenticator(String username, String userpass) {   
        super();   
        setUsername(username);   
        setUserpass(userpass);
    }
    
    public PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(username, userpass);   
    }
}

Remember to open your mailbox POP3/SMTP service


Click on settings

account in settings

turn this on


Then send SMS and set it to turn on. After turning it on, he will give you a password. Use that password to fill in the password in the emil class above.

Guess you like

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