Untitled

mail@pastecode.io avatar
unknown
java
2 years ago
3.0 kB
4
Indexable
Never
package com.muhkanda.google.mail.web;

import com.sun.mail.smtp.SMTPTransport;
import com.sun.mail.util.BASE64EncoderStream;
import java.io.Serializable;
import java.util.Properties;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Muh Kanda <kanda.wibawa@daksa.co.id>
 */
@Named
@RequestScoped
public class MailBacking implements Serializable {
    
    private static final long serialVersionUID = 1L;
    private final static Logger LOG = LoggerFactory.getLogger(MailBacking.class);
    
    private static final String SMTP_SERVER_HOST = "smtp.gmail.com";
    private static final String SMTP_SERVER_PORT = "587";
    private static final String SUBJECT = "Hello Ini BOT";
    private static final String BODY = "Hi,<br>Email ini dikirim menggunakan code program!";

    public void send() {
        LOG.info("### Send Clicked ###");
        final String FROM_USER_EMAIL = "email_pengirim@gmail.com"; // Email pengirim harus mendaftarkan web di google dev console untuk generate token.
        final String FROM_USER_FULLNAME = "Nama Pengirim";
        final String FROM_USER_ACCESSTOKEN = "AksesTokennya";
        final String TO_USER_EMAIL = "email_penerima@gmail.com";
        sendMail(SMTP_SERVER_HOST, SMTP_SERVER_PORT, FROM_USER_EMAIL, FROM_USER_ACCESSTOKEN, FROM_USER_EMAIL, FROM_USER_FULLNAME, TO_USER_EMAIL, SUBJECT, BODY);
    }
    
    public void sendMail(String smtpServerHost, String smtpServerPort,  String smtpUserName, String smtpUserAccessToken, String fromUserEmail, String fromUserFullName, String toEmail, String subject, String body) {
        try {
            Properties props = System.getProperties();
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.port", smtpServerPort);
            props.put("mail.smtp.starttls.enable", "true");

            Session session = Session.getDefaultInstance(props);
            session.setDebug(true);

            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(fromUserEmail, fromUserFullName));
            msg.setRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
            msg.setSubject(subject);
            msg.setContent(body, "text/html");

            SMTPTransport transport = new SMTPTransport(session, null);
            transport.connect(smtpServerHost, smtpUserName, null);
            transport.issueCommand("AUTH XOAUTH2 " + new String(BASE64EncoderStream.encode(String.format("user=%s\1auth=Bearer %s\1\1", smtpUserName, smtpUserAccessToken).getBytes())), 235);
            transport.sendMessage(msg, msg.getAllRecipients());
        } catch (Exception ex) {
            LOG.error("Error : " + ex.getMessage(), ex);
        }
    }
    
}