일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 | 29 |
30 | 31 |
- cisco vpn
- 코로나백신 어지러움
- 성수족발
- 신주쿠맛집
- windows10 cisco vpn
- 화이자백신
- Windows10
- 타카마루
- 크롬 인너넷연결 안됨
- 코로나백신
- 분기날짜
- 응용 프로그램 내 구입
- 로니세라
- 노리로또
- windows10 크롬
- 크롬 타임아웃
- 강화 프로방스
- 코로나백신 갈증
- takanaru
- 오라클
- 10 올림
- 안드로이드폰 위치
- 크롬 인터넷연결
- 신주쿠 로컬식당
- 핸드폰 찾기
- 10원단위 올림
- 윈도우10 vpn
- 코로나백신 부작용
- 코로나백신이상증상
- 랍스터 찜
- Today
- Total
이거 맘대로 되는 세상이 아니구만...
javaMail 첨부메일 보내기 본문
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import com.top.common.util.StringUtil;
import com.top.eng.entity.Entity;
/**
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class consoleMail {
private static final String SMTP_HOST_NAME = "smtp.naver.com"; //HOST
private static final String SMTP_AUTH_USER = "********"; //ID
private static final String SMTP_AUTH_PWD = "******"; //PASSWORD
private static final String emailMsgTxt = ""; //내용
private static final String emailSubjectTxt = ""; //제목
private static final String emailFromAddress = "AAA@GMAIL.COM"; //보내는 이 이메일주소
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"aaa@gmail.com", "ccc@gmail.com"}; //받는 사람 메일주소
public static void main(String args[]) throws Exception {
try {
consoleMail smtpMailSender = new consoleMail();
smtpMailSender.postMail(emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("["+StringUtil.getTodayTime()+"] : Sucessfully Sent mail to All Users"); //로그
} catch (Exception e) {
e.printStackTrace();
System.out.println("["+StringUtil.getTodayTime()+"] : Error Sent mail to All Users =]: "+ e.getMessage());
}
}
public void postMail(String recipients[], String subject, String message,
String from) throws Exception {
boolean debug = false;
try {
//Set the host smtp address
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++) {
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
//메일 제목///
subject = StringUtil.getDate( StringUtil.getOffsetDateToDate(StringUtil.getToday("yyyyMMdd"), -1), "yyyy-MM-dd")+"일 정보입니다.";
msg.setSubject(MimeUtility.encodeText(subject,"EUC-KR","B"));
//메일 내용
message = "<html><body><font color=green>"
+ "<b>★ 머꼬<b>"
+ 머꼬"
+ " 머꼬
+ "</font></body></html>";
MimeBodyPart mbp_cont = new MimeBodyPart();
mbp_cont.setContent(message, "text/html; charset=euc-kr"); //한글 과 html사용
String fileaddress = "파일이 있는 절대경로"; //파일이 있는 절대경로
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(fileaddress);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(MimeUtility.encodeText(fds.getName(),"EUC-KR","B")); //한글파일네임을 적기 위해서
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp_cont);
mp.addBodyPart(mbp);
msg.setContent(mp);
Transport.send(msg);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
/**
* SimpleAuthenticator is used to do simple authentication when the SMTP
* server requires it.
*/
private class SMTPAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}