메일 보내기
@Override
public void send (String toAddr, String subject, String content)
{
final BaseSetting baseSetting = settingSv.getBaseSetting ();
// javamail lib 이 필요합니다.
class MyAuthentication extends Authenticator
{
PasswordAuthentication pa;
public MyAuthentication ()
{
pa = new PasswordAuthentication (baseSetting.getCommonEmail (), baseSetting.getCommonEmailPasswd ()); //ex) ID:cafe24@cafe24.com PASSWD:1234
}
public PasswordAuthentication getPasswordAuthentication ()
{
return pa;
}
}
String host = baseSetting.getSmtpAddr (); //smtp mail server
String from = baseSetting.getCommonEmail (); //sender email address
String to = toAddr; //receiver email address
if (StrLib.isEmptyStr (baseSetting.getSmtpAddr ()) || StrLib.isEmptyStr (baseSetting.getCommonEmail ()) || StrLib.isEmptyStr (baseSetting.getCommonEmailPasswd ()))
return;;
Properties props = new Properties ();
props.put ("mail.smtp.host", host);
props.put ("mail.smtp.auth", "true");
props.put ("mail.smtp.port", String.valueOf (baseSetting.getSmtpPort ()));
if (baseSetting.getSmtpPort () == 997 || baseSetting.getSmtpPort () == 465 || baseSetting.getSmtpPort () == 587)
{
props.put ("mail.smtp.socketFactory.port", String.valueOf (baseSetting.getSmtpPort ()));
props.put ("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put ("mail.smtp.socketFactory.fallback", "false");
props.put ("mail.smtp.starttls.enable","true");
props.put ("mail.smtp.user",baseSetting.getCommonEmail ());
}
Authenticator auth = new MyAuthentication ();
Session sess = Session.getInstance (props, auth);
try
{
Message msg = new MimeMessage (sess);
msg.setFrom (new InternetAddress (from));
InternetAddress[] address = {new InternetAddress (to)};
msg.setRecipients (Message.RecipientType.TO, address);
msg.setSubject (subject);
msg.setSentDate (new Date ());
// Multipart 객체 생성
MimeMultipart multipart = new MimeMultipart ();
MimeBodyPart part = new MimeBodyPart ();
// 부가적인 해더 추가
/**********************************************************/
msg.setHeader ("MIME-Version" , "1.0" );
/**********************************************************/
// 메세지 Part 추가 S
/**********************************************************/
part.setContent (content, "text/html;charset=UTF-8");
part.removeHeader ("Content-Transfer-Encoding");
// Encoding 타입 추가..
/**********************************************************/
part.addHeader ("Content-Transfer-Encoding", "base64");
/**********************************************************/
multipart.addBodyPart (part);
/**********************************************************/
// 메세지 Part 추가 E
msg.setContent (multipart);
Transport.send (msg);
}
catch (MessagingException mex)
{
mex.printStackTrace ();
}
}
'Java > 오류해결' 카테고리의 다른 글
JavaMail 사용시 501 Syntax error 해결방법 (0) | 2012.05.02 |
---|