com.mycompany.apps.MailService.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.apps.MailService.java

Source

/*
 * 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.mycompany.apps;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

/**
 * ?
 */
public class MailService {

    private MessageBuilder mailBuilder;

    public MailService() throws IOException {
        mailBuilder = new MessageBuilder("/mailTemplate.txt");
    }

    public void doSomeService() {
        HashMap map = new HashMap();
        map.put("name", "hoge");
        map.put("today", "2014/11/20");
        // ... ?map?????
        String msg = mailBuilder.buildMessage(map);
        System.out.println("?:" + msg);

        try {
            SimpleEmail email = new SimpleEmail();
            //??
            email.setHostName("localhost");
            //??
            List to = new ArrayList();
            InternetAddress to1 = new InternetAddress("to1@mail.myserver.com");
            to.add(to1);
            InternetAddress to2 = new InternetAddress("to2@mail.myserver.com");
            to.add(to2);
            email.setTo(to);

            //??
            email.setFrom("from@mail.myserver.com");
            //?
            email.addReplyTo("reply@mail.myserver.com");
            //??
            email.setSubject("");
            //??
            email.setMsg(msg);
            //??
            //            email.send();
        } catch (EmailException ex) {
            ex.printStackTrace();
        } catch (AddressException ex) {
            ex.printStackTrace();
        }
    }
}