Email is in daily life. This article mainly introduces Java's method of sending emails based on SMTP. It analyzes the relevant skills of Java's sending emails based on SMTP service. It has certain reference value. Friends who need it can refer to it.
1. Write a MailSendProper class to encapsulate the required properties
import java.util.Properties;public class MailSendProper { private String Host; // ip private String Port of the sending mail server; // port number of the sending mail server private String SendAddress; // mail sender's address private String ReceiveAddress; // mail recipient's address private String username; // log in to sending mailbox username; // log in to sending mailbox password private boolean isvalidate = true; // Whether authentication is required private String subject; // email title private String content ; //Email content public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getHost() { return Host; } public void setHost(String host) { Host = host; } public String getPort() { return Port; } public void setPort(String port) { Port = port; } public String getSendAddress() { return SendAddress; } public void setSendAddress(String sendAddress) { SendAddress = sendAddress; } public String getReceiveAddress() { return ReceiveAddress; } public void setReceiveAddress(String receiveAddress) { ReceiveAddress = receiveAddress; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public boolean isIsvalidate() { return isvalidate; } public void setIsvalidate(boolean isvalidate) { this.isvalidate = isvalidate; } public Properties getProperties(){ Properties properties = new Properties() ; properties.put("mail.smtp.host", this.Host) ; properties.put("mail.smtp.port", this.Port) ; properties.put("mail.smtp.auth", isvalidate?"true":"false") ; return properties ; }}2. Write an EmailMessage to encapsulate the sending information
public class EmailMessage { private String title ; private String context ; private String toEmail ; public EmailMessage() { super(); } public EmailMessage(String title, String context, String toEmail) { super(); this.title = title; this.context = context; this.toEmail = toEmail; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String getToEmail() { return toEmail; } public void setToEmail(String toEmail) { this.toEmail = toEmail; }}3. Write a MailAttorney Email Password Verifier Class
import javax.mail.Authenticator;import javax.mail.PasswordAuthentication;public class MailAttorney extends Authenticator { private String username ; private String password ; public MailAttorney(String username,String password) { this.username = username ; this.password = password ; } //Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username,password); }}4. Write a MailSend mail tool class
import java.util.Date;import javax.mail.Address;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;import com.VTBBS.entity.EmailMessage;public class MailSend { public static boolean mailTest(MailSendProper mailsender){ MailAttorney attorney = null; if(mailsender.isIsvalidate()){//Determine whether identity authentication is required attorney = new MailAttorney(mailsender.getUsername(),mailsender.getPassword())); } //Construct a viewion session session for sending mailbox based on the mailbox session attributes and password validator. Session session = Session.getInstance(mailsender.getProperties(),attorney); //Create a mail message based on the session Message mailMessage = new MimeMessage(session) ; try { //Create the address of the sender of the mailbox Address from = new InternetAddress(mailsender.getSendAddress()); //Set the sender of the mailbox message mailMessage.setFrom(from); //Create the address to receive this from the mail message and set it to the mail message Address to = new InternetAddress(mailsender.getReceiveAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailsender.getSubject()); //Set the mail title mailMessage.setSentDate(new Date()); //Set the sending time mailMessage.setText(mailsender.getContent()); //Set the mail content Transport.send(mailMessage); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public static boolean sendEmail(EmailMessage message){ MailSendProper mail = new MailSendProper() ; mail.setHost("smtp.126.com"); //Smtp's simple mail transmission protocol, the default port number is 25, mail.setPort("25"); mail.setIsvalidate(true); //Require authentication mail.setUsername(""); //Set the login username mail.setPassword(""); //Set the sender's password mail.setSendAddress(""); //Set the address of sending this sender and the login username are the same mail.setReceiveAddress(message.getToEmail()); //Set the recipient's address mail.setSubject(message.getTitle()); //Set the mailbox title mail.setContent(message.getContext()); //Set the content of the mailbox return mailTest(mail); }}Note: The email address used must be enabled for POP3/SMTP service to be sent successfully. Different email addresses and email addresses have different transmission protocols, such as:
QQ Email: SMTP transmission protocol is smtp.qq.com port 25
The POP3 transmission protocol is pop3.qq.com port 110
5. Use method test
public static void main(String[] args) { EmailMessage message = new EmailMessage() ; String code = String.valueOf(Math.random()).substring(3, 9) ; //Generate verification code message.setTitle("Email Verification"); //Email title message.setContext("Hello, Dear user, your verification code is "+code+"."); //Email content message.setToEmail("[email protected]"); //Who to send to System.out.println(MailSend.sendEmail(message)? "SendSuccess": "SendFailed");}I hope this article will be helpful to everyone to learn Java programming.