Problem description
when we register a website or use a software to download, we need to register the function. When we register, the official application provider will send us an email to us. When we click the email, the activation is successful and the registration is completed, then we can have the related functions of login and download. Therefore, it is very important to use java to realize mail sending. This paper realizes the function of sending mail to the mailbox through Java.
Concrete implementation
this paper takes QQ mailbox as a case, and other mailboxes are similar. We need to log in to our before we write code QQ email , and then set it to obtain the POP3/SMTP service. It is turned off by default and needs to be turned on manually. The specific steps are: Set - > account - > POP3 / imap / SMTP / exchange / carddav / caldav Service - > POP3/SMTP service. The specific operation process is as follows:
here, we need to send a short message through our mobile phone number according to the prompt, so as to generate the corresponding activation code. The specific operations are as follows:
the content we sent by mobile phone is as follows:
after the SMS is sent successfully, click the I have sent button to generate the corresponding activation code. This activation code is to be filled in our code, so it is recommended to copy it instead of handwritten. If you write wrong, you can't get the mailbox. The details are as follows:
code implementation
import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.util.Properties; /** * Email tool class */ public final class MailUtils { private static final String USER = "Fill in your own email"; // Sender's title, same as email address private static final String PASSWORD = "Fill in the authorization code just obtained"; // Enables the client authorization code /** * * @param to Recipient mailbox * @param text Message body * @param title title */ /* Send verification message */ public static boolean sendMail(String to, String text, String title){ try { final Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.host", "smtp.qq.com"); // Sender's account number props.put("mail.user", USER); //Sender's password props.put("mail.password", PASSWORD); // Build authorization information for SMTP authentication Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // User name and password String userName = props.getProperty("mail.user"); String password = props.getProperty("mail.password"); return new PasswordAuthentication(userName, password); } }; // Create a mail session using environment properties and authorization information Session mailSession = Session.getInstance(props, authenticator); // Create mail message MimeMessage message = new MimeMessage(mailSession); // Set sender String username = props.getProperty("mail.user"); InternetAddress form = new InternetAddress(username); message.setFrom(form); // Set recipient InternetAddress toAddress = new InternetAddress(to); message.setRecipient(Message.RecipientType.TO, toAddress); // Set message header message.setSubject(title); // Set the content body of the message message.setContent(text, "text/html;charset=UTF-8"); // Send mail Transport.send(message); return true; }catch (Exception e){ e.printStackTrace(); } return false; } public static void main(String[] args) throws Exception { // For testing MailUtils.sendMail("Fill in your own email","Hello, this is a test email. There is no need to reply.","Test mail"); System.out.println("Sent successfully"); } }
what needs special attention here is: I hope readers can study the code and don't paste it directly. The code can't run directly and will report errors, which needs special attention. You need to fill in your own email and corresponding authorization code. After the code is run, the effect of IDEA is as follows:
this means that we have successfully sent the code, and the code is correct!!! Let's look at the mailbox to see if we have received the mail:
it seems that we have succeeded. After receiving the email, we open one of the emails, and we check the contents. We have a feeling of deja vu:
summary
sometimes when we encounter some interesting things during project development or learning, we can record them. First, we can provide some help when other children need it, and second, we can view our own articles. When we need it, it is equivalent to a notebook. Come on, people who work hard every day will be the most handsome. There must be. Come on, symbols!!! The future can be expected!!!!!!!!!!!!! Today is the global finals of 2021S11 hero League. I hope EDG can break through the encirclement and win the championship perfectly!!! When the silver dragon is recast, when the Knight Returns. Come on EDG, come on LPL. I set up a Flag. If EDG wins, I will unload the hero League, work hard every day and pay attention to rest!!!