Send mail through JAVA --mail.jar

Send email through JAVA. You need to download mail.jar. Some settings of the fourth java code are set according to the actual situation.

This example is for the google mail service: google mail: smtp.gmail.com 465 needs to enable ssl. Others can be referenced online.

 

1. MailSenderInfo

import java.util.Properties;

public class MailSenderInfo {
  private String mailServerHost;
  private String mailServerPort = "25";
   private String fromAddress;
   private String toAddress;
  private String userName;
  private String password;
  private boolean validate = false;
  private String subject;
  private String content;
  private String[] attachFileNames;
  private boolean ssl;
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  
  public Properties getProperties() {
   Properties p = new Properties();
   p.put("mail.smtp.host", this.mailServerHost);
   p.put("mail.smtp.port", this.mailServerPort);
   p.put("mail.smtp.auth", validate ? "true" : "false");
   if(ssl) p.put("mail.smtp.socketFactory.class", SSL_FACTORY);
   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;
  }

  public boolean isSsl() {
   return ssl;
  }

  public void setSsl(boolean ssl) {
   this.ssl = ssl;
  }
}

 

2. MyAuthenticator

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

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);
 }
}

 

3.SimpleMailSender

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SimpleMailSender {  /** */  /**   * Send mail in text format   *   * @param mailInfo   * Information about the mail to be sent   */  public static boolean sendTextMail(MailSenderInfo mailInfo) {   // Determine whether authentication is required   MyAuthenticator authenticator = null;   Properties pro = mailInfo.getProperties();   if (mailInfo.isValidate()) {    // If authentication is required, create a password authenticator    authenticator = new MyAuthenticator(mailInfo.getUserName(),      mailInfo.getPassword() );   }   // Construct a session to send mail according to the mail session attribute and password authenticator Session   sendMailSession = Session     .getDefaultInstance(pro, authenticator);   try {




















   // Create a mail message according to the session Message
   mailMessage = new MimeMessage(sendMailSession);
   // Create the mail sender address
   Address from = new InternetAddress(mailInfo.getFromAddress());
   // Set the sender of the mail message
   mailMessage.setFrom(from );
   // Create the recipient address of the mail and set it in 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 when the mail message is sent
   mailMessage.setSentDate(new Date());
   // Set the main content of the mail message
   String mailContent = mailInfo.getContent();
   mailMessage.setText( mailContent);
   // send mail
   Transport.send(mailMessage);
   return true;
  } catch (MessagingException ex) {    ex.printStackTrace();   }   return false;  }  /** */  /**   *   * Send mail in HTML format   *   * @param mailInfo   * pending Sent email message   */











 public static boolean sendHtmlMail(MailSenderInfo mailInfo) {   // Determine whether authentication is required   MyAuthenticator authenticator = null;   Properties pro = mailInfo.getProperties();   // If authentication is required, create a password validator   if (mailInfo.isValidate() ) {    authenticator = new MyAuthenticator(mailInfo.getUserName(),      mailInfo.getPassword());   }   // Construct a session to send mail according to the mail session attribute and password authenticator   Session sendMailSession = Session     .getDefaultInstance(pro, authenticator);   try {    // Create a mail message according to session    Message mailMessage = new MimeMessage(sendMailSession);    // Create mail sender address    Address from = new InternetAddress(mailInfo.getFromAddress());
















   // Set the sender of the mail message
   mailMessage.setFrom(from);
   // Create the recipient address of the mail and set it in the mail message
   Address to = new InternetAddress(mailInfo.getToAddress());
   // Message.RecipientType.TO The property indicates that the type of the recipient is TO
   mailMessage.setRecipient(Message.RecipientType.TO, to);
   // Set the subject of the mail message
   mailMessage.setSubject(mailInfo.getSubject());
   // Set the sending time of the mail message
   mailMessage.setSentDate (new Date());
   // The MiniMultipart class is a container class that contains objects of type MimeBodyPart
   Multipart mainPart = new MimeMultipart();
   // Create a MimeBodyPart that contains HTML content BodyPart
   html = new MimeBodyPart();
   // Set HTML Content
   html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
   mainPart.addBodyPart(html);
   // Set MiniMultipart object as mail content
   mailMessage.setContent(mainPart);
   // Send mail
   Transport.send(mailMessage);
   return true;
  } catch (MessagingException ex) {    ex.printStackTrace();   }   return false;  } }





 

4.

public class MailSenderTest {

 @SuppressWarnings("static-access")
 public static void main(String args[]) {

  MailSenderInfo mailInfo = new MailSenderInfo();
  mailInfo.setMailServerHost("smtp.gmail.com");
  mailInfo.setMailServerPort("465");
  mailInfo.setValidate(true);
  mailInfo.setSsl(true);
  mailInfo.setUserName(" username
  mailInfo.setPassword("*********");// password
  mailInfo.setFromAddress("
Sender address
  mailInfo.setToAddress("
 }
}

Guess you like

Origin blog.csdn.net/IamstudyingJava/article/details/40891869