Gửi mail trong java sử dụng Gmail SMTP

Trong bài này chúng ta sẽ tìm hiểu sử dụng JavaMail với server Google SMTP

  • Gửi mail với GMail via TLS
  • Gửi mail với GMail via SSL

1. Các thông tin cần thiết

Outgoing Mail (SMTP) Server

requires TLS or SSL: smtp.gmail.com (use authentication)

Use Authentication: Yes

Port for TLS/STARTTLS: 587

Port for SSL: 465

Cấu hình mail server : https://support.google.com/mail/answer/7104828?hl=en&visit_id=1-636269110766936526-2292973163&rd=3

Các gói thư viện cần thêm vào:

2. Gửi mail với GMail via TLS

package com.loop.mail;

import java.util.Properties;

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;

public class SampleSendMailTLS {

  public static void main(String[] args) {

    final String username = "xxxxxxxxxxxxxx";
    final String password = "xxxxxxxxxxxxxx";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "xxxxxxxxxxxxxx");
    props.put("mail.smtp.port", "587");
    Session session = Session.getInstance(props,
    new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(username, password);
      }
    });
    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("xxxxxxxxxxxxxx"));
      message.setRecipients(Message.RecipientType.TO,
      InternetAddress.parse("xxxxxxxxxxxxxx"));
      message.setSubject("Testing Subject");
      message.setText("Dear Dinh The Hien," + "\n\n Khong duoc spam Mail!");
      Transport.send(message);
      System.out.println("Done");
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
}

3. Gửi mail với GMail via SSL

package com.loop.mail;

import java.util.Properties;

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;

/**
* Create by Cuder
*
*/
public class SampleSendMailSSL {

  public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
    "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
    new javax.mail.Authenticator() {
      protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication("xxxxxxxxxxxxxx","xxxxxxxxxxxxxx");
      }
    });
    try {
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("xxxxxxxxxxxxxx"));
      message.setRecipients(Message.RecipientType.TO,
      InternetAddress.parse("xxxxxxxxxxxxxx"));
      message.setSubject("Testing Subject");
      message.setText("Dear Dinh The Hien," + "\n\n Khong duoc spam Mail!");
      Transport.send(message);
      System.out.println("Done");
    } catch (MessagingException e) {
      throw new RuntimeException(e);
    }
  }
}

Chú ý : hiện tại Google đang chặn không cho gửi thông qua giao thức TLS

Rate this post
Click Here to Leave a Comment Below 0 comments