net.kamhon.ieagle.function.email.SendMail.java Source code

Java tutorial

Introduction

Here is the source code for net.kamhon.ieagle.function.email.SendMail.java

Source

/*
 * Copyright 2012 Eng Kam Hon (kamhon@gmail.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.kamhon.ieagle.function.email;

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;

import net.kamhon.ieagle.exception.SystemErrorException;
import net.kamhon.ieagle.function.email.vo.Emailq;

import org.apache.commons.lang.StringUtils;

public class SendMail {
    private Properties props = System.getProperties();

    private EmailServerSetting emailServerSetting;

    public void send(String to, String cc, String bcc, String from, String subject, String text, String emailType) {

        // Session session = Session.getInstance(props, null);
        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(emailServerSetting.getUsername(),
                        emailServerSetting.getPassword());
            }
        });

        session.setDebug(emailServerSetting.isDebug());

        try {
            Message msg = new MimeMessage(session);
            if (StringUtils.isNotBlank(from)) {
                msg.setFrom(new InternetAddress(from));
            } else {
                msg.setFrom();
            }

            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
            if (StringUtils.isNotBlank(cc)) {
                msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
            }
            if (StringUtils.isNotBlank(bcc)) {
                msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
            }

            msg.setSubject(subject);
            if (Emailq.TYPE_HTML.equalsIgnoreCase(emailType)) {
                msg.setContent(text, "text/html");
            } else {
                msg.setText(text);
            }
            msg.setSentDate(new java.util.Date());
            Transport.send(msg);
        } catch (MessagingException mex) {
            mex.printStackTrace();
            Exception ex = null;
            if ((ex = mex.getNextException()) != null) {
                ex.printStackTrace();
            }
            throw new SystemErrorException(mex);
        }
    }

    public void send(String to, String from, String subject, String text) {
        send(to, "", "", from, subject, text, Emailq.TYPE_TEXT);
    }

    public SendMail() {
    }

    public int getMaxSendRetry() {
        return emailServerSetting.getMaxSendRetry();
    }

    // ---------------- GETTER & SETTER (START) -------------

    public void setEmailServerSetting(EmailServerSetting emailServerSetting) {
        this.emailServerSetting = emailServerSetting;

        props.put("mail.smtp.host", emailServerSetting.getSmtpHost());
        props.put("mail.smtp.port", emailServerSetting.getSmtpPort());
        props.put("mail.smtp.auth", "true");
    }

    // ---------------- GETTER & SETTER (END) ---------------

    public static void main(String[] args) throws Exception {
        if (args.length != 2) {
            System.out.println("Usage: java mail.SendMail <to> <from>");
            System.exit(1);
        }

        String to = args[0];
        String from = args[1];
        String subject = "SMTP testing";
        String text = "some message text for sending email.\n";

        SendMail mail = new SendMail();
        mail.send(to, from, subject, text);
    }
}