728x90
Java mail. 파일첨부하기.
import java.net.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* msgmultisendsample creates a simple multipart/mixed message and sends
Appendix B: Examples Using the JavaMail API 79
Example: Sending a Message
JavaMail™ API Design Specification September 2000
* it. Both body parts are text/plain.
*
* usage:java msgmultisendsample to from smtp true|false
* where to and from are the destination and
* origin email addresses, respectively, and smtp
* is the hostname of the machine that has smtp server
* running. The last parameter either turns on or turns off
* debugging during sending.
*/
public class MailSenderNoThread
{
private boolean debug = true;
//SMTP Server 관련 환경
private String smtpHost;
private String smtpUser;
private String smtpPassword;
private boolean requireAuth;
//Email전송 관련 환경
private String senderemail; //보내는 사람
private String rcvemail; //받는 사람
private String cc; //참조
private String bcc; //숨은 참조
private String title; //제목
private String contents; //내용
private String mailformat; //메일 형식
private String base; //첨부파일이 있을 경우 파일이 존재하는 Location(디텍토리명)
private String filename; //파일이름
private int filelength; //파일 길이
private String fullpath; //파일의 Full Path
private File f = null; //첨부파일을 메일에 첨부하기 위한 파일 객체
private String sessionid;
private Thread m_thread;
public MailSenderNoThread(String senderemail, String rcvemail,String cc, String bcc, String title, String contents, String mailformat, String base, String filename, String sessionid)
{
this.senderemail = senderemail;
this.rcvemail = rcvemail;
this.cc = cc;
this.bcc = bcc;
this.title = title;
this.contents = contents;
this.mailformat = mailformat;
this.base = base;
this.filename = filename;
this.sessionid = sessionid;
try
{
Property m_prop = PropertyManager.getInstance();
smtpHost = m_prop.get("smtp.host");
smtpUser = m_prop.get("smtp.user");
smtpPassword = m_prop.get("smtp.password");
requireAuth = m_prop.get("smtp.auth").equalsIgnoreCase("true") ? true : false;
if(smtpHost==null)
throw new NullPointerException("SMTP host can't be null");
}
catch(IOException ie)
{
if(debug)
ie.printStackTrace();
}
}
public void send()
{
MimeBodyPart mbp2 = null;
FileDataSource fds = null;
//filename = LangConvert.toKor(filename);
fullpath = base + File.separator + sessionid + "_" + filename;
if(debug)
System.out.println("## File Name : " + fullpath);
if(rcvemail==null)
throw new NullPointerException("Email address can't be null.");
if(title==null)
title = "";
if(contents == null)
contents = "";
if(filename != null && filename.length() > 0)
{
//파일크기를 구한다.
try
{
f = new File(fullpath);
filelength = Integer.parseInt(""+f.length());
}
catch(Exception e)
{
e.printStackTrace();
}
}
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
Session session = null;
if(requireAuth && smtpUser != null)
{
props.put("mail.smtp.auth", "true");
MailAuthenticator auth = new MailAuthenticator(smtpUser, smtpPassword);
session = Session.getDefaultInstance(props, auth);
}
else
{
session = Session.getDefaultInstance(props, null);
}
session.setDebug(debug);
try
{
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(senderemail));
InternetAddress[] address = {new InternetAddress(rcvemail)};
msg.setRecipients(Message.RecipientType.TO, address);
if(cc != null)
{
InternetAddress[] addrcc = {new InternetAddress(cc)};
msg.setRecipients(Message.RecipientType.CC, addrcc);
}
if(bcc != null)
{
InternetAddress[] addrbcc = {new InternetAddress(bcc)};
msg.setRecipients(Message.RecipientType.BCC, addrbcc);
}
//System.out.println(msg.getRecipients(Message.RecipientType.TO).toString());
//System.out.println(msg.getRecipients(Message.RecipientType.CC).toString());
//System.out.println(msg.getRecipients(Message.RecipientType.BCC).toString());
msg.setSubject(title);
msg.setSentDate(new Date());
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
if(mailformat.equalsIgnoreCase("HTML"))
mbp1.setContent(contents, "text/html; charset=euc-kr");
else
mbp1.setContent(contents, "text/plain; charset=euc-kr");
if(filename != null && filename.length() > 0)
{
// create the second message part
mbp2 = new MimeBodyPart();
// attach the file to the message
fds = new FileDataSource(fullpath);
mbp2.setDataHandler(new DataHandler(fds));
//mbp2.setFileName(fds.getName());
mbp2.setFileName(LangConvert.toUni(filename));
//System.out.println("fds.getName() : " + fds.getName());
}
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
//80 Appendix B: Examples Using the JavaMail API
//Example: Sending a Message
mp.addBodyPart(mbp1);
if(filename != null && filename.length() > 0)
mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// send the message
Transport.send(msg);
//Transport.close();
new FileUtility(base, sessionid+"_"+filename).delete();
}
catch (MessagingException mex)
{
Exception ex = null;
if ((ex = mex.getNextException()) != null)
{
ex.printStackTrace();
}
}
finally
{
//Do Something
}
}
}
728x90
'Development > Java' 카테고리의 다른 글
[Spring Framework] java.util.zip.ZipException: invalid LOC header (bad signature) (0) | 2019.06.16 |
---|---|
Multicast Programming. 멀티캐스트 프로그래밍. (0) | 2019.06.16 |
JAVA를 이용한 메일 보내기 (1) | 2019.06.16 |
[Eclipse] AnyEdit 플러그인 설치 방법 (0) | 2019.06.16 |
[Eclipse] 어느날 갑자기.... Tomcat Server start error. (0) | 2019.06.16 |
댓글