Java USES JavaMail to send mail

  • 2020-05-07 19:51:14
  • OfStack

This article illustrates how Java USES JavaMail to send messages. Share with you for your reference, as follows:

Code 1. Email_Autherticator.java server validation code


import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
  String username = " The username of your mailbox ";
  String password = " The password to your email ";
  public Email_Autherticator() {
 super();
  }
  public Email_Autherticator(String user,String pwd){
 super();
 username = user;
 password = pwd;
  }
  public PasswordAuthentication getPasswordAuthentication(){
 return new PasswordAuthentication(username,password);
  }
}

Code 2. Mail


import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
  private String host = "smtp.126.com";
  private String mail_head_name = "this is head of this mail";
  private String mail_head_value = "this is head of this mail";
  private String mail_to = "254173774@qq.com";
  private String mail_from = "hackboyo@126.com";
  private String mail_subject = "this is the subject of this test mail";
  private String mail_body = "this is mail_body of this test mail";
  private String personalName = " My email ";
  public void sendMail() throws SendFailedException{
 try {
   Properties props = new Properties();// Acquiring system environment 
   Authenticator auth = new Email_Autherticator();// Authentication of mail service users 
   props.put("mail.smtp.host", host);
   props.put("mail.smtp.auth", "true");
   System.out.println(props);
   Session session = Session.getDefaultInstance(props,auth);
   // Set up the session, Communicate with the mail server 
   MimeMessage message = new MimeMessage(session);
   message.setContent("Hello","text/plain");// Format your message 
   message.setSubject(mail_subject);// Set email subject 
   message.setText(mail_body);// Set email content 
   message.setHeader(mail_head_name, mail_head_value);// Set the subject line 
   message.setSentDate(new Date());// Set a time for the message to be sent 
   Address address = new InternetAddress(mail_from,personalName);
   message.setFrom(address);// Set the sender's address 
   Address toaddress = new InternetAddress(mail_to);// Set the address of the recipient 
   message.addRecipient(Message.RecipientType.TO,toaddress);
   System.out.println(message);
   Transport.send(message);
   System.out.println("Send Mail Ok!");
 } catch (Exception e) {
   e.printStackTrace();
 }
 //return flag;
  }
}

Code 3. Test.java tests the code to send the message


public class Test {
  public static void main(String[] args) {
 Mail m = new Mail();
 try {
   m.sendMail();
 } catch (Exception e) {
 }
  }
}

I hope that this article has been helpful to you in Java programming.


Related articles: