예전 쇼핑몰 개발할때 그러니까 7~8년전 메일 발송하는 프로그램을 만들었던 기억이 있다.

여기 저기 소스 참고해서 만들었는데...

느리긴하지만.. smtp.gmail.com을 이용할 수 있다는게 좋다. 

3초는 걸리는 듯.

메일로 인증하는 프로그램을 만들려다 호스팅업체에서 제공하는건 하루 몇건 제한이 있어 포기했었는데.

근데, gmail도 사용제한이 있을 것 같은 느낌.


package test.mail;


import java.io.UnsupportedEncodingException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.Properties;


import javax.mail.Authenticator;

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 org.apache.log4j.Logger;


public class SMTPMailSender extends Thread {

final private  Logger logger = Logger.getLogger(getClass());


private String protocol = "smtp";

private String type = "text/html; charset=KSC5601";

private String userName = "xxxx";    //구글메일계정

private String password = "xxxx";    //구글메일계정

private String host = "smtp.gmail.com";

private int port = 587; //25

private boolean starttlsEnable = true;


private String subject = "";

private String contents = "";

private String fromEmail = "";

private String fromName = "";

ArrayList<HashMap<String, String>> list;

public SMTPMailSender(String subject, String contents, String fromEmail, String fromName, ArrayList<HashMap<String, String>> list){

this.subject = subject;

this.contents = contents;

this.fromEmail = fromEmail;

this.fromName = fromName;

this.list = list;

}

public void run() {

HashMap<String, String> map = null;

for(int i=0, j=list.size(); i<j; i++){

map = list.get(i);

try {

Thread.sleep(100);

send(map.get("email"), map.get("name"), fromEmail, fromName, subject, contents);

} catch (Exception e) {

e.printStackTrace();

}

}

}

public void send(String toEmail, String toName, String fromEmail, String fromName, String subject, String content) throws Exception {

try {

Properties props = new Properties();

props.put("mail.transport.protocol", protocol);

props.put("mail.smtp.host", host);

props.put("mail.smtp.port", String.valueOf(port));

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.starttls.enable", Boolean.toString(starttlsEnable));

Authenticator authenticator = new SMTPAuthenticator(userName, password);

Session session = Session.getInstance(props, authenticator);

MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(fromEmail, fromName));

message.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail, toName));

message.setSubject(subject);

message.setContent(content, type);

Transport.send(message);

logger.debug("메일 발송 성공"+"["+toEmail+"]");

} catch (UnsupportedEncodingException e) {

logger.debug("메일 발송 실패"+"["+toEmail+"]"+e.toString());

} catch (MessagingException e) {

logger.debug("메일 발송 실패"+"["+toEmail+"]"+e.toString());

} catch (Exception e) {

logger.debug("메일 발송 실패"+"["+toEmail+"]"+e.toString());

}

}


class SMTPAuthenticator extends Authenticator {

PasswordAuthentication passwordAuthentication;

SMTPAuthenticator(String userName, String password) {

passwordAuthentication = new PasswordAuthentication(userName, password);

}

public PasswordAuthentication getPasswordAuthentication() {

return passwordAuthentication;

}

}

}


====================================================================================================

package test.mail;

import java.util.ArrayList;
import java.util.HashMap;

public class SMTPMailSenderMain {
public static void main(String[] args) {
String subject = "제목입니다";
String contents = "본문입니다.";
String from = "leeky@xxxx.co.kr";
String fromName = "알통";

ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
HashMap<String,String> map = new HashMap<String,String>();
map.put("email", "xxxx@ gmail.com ");
map.put("name", "알통1");
list.add(map);
map = new HashMap<String,String>();
map.put("email", "xxxx@gmail.com");
map.put("name", "알통2");
list.add(map);

SMTPMailSender msm = new SMTPMailSender(subject, contents, from, fromName, list);
msm.start();
}
}


  1. ㅇㄹ 2016.03.28 15:53

    ㄴㅇㄹ