MailUtil.java 7.69 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
package jp.agentec.sinaburocast.common.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.seasar.framework.util.StringUtil;

/**
 * メール送信ユーティリティクラス
 *
 * @author tsukada
 *
 */
public class MailUtil {
	private static final Logger LOGGER = Logger.getLogger(MailUtil.class);

	/**
	 * メールを送信する。
	 * 引数以外はプロパティファイルから値を取得。
	 *
	 * @param to
	 * @param body
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public static String send(String to, String body) throws MessagingException,UnsupportedEncodingException {
		String smtphost = PropertyUtil.getProperty("SMTP_HOST");
		String from = PropertyUtil.getProperty("DEFAULT_MAIL_FROM_ADDRESS");
		String fromName = PropertyUtil.getProperty("DEFAULT_MAIL_FROM_NAME");
		String subject = PropertyUtil.getProperty("DEFAULT_MAIL_SUBJECT");

		return send(smtphost, from, fromName, to, subject, body);
	}

	/**
	 * メールを送信する。
	 * 引数以外はプロパティファイルから値を取得。
	 *
	 * @param to
	 * @param body
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public static String send(String to, String subject, String body) throws MessagingException,UnsupportedEncodingException {
		String smtphost = PropertyUtil.getProperty("SMTP_HOST");
		String from = PropertyUtil.getProperty("DEFAULT_MAIL_FROM_ADDRESS");
		String fromName = PropertyUtil.getProperty("DEFAULT_MAIL_FROM_NAME");

		return send(smtphost, from, fromName, to, subject, body);
	}
	public static String  send(String smtphost, String from, String fromName, String to, String subject, String body) throws MessagingException, UnsupportedEncodingException {
		String smtpAuthId = PropertyUtil.getProperty("DEFAULT_SMTP_AUTH_ID");
		String smtpAuthPass = PropertyUtil.getProperty("DEFAULT_SMTP_AUTH_PASS");

		if (StringUtils.isNotEmpty(smtpAuthId)) { // smtp authで送信
	 		return send(smtphost, from, fromName, to, subject, body, smtpAuthId, smtpAuthPass);
		}
		else {
	 		return send(smtphost, from, fromName, to, subject, body, null, null);
		}
	}

	/**
	 * メールを送信する。
	 *
	 * @param smtphost
	 * @param from
	 * @param fromName
	 * @param to
	 * @param subject
	 * @param body
	 * @param smtpAuthId smtpauthで送信しない場合はnull
	 * @param smtpAuthPass
	 * @throws MessagingException
	 * @throws UnsupportedEncodingException
	 */
	public static String  send(String smtphost, String from, String fromName, String to, String subject, String body, String smtpAuthId, String smtpAuthPass) throws MessagingException, UnsupportedEncodingException {

		String messageId = null;

		Properties props = System.getProperties();
		props.put("mail.smtp.host", smtphost);
		//choi-c
		if(!StringUtil.isEmpty(PropertyUtil.getProperty("SMTP_PORT"))){
			props.put("mail.smtp.port", PropertyUtil.getProperty("SMTP_PORT"));//587
		}
		//choi-c END
		if (smtpAuthId != null && SinaburoUtil.isNotBlankOrSpace(smtpAuthId)) {
			props.put("mail.smtp.auth","true");
		}

		Session session = Session.getDefaultInstance(props, null);
		MimeMessage mimeMessage = new MimeMessage(session);
		mimeMessage.setFrom(new InternetAddress(from, fromName, "UTF-8"));
		mimeMessage.setRecipients(Message.RecipientType.TO, to);
		mimeMessage.setSubject(subject, "UTF-8");
		mimeMessage.setText(body, "UTF-8");
		mimeMessage.setHeader("Content-Transfer-Encoding", "7bit");
		mimeMessage.setSentDate(new Date());
		mimeMessage.getMessageID();

		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("from: " + from);
			LOGGER.debug("fromName: " + fromName);
			LOGGER.debug("to: " + to);
			LOGGER.debug("subject: " + subject);
			LOGGER.debug("body:\n" + body);
		}

