자바 메일 발송
SendMail mail = new SendMail();
int a = mail.sendMemberMail(SMTP 서버, 수신자메일주소, 제목, 보내는이);
if(a>0){
성공
}else if(a==0){
SMTP 서버 문제
}else{
실패
}
=================SendMail.java======================================
package ;
import java.net.MalformedURLException;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import com.sun.mail.smtp.*;
public class SendMail {
public static int sendMemberMail(String smtp, String tolist, String subject, String from) {
Main knoc =new Main();
Document doc = null;
try {
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
//내용 쓰기
String body = "";
body += "<table><tr><td>";
body += "====================================================\n\n";
body += "</td></tr>";
body += "<tr><td>";
body += "안녕하십니까.\n";
body += "</td></tr>";
body += "<tr><td>";
body += "테스트 메일 입니다. \n";
body += "</td></tr>";
body += "<tr><td>";
body += "</td></tr>";
body += "<tr><td>";
body += "=====================================================\n";
body += "</td></tr>";
body += "</table>";
try {
Session ses;
URLName url;
url = new URLName(smtp);
Properties props = System.getProperties();
props.put("mail.smtp.allow8bitmime", "true");
props.put("mail.smtp.sendpartial", "true");
props.put("mail.smtp.host", smtp);
ses = Session.getInstance(props, null);
com.sun.mail.smtp.SMTPMessage msg = new SMTPMessage(ses);
msg.setAllow8bitMIME(true);
InternetAddress fromAddr = new InternetAddress(
from);
fromAddr.setPersonal("보내는사람 이름", "EUC-KR");
msg.setFrom(fromAddr);
subject = MimeUtility.encodeText(subject, "EUC-KR", "B");
msg.setSubject(subject);
InternetAddress[] address = { new InternetAddress(tolist) };
msg.setRecipients(Message.RecipientType.TO, address);
body = replaceString(body, "\n", "<br>");
msg.setContent(body, "text/html;charset=\"EUC-KR\"");
msg.removeHeader("Content-Transfer-Encoding");
msg.addHeader("Content-Transfer-Encoding", "base64");
msg.setSentDate(new Date());
Transport.send(msg);
//logger.info(body);
} catch (Exception e) {
knoc.log(e.toString());
if(e.toString().startsWith("javax.mail.MessagingException: Could not connect to SMTP host")){
return 0;
}
return -1;
}
return 1;
}
}
'Java' 카테고리의 다른 글
java.lang.UnsupportedClassVersionError: Bad version number in .class file 에러 날때.. (0) | 2009.05.06 |
---|---|
간단한 스케줄링 데몬 만들기.. (0) | 2008.11.25 |
POI 이용해 엑셀파일 읽기 (0) | 2008.07.28 |
자바 Serializable(객체직렬화) 를 통해 소켓 전송 수신. (2) | 2008.07.28 |
jxl.jar 이용해서 엑셀파일 읽어 들이기~ (0) | 2008.07.24 |