Some time ago, I used the alarm information of mail sending monitoring in my work. Today, I will record the mail tool class of JavaMail here.
The following is the pom dependency of the JavaMail jar package used. The version 1.4.6 of JavaMail is used here.
<dependency> <groupId>javax.mail</groupId> <artifactId>mail</artifactId> <version>1.4.6</version> </dependency>
Here is the entity class of some parameter objects of the mail used in the tool class.
package com.juihai.entity; import java.util.Arrays; public class EmailTO { private String title;//Mail subject private String content;//Mail content private String sender;//Sender account private String password;//Sender password private String host;//Outgoing email server private String[] toAdress;//Recipient address public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getSender() { return sender; } public void setSender (String sender) { this .sender = sender; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String[] getToAdress() { return toAdress; } public void setToAdress(String[] toAdress) { this.toAdress = toAdress; } @Override public String toString() { return "EmailTO [title=" + title + ", content=" + content + ", sender=" + sender + ", password=" + password + ", host=" + host + ", toAdress=" + Arrays.toString(toAdress) + "]"; } }
Here is the tool class for sending Email. Specify comments in the code of the piece.
import java.io.UnsupportedEncodingException; import java.security.GeneralSecurityException; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.log4j.Logger; import com.juihai.entity.EmailTO; public class SendEmailUtils { / **日志 输出* / private static Logger log = Logger.getLogger (SendEmailUtils. class ); public static boolean send(EmailTO to) throws GeneralSecurityException{ final String user = to.getSender(); final String password = to.getPassword(); log.info("SendEmailUtils >>>> Start Send Mail"); Properties properties = System.getProperties(); // Get system properties properties.setProperty("mail.smtp.host", to.getHost()); // Set the mail server "smtp.163.com", this is the sender person's mailbox server properties.put("mail.smtp.auth", "true" ); String senderNick = null ;
//Some mailbox servers need to enable SSL verification before they can send emails. If necessary, enable comments./* MailSSLSocketFactory sf = new MailSSLSocketFactory();// SSL verification begin sf.setTrustAllHosts(true); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.ssl.socketFactory", sf);// SSL验证 end*/ Session session = Session.getDefaultInstance(properties, new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); // sender email username, password } }); try { MimeMessage message = new MimeMessage(session); // Create a default MimeMessage object senderNick = javax.mail.internet.MimeUtility.encodeText("Juihai" ); //Set the sender nickname message.setFrom( new InternetAddress(senderNick+"< "+user+">" )); // message.addRecipient(Message.RecipientType.TO,new InternetAddress()); // Create a single recipient String[] tos = to.getToAdress(); // Create multiple recipients if (tos != null && tos.length != 0 ) { InternetAddress[] toAddress = new InternetAddress[tos.length]; for (int i = 0; i < tos.length; i++) { toAddress[i] = new InternetAddress(tos[i]); } message.setRecipients(Message.RecipientType.TO, toAddress); } message.setSubject(to.getTitle()); // Set the email subject // message.setText(content); // Send plain text content message.setContent(to.getContent(), "text/html;charset=utf- 8"); // Send html mail content Transport.send(message); // Send Email log.info("SendEmailUtils mail sent successfully" ); return true ; }catch (MessagingException mex) { mex.printStackTrace(); log.info( " >>>>SendEmailUtils failed to send mail"+ mex); } catch (UnsupportedEncodingException e) { log.info( " >>>>SendEmailUtils failed to send mail - set sender nickname error"+ e); e.printStackTrace (); } return false; } }