		// 送信する
		if (smtpAuthId == null || SinaburoUtil.isBlankOrSpace(smtpAuthId)) {
			Transport.send(mimeMessage);
			messageId = mimeMessage.getMessageID();
		}
		else { // smtpauthで送信
			Transport transport = session.getTransport("smtp");
			transport.connect(null, smtpAuthId, smtpAuthPass);
			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
			messageId = mimeMessage.getMessageID();
			transport.close();
		}
		return messageId;
	}

	/**
	 * メールを送信する。
	 */
	public static String send(String smtphost, String from, String fromName, Address[] to, Address[] bcc, String cc, String subject, String body, String smtpAuthId, String smtpAuthPass) throws MessagingException, UnsupportedEncodingException {

		String messageId = null;

		Properties props = System.getProperties();
		props.put("mail.smtp.host", smtphost);
		//choi-c
		if(!StringUtil.isEmpty(PropertyUtil.getProperty("SMTP_PORT"))){
			props.put("mail.smtp.port", PropertyUtil.getProperty("SMTP_PORT"));//587
		}
		//choi-c END
		if (smtpAuthId != null && SinaburoUtil.isNotBlankOrSpace(smtpAuthId)) {
			props.put("mail.smtp.auth","true");
		}

		Session session = Session.getDefaultInstance(props, null);
		MimeMessage mimeMessage = new MimeMessage(session);
		mimeMessage.setFrom(new InternetAddress(from, fromName, "UTF-8"));

		if (to != null && to.length > 0) {
			mimeMessage.setRecipients(Message.RecipientType.TO, to);
		} else {
			LOGGER.error("alertMail [to] is null or size 0 .......................................!!");
		}

		if (!SinaburoUtil.isBlankOrSpace(cc)) {
			mimeMessage.setRecipients(Message.RecipientType.CC, cc);
		}
		if (bcc != null && bcc.length > 0) {
			mimeMessage.setRecipients(Message.RecipientType.BCC, bcc);
		}
		mimeMessage.setSubject(subject, "UTF-8");
		mimeMessage.setText(body, "UTF-8");
		mimeMessage.setHeader("Content-Transfer-Encoding", "7bit");
		mimeMessage.setSentDate(new Date());

		if (LOGGER.isDebugEnabled()) {
			LOGGER.debug("from: " + from);
			LOGGER.debug("fromName: " + fromName);
			if (to != null) {
				String addresses = "";
				for (Address address : to) {
					addresses = addresses + address.toString() + ";";
				}
				LOGGER.debug("to: " + addresses);
			}
			LOGGER.debug("cc: " + cc);
			LOGGER.debug("bcc: " + bcc);
			LOGGER.debug("subject: " + subject);
			LOGGER.debug("body:\n" + body);
		}
		// 送信する
		if (smtpAuthId == null || SinaburoUtil.isBlankOrSpace(smtpAuthId)) {
			Transport.send(mimeMessage);
			messageId = mimeMessage.getMessageID();
		}
		else {
			// smtpauthで送信
			Transport transport = session.getTransport("smtp");
			transport.connect(null, smtpAuthId, smtpAuthPass);
			transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());
			messageId = mimeMessage.getMessageID();
			transport.close();
		}

		return messageId;
	}

	public static void main(String[] args) {
		try {
			if (args.length != 6 && args.length != 8) {
				System.out.println("usage: java -cp .;mail.jar MailUtil <smtphost> <to> <from> <fromName> <subject> <body>");
				System.out.println("usage: java -cp .;mail.jar MailUtil <smtphost> <to> <from> <fromName> <subject> <body> <smtpAuthId> <smtpAuthPass>");
				return;
			}
			String smtphost = args[0];
			String to = args[1];
			String from = args[2];
			String fromName = args[3];
			String subject = args[4];
			String body = args[5];
			String smtpAuthId = (args.length == 8)? args[6]: "";
			String smtpAuthPass = (args.length == 8)? args[7]: "";

			if (args.length == 6) {
				send(smtphost, from, fromName, to, subject, body);
			}
			else {
				send(smtphost, from, fromName, to, subject, body, smtpAuthId, smtpAuthPass);
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}