๐Ÿ‘จ‍๐Ÿ’ป kimtongting
article thumbnail

๐Ÿ“”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();
        }
	}
}


 

๋ฐ˜์‘ํ˜•
profile

๐Ÿ‘จ‍๐Ÿ’ป kimtongting

@kimtongting ์ž…๋‹ˆ๋‹ค

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!