Send emails through javamail for your reference. The specific content is as follows
Note: Some ports on the server are not open and need to open the port. Some email addresses need to enable corresponding authorization services.
1.maven dependencies:
<!-- https://mvnrepository.com/artifact/javax.mail/javax.mail-api --> <dependency > <groupId >javax.mail </groupId > <artifactId >mail </artifactId > <version >1.4.5 </version > </dependency > <dependency > <groupId >com.sun.mail </groupId > <artifactId >javax.mail </artifactId > </dependency >
2. Create a new entity class to save information
import java.util.Properties; public class MailSenderInfo { // IP (or host address) of the server sending the mail private String mailServerHost; // Port of the server sending the mail private String mailServerPort; // Sender email address private String fromAddress; // Recipient email address private String toAddress; // Username of the login email sending server private String userName; // Password of the login email sending server private String password; // Whether authentication is required private boolean validate = true; // Email subject private String subject; // The text content of the mail private String content; // The file name of the mail attachment private String[] attachFileNames; public Properties getProperties() { Properties p = new Properties(); p.put("mail.smtp.host", this.mailServerHost); p.put("mail.smtp.port", this.mailServerPort); // Set whether to verify safely, the default is false, and it is set to true in general p.put("mail.smtp.auth", "true"); p.put("mail.smtp.starttls.enable","true"); p.put("mail.smtp.EnableSSL.enable","true"); return p; } public String getMailServerHost() { return mailServerHost; } public void setMailServerHost(String mailServerHost) { this.mailServerHost = mailServerHost; } public String getMailServerPort() { return mailServerPort; } public void setMailServerPort(String mailServerPort) { this.mailServerPort = mailServerPort; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } public String[] getAttachFileNames() { return attachFileNames; } public void setAttachFileNames(String[] fileNames) { this.attachFileNames = fileNames; } public String getFromAddress() { return fromAddress; } public void setFromAddress(String fromAddress) { this.fromAddress = fromAddress; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String textContent) { this.content = textContent; } }3. Create a validator
import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * Mail username and password authenticator*/ public class MyAuthenticator extends Authenticator{ String userName = null; String password = null; public MyAuthenticator() { } public MyAuthenticator(String username, String password) { this.userName = username; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userName, password); } } 4. Assign value to entity class at the call
private void email(HttpSession session, String email) { // Set mail server information MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp-mail.outlook.com"); // The IP (or host address) of the server that sent the mail mailInfo.setMailServerPort("587"); // Some ports are not open on the server. Here you need to pay attention to mailInfo.setValidate(true); // Email username (set according to your own situation) Here you can get a few more emails to avoid the email account needing verification or being treated as spam mail ban. A fails to use B mailInfo.setUserName("Fill in the email corresponding to the email server above"); // Email Password (set according to your own situation) mailInfo.setPassword("This is your password"); // Sender Email (set according to your own situation. If you do not set the email specifically, it should be the same as the email username) mailInfo.setFromAddress("This is the same as the above"); // Recipient Email (set according to your own situation) mailInfo.setToAddress(email); // Email title mailInfo.setSubject("I am the title"); // Email content mailInfo.setContent("I am the content, the serious content is not a spam"); // Send Email SimpleMailSender sms = new SimpleMailSender(); // Send text format sms.sendTextMail(mailInfo); } 5. This is the real email sending
public class SimpleMailSender { public boolean sendTextMail(MailSenderInfo mailInfo) { // Determine whether identity authentication is required MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { // If identity authentication is required, create a password authenticator authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } // Construct a session session for sending mails based on the mail session properties and password authenticator sendMailSession = Session.getDefaultInstance(pro, authenticator); try { // Create a mail message based on session Message mailMessage = new MimeMessage(sendMailSession); // Create the sender of the mail message Address from = new InternetAddress(mailInfo.getFromAddress()); // Set the sender of the mail message mailMessage.setFrom(from); // Create the recipient address of the mail message and set it to the mail message Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); // Set the subject of the mail message mailMessage.setSubject(mailInfo.getSubject()); // Set the time for sending the mail message mailMessage.setSentDate(new Date()); // Set the main content of the mail message String mailContent = mailInfo.getContent(); mailMessage.setText(mailContent); mailMessage.saveChanges(); // Send mailTransport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.