Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- JavaScript
- eclipse query validate
- jQuery
- 원격 제어
- 이클립스
- 플러그인
- tomcat
- HelloWorld
- org.apache.commons.net.ftp
- WTP
- 중복 로그인
- AWS Architecture
- mod_jk
- mod_proxy_ajp
- 펀드
- apache 와 tomcat 연동
- AWS
- eclipse jquery
- Divx 플레이어
- iText
- javax.mail
- apache
- AWS Web Console
- Amazon Web Service
- defaultContext
- 재태크
Archives
- Today
- Total
비얌
javax.mail.* 사용기 본문
(2013년 버전은 이쪽으로....)
사내 Site 업무로 다량의 메일을 발송할 필요가 있어 javax.mail.* 클래스들을 이용하게 되었는데... 혹시 싶어 기록을 남깁니다.
Javax.mail 클래스들은 sun 사에서 제공되는 Mail 관련 클래스 들로 Mail발송시 유용합니다.
일단 javax.mail 관련 package(mail.jar,activation.jar)들을 받아봅시다.
여기서 메일 관련 package들을 받고.
class path는 잘 잡고..
프로그램을 작성합니다...
===================================================
package kr.co.cleanyes;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mailsend {
String from = "";
String msgText = "";
String name = "";
String tel = "";
String contents = "";
public void setMailForm(String from,String name,String tel,String contents){
this.from = from;
this.name = name;
this.tel = tel;
this.contents =contents;
}
public void sendMail() throws Exception {
String send_mail_address = ""; //보내는 사람 주소
String host = ""; //SMTP 서버 주소
send_mail_address = st.nextToken();
host = st.nextToken();
String msgSubj = "Title 작성";//메일 제목
String _mp1charset = "text/html; charset=euc-kr";
String from = "";
String msgText = "";
String name = "";
String tel = "";
String contents = "";
public void setMailForm(String from,String name,String tel,String contents){
this.from = from;
this.name = name;
this.tel = tel;
this.contents =contents;
}
public void sendMail() throws Exception {
String send_mail_address = ""; //보내는 사람 주소
String host = ""; //SMTP 서버 주소
send_mail_address = st.nextToken();
host = st.nextToken();
String msgSubj = "Title 작성";//메일 제목
String _mp1charset = "text/html; charset=euc-kr";
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session sess = Session.getDefaultInstance(props, null);
try {
// create a message
InternetAddress _from = new InternetAddress(from);
Message msg = new MimeMessage(sess);
msg.setFrom(_from);
InternetAddress[] address = {new InternetAddress(send_mail_address)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(msgSubj);
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session sess = Session.getDefaultInstance(props, null);
try {
// create a message
InternetAddress _from = new InternetAddress(from);
Message msg = new MimeMessage(sess);
msg.setFrom(_from);
InternetAddress[] address = {new InternetAddress(send_mail_address)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(msgSubj);
//.........1
MimeBodyPart mbp1 = new MimeBodyPart();
//본문 내용 추가.
mbp1.setContent("메일 내용 작성...",_mp1charset);
MimeBodyPart mbp1 = new MimeBodyPart();
//본문 내용 추가.
mbp1.setContent("메일 내용 작성...",_mp1charset);
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
// ...............1 종료
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
// ...............1 종료
// add the Multipart to the message
msg.setContent(mp);
msg.setSentDate(new Date());
Transport.send(msg);
//System.out.println("Mail sent.");
} catch (MessagingException mex) {
//System.out.println(mex.getMessage());
//System.out.println("Mail Error");
}
}
}
}
=======================================================
여기서 재미있는 사실을 하나 발견할 수 있는데.
outlook을 사용하는 사람들이라면 html 원본을 그대로 import 시켜 보내기를 바랍니다.
물론 디자이너가 작업한 페이지가 보통 html로 작성이 되어있기 때문에.
원문을 그대로 살려서 전송을 하려면..
1표시 부분을....이런 식으로 고치면 됩니다.
=========================================================
MimeBodyPart mbp1 = new MimeBodyPart();
BodyPart bp = mailsender.getFileBodyPart("invitation.html");
BodyPart bp = mailsender.getFileBodyPart("invitation.html");
Multipart mp = new MimeMultipart("related");
mp.addBodyPart(bp);
mp.addBodyPart(bp);
======================================================
이렇게 작성하고 보냈더니 성공적이었습니다.
mail API를 보니 생각 보다 쓸만한게 많이 있었습니다.
열심히 보시기를....
ps. mail일 관련 좋은 Tip에 대한 설명
Comments