/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pamarin.learning.mail;

import java.util.Properties;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

/**
 *
 * @author anonymous
 */
public class EmailSender {

    private static final String SMTP_HOST_NAME = "smtp.gmail.com";
    private static final String SOURCE_EMAIL = "SOURCE_EMAIL";
    private static final String SOURCE_EMAIL_PASSWORD = "SOURCE_EMAIL_PASSWORD";
    private static final String DESTINATION_EMAIL = "DESTINATION_EMAIL";

    private static final String emailSubject = "test send email";
    private static final String emailMessage = "test!!!";

    static {
        MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
        mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
        mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
        mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
        mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
        mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
        CommandMap.setDefaultCommandMap(mc);
    }

    public static void main(String args[]) throws Exception {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        
        
        Properties pro = new Properties();
        pro.setProperty("mail.smtps.auth", "true");
        pro.setProperty("mail.smtps.starttls.enable", "true");
        pro.setProperty("mail.debug", "true");

        sender.setJavaMailProperties(pro);
        sender.setUsername(SOURCE_EMAIL);
        sender.setPassword(SOURCE_EMAIL_PASSWORD);
        sender.setProtocol("smtps");
        sender.setPort(465);
        sender.setHost(SMTP_HOST_NAME);
        sender.setDefaultEncoding("utf-8");

        MimeMessage message = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(message, true);

        helper.setSubject(emailSubject);
        helper.setText(emailMessage);

        helper.setTo(DESTINATION_EMAIL);
        sender.send(message);
    }
}

@@ -1,189 +1,4 @@
-/*%0A * To change this license header, choose License Headers in Project Properties.%0A * To change this template file, choose Tools %7C Templates%0A * and open the template in the editor.%0A */%0A
 pack
@@ -29,16 +29,37 @@
 .mail;%0A%0A
+import java.io.File;%0A
 import j
@@ -198,16 +198,71 @@
 essage;%0A
+import org.springframework.core.io.FileSystemResource;%0A
 import o
@@ -397,17 +397,31 @@
 hor 
-anonymous
+jittagorn pitakmetagoon
 %0A */
@@ -800,16 +800,16 @@
 email%22;%0A
-
     priv
@@ -857,16 +857,133 @@
 est!!!%22;
+%0A    //%0A    private static final String ATTACH_FILE_PATH = %22%22;%0A    private static final String ATTACH_FILE_NAME = %22%22;
 %0A%0A    st
@@ -2453,24 +2453,246 @@
 lMessage);%0A%0A
+//        File directory = new File(ATTACH_FILE_PATH); %0A//        FileSystemResource file = new FileSystemResource(new File(directory, ATTACH_FILE_NAME));%0A//        helper.addAttachment(ATTACH_FILE_NAME, file);%0A %0A        %0A
         help

add attach file