๐SMTP For Gmail
๐Gmail ๋ฒ์
๐Batch ํ๋ก์ธ์ค๋ฅผ ๊ฐ๋ฐํ๋ ์ค, ํน์ ์๋์ ๋ฐ๊ณ ์ถ์ ๊ฒฝ์ฐ๊ฐ ์๊ฒผ๋ค. Batch ์์๊ณผ ์๋ฃ ์์ ์ด๋ผ๋์ง, Schedule ์ ๋ฐ๋ฅธ ๋ฐ์ดํฐ ํ์ธ์ด๋ผ๋์ง.. ๊ทธ๋ฐ ์๊ฐ์ ์ํด SMTP Library ๋ฅผ ์ฌ์ฉํด์ ์ฌํํ๊ฒ ๊ตฌ์ถํด๋ดค๋ค.
์ฌ๊ธฐ์์ ํ์ฅ์ฑ์ ์ผ๋์ ๋๊ณ ์ถ๊ฐ ๊ฐ๋ฐ์ ํ๋ฉด ๋์ฑ ๋ฉ์ง ์ฝ๋๊ฐ ๋ ๊ฒ์ด๋ค.
์๋๋ ๋ฐ๋ก ๊ตฌํํ ์๋๋ฆฌ์ค์ด๋ค.
* ์์ ์๋ฅผ ๊ณ ์ ํ๋๋ก
* ํ์ ์ฐธ์กฐ๋ก ๋๋ฅผ ๊ณ ์ ํ๋๋ก
* ํ ๋ฉ์ผ์ ์๋ ์์ฑํ๋๋ก
* ์กฐ๊ฑด์ ๋ฐ๋ฅธ ๋ฉ์ผ ๋ด์ฉ์ด ๋ค๋ฅด๊ฒ ์ ์ก๋๋๋ก
* ํน์ ํ๋ผ๋ฏธํฐ๋ฅผ ํตํด์๋ ๋ฉ์ผ์ด ์ ์ก๋๋๋ก
๐จ๐ป๐ปโจ
package smtp;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
/**
*
* @lib mail-1.4.7.jar
*
*/
@Slf4j
public class AdjustSmtp {
//Parameters : ์๋ฌ๋ ์ง, ์๋ฌ์๊ฐ, ์๋ฌํ์
, (+Shell์์ ํ์ผ๋ช
) ex) 20210420, 14:01:00, DDC, (+FileName)
public static void main(String[] args) {
final String user = "@gmail.com"; //๋ฐ์ ๋ฉ์ผ (์ด ์ฌ๋ ๊ณ์ ์ผ๋ก ๋ณด๋ด๊ฒ ๋ค.)
final String password = ""; //ํจ์ค์๋
final String port="587";
final String bodyEncoding = "UTF-8"; //์ฝํ
์ธ ์ธ์ฝ๋ฉ
List <String> arr=new ArrayList<String>();
/*for (int i=0;i<args.length;i++) {
arr.add(args[i]);
} */
// SMTP ์๋ฒ ์ ๋ณด๋ฅผ ์ค์ ํ๋ค.
Properties prop = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.port", port);
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.ssl.trust", "smtp.gmail.com");
prop.put("mail.smtp.starttls.enable","true");
Session session = Session.getDefaultInstance(prop, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
StringBuffer sb = new StringBuffer();
//๋ฉ์์ง ์์ธ์ฒ๋ฆฌ
try {
sb.append("<div class=\"container\"\r\n"
+ "style=\"max-width: 350px; max-height: 630px; overflow: hidden; margin: 30px auto 0; box-shadow: 0 0 40px #a0a0a0; font-family: '\r\n"
+ "OpenSans', sans-serif; OpenSans', sans-serif;\">\r\n"
+ "<div class=\"timeline\">\r\n"
+ "<ul\r\n"
+ "style=\"padding: 1em 0 0 2em; margin: 0; list-style: none; position: relative;\">\r\n"
+ "<li>\r\n"
+ "<div class=\"bullet pink\"\r\n"
+ "style=\"width: 1em; height: 1em; box-sizing: border-box; border-radius: 50%; background: #fff; z-index: 1; margin-right: 1em; border: 2px solid #f93b69; display: inline-block; margin: 1em 0; vertical-align: top;\"></div>\r\n"
+ "<div class=\"time\"\r\n"
+ "style=\"width: 20%; font-size: 0.75em; padding-top: 0.25em; display: inline-block; margin: 1em 0; vertical-align: top;\">Time : "+"</div>\r\n"
+ "<div class=\"desc\"\r\n"
+ "style=\"width: 50%; display: inline-block; margin: 1em 0; vertical-align:"
+ " top;\">\r\n"
+ "<h3 style=\"font-size: 0.9em; font-weight: 400; margin: 0;\">TYPE : "+"</h3>\r\n"
+ "<h4\r\n"
+ "style=\"margin: 0; font-size: 0.7em; font-weight: 400; color: #808080;\"> JOB DATE : "+"</h4>\r\n"
+ "</div>\r\n"
+ "</li>\r\n"
+ "</ul>\r\n"
+ "</div>\r\n"
+ "</div>");
//sb.append(arr.get(0)+"\n");
//sb.append(arr.get(1)+"\n");
String html = sb.toString();
//๋ฉ์์ง ํฌ๋งท์ ๋ด๊ธฐ์ํด ์์ฑ
MimeMessage message = new MimeMessage(session);
// ๋ฉ์ผ ์ฝํ
์ธ ์ค์
Multipart mParts = new MimeMultipart();
MimeBodyPart mTextPart = new MimeBodyPart();
MimeBodyPart mFilePart = null;
//์ด๋ฉ์ผ ํค๋ ์ค์
message.setHeader("content-Type", "text/html");
//๋ฐ์ ์ ์ค์
message.setFrom(new InternetAddress(user));
//์์ ์๋ฉ์ผ
message.addRecipient(Message.RecipientType.TO, new InternetAddress("์์ ์์ด๋ฉ์ผ"));
//์ ๋ชฉ
//message.setSubject("โปBATCH ERROR - TYPE : "+arr.get(2)+" // File : "+arr.get(3)); //๋ฉ์ผ ์ ๋ชฉ์ ์
๋ ฅ
message.setSubject("ํ
์คํธ์ฉ");
// ๋ฉ์ผ ์ฝํ
์ธ - ๋ด์ฉ
mTextPart.setText(html, bodyEncoding, "html");
mParts.addBodyPart(mTextPart);
// ๋ฉ์ผ ์ฝํ
์ธ ์ค์
message.setContent(mParts);
// MIME ํ์
์ค์
MailcapCommandMap MailcapCmdMap = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
MailcapCmdMap.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
MailcapCmdMap.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
MailcapCmdMap.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
MailcapCmdMap.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
MailcapCmdMap.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(MailcapCmdMap);
//send
Transport.send(message); //javax.mail.Transport์
//์ ์ก ํ, Console ํ์ธ์ฉ
System.out.println("์ ์ก์๋ฃ");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}