1024programmer Java Example of sending emails in java

Example of sending emails in java

The example in this article explains the detailed process of sending emails in Java for your reference. The specific content is as follows

1. Email protocol

Sending email: SMTP (Simple Mail Transport Protocol)

The protocol for receiving emails: pop3 (Post Office Protocol 3)

IMAP new protocol allows you to send and receive emails.

(Step-by-step interaction with the server)


2. The process of sending and receiving emails:

Generally, smtp and pop3 are two servers (hosts).

The port for Smtp mail is 25.

POP3 port is 110.

Example of sending an email

1), install foxmail:

2) When sending an email, the user name and password must be base64 encoded

  //base64 encode the username and password


  public void base64(){
  String name = "wj_leaf12345";
  String pwd = "1qaz2wsx";
  BASE64Encoder en = new BASE64Encoder();
  name = en.encode(name.getBytes());
  pwd = en.encode(pwd.getBytes());



3), send emails through java code

To send emails using java, you must import a new package

mail.jar – core package for sending emails

activation.jar – Encrypt user and password.

There are three core classes in mail.jar:

Javax.mail.Session – refers to the session with the mail server. Just one is enough for the entire project.

Javax.mail.Message (interface) – Prepare to send data messages.

MimeMessage – You can set the type of data information.

Transport – It has a method to send Message.

Step one: Import two jar packages

Step 2: Send a simple email

 public void sendMail() throws Exception{

  //The first step: declare the properties object to put information
  Properties prop = new Properties();
  //Set which server to connect to
  //Set whether to verify
  prop.setProperty("mail.smtp.auth", "true");
  //Step 2: Declare username and password
  Authenticator auth = new Authenticator() {
   //This request returns the object of user and password
   public PasswordAuthentication getPasswordAuthentication() {
   PasswordAuthentication pa =
    new PasswordAuthentication("aaa", "sss");
   return pa;



  ////Step 2: Get the Session object
  Session session =
  //Set the debugging mode of the session
  //Step 3: Declaration information
  MimeMessage mm1 =
   new MimeMessage(session);

  //Step 4: Set the sender email
  Address from = new InternetAddress("[email protected]");
  //Step 5: Set recipients
  mm1.setRecipient(RecipientType.TO,new InternetAddress("[email protected]"));
  mm1.setRecipient(RecipientType.CC, new InternetAddress("[email protected]"));
  mm1.setRecipient(RecipientType.BCC, new InternetAddress("[email protected]"));
  //Step 6: Set the theme
  mm1.setSubject("This is an email sent in Java 3");
  mm1.setContent("Hello, this is an email sent using java, 3333 try again", "text/plain;charset=UTF-8");
  //Step 7:




Step 3: Send an email with a hyperlink

 mm1.setSubject("This is an email sfasdf3 sent in Java");

  mm1.setContent("Hello, this is an email sent in java, Baidu", "text/html;charset=UTF-8" );
  //Step 7:



Step 4: Email of the symbol

 public void sendFile() throws Exception{

  Properties p = new Properties();
  Session s = Session.getDefaultInstance(p,new Authenticator() {
   public PasswordAuthentication getPasswordAuthentication() {
   return new PasswordAuthentication("ww", "123");



  //Declare MimeMessage
  MimeMessage msg = new MimeMessage(s);
  msg.setFrom(new InternetAddress("[email protected]"));
  msg.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
  //Step 1: Declare the multiprocessing Part
  MimeMultipart mm = new MimeMultipart();
  //Step 2: Statement
  MimeBodyPart body1 = new MimeBodyPart();
  //Step 3: Set symbols
  DataSource ds = new FileDataSource(new File("./img/a.jpg"));
  DataHandler dh = new DataHandler(ds);
  //Must set name
  MimeBodyPart body2 = new MimeBodyPart();
  //Step 3: Set symbols
  DataSource ds2 = new FileDataSource(new File("./img/b.jpg"));
  DataHandler dh2 = new DataHandler(ds2);
  //Must set name
  body2.setFileName(MimeUtility.encodeText("Beauty 2.jpg"));
  MimeBodyPart body3 = new MimeBodyPart();
  //Step 3: Set symbols
  DataSource ds3 = new FileDataSource(new File("./img/m.mp3"));
  DataHandler dh3 = new DataHandler(ds3);
  //Must set name
  body3.setFileName(MimeUtility.encodeText("End of the Century.mp3"));
  //Add body1 to mm


The above is the entire content of this article, I hope it will be helpful to everyone’s study.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/703501

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us


Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top