/*
 * blanco Framework
 * Copyright (C) 2004-2007 IGA Tosiki
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 */
package blanco.mail.core;

import java.io.IOException;
import java.net.ConnectException;

import blanco.mail.core.valueobject.BlancoMailAddress;
import blanco.mail.core.valueobject.BlancoMailMessage;
import blanco.mail.core.valueobject.BlancoMailMessageAttachment;
import blanco.mail.core.valueobject.BlancoMailSetting;

public class BlancoMailSenderSample {
    /**
     * blancoMailCoreを用いたメール送信サンプル。
     * 
     * @param args
     *            入力パラメータ。
     * @throws ConnectException
     *             接続例外が発生した場合。
     * @throws IOException
     *             メール送信中に例外が発生した場合。
     */
    public static void main(final String[] args) throws ConnectException,
            IOException {
        final BlancoMailSetting setting = BlancoMailSettingFactory
                .getInstance();
        if (true) {
            // 設定値は BlancoMailSetting.properties から取得します。
        } else {
            setting.setSendHost("smtp.gmail.com");
            setting.setSendPort(465);
            setting.setSendUseSsl(true);
            setting.setSendUserName("送信者○○○@gmail.com");
            setting.setSendPassword("password");
            // setting.setSessionLogDirectorySuccess("./log");
            // setting.setSessionLogDirectoryError("./log.err");
        }

        final BlancoMailMessage message = new BlancoMailMessage();
        final BlancoMailAddress from = new BlancoMailAddress();
        from.setAddress(BlancoMailTestConstants.MAIL_SEND_FROM_ADDR);
        from.setPersonal("送信者の表示名");
        message.setFrom(from);

        final BlancoMailAddress to1 = new BlancoMailAddress();
        to1.setAddress(BlancoMailTestConstants.MAIL_SEND_TO_ADDRS[0]);
        to1.setPersonal("受信者の表示名");
        message.getTo().add(to1);

        final BlancoMailAddress to2 = new BlancoMailAddress();
        to2.setAddress(BlancoMailTestConstants.MAIL_SEND_TO_ADDRS[1]);
        to2.setPersonal("受信者の表示名");
        message.getTo().add(to2);

        final BlancoMailAddress cc1 = new BlancoMailAddress();
        cc1.setAddress(BlancoMailTestConstants.MAIL_SEND_CC_ADDRS[0]);
        message.getCc().add(cc1);

        message.setSubject("[blanco] 添付ファイルなしサンプル(BlancoMailSenderSample)");
        message.setText("こんにちは。\nごきげんよう。\nさようなら。");

        BlancoMailSenderFactory.getInstance().send(setting, message);

        // 添付ファイル版
        final BlancoMailMessageAttachment attachment = new BlancoMailMessageAttachment();
        attachment.setData("添付ファイル".getBytes());
        attachment.setFileName("aaa.txt");
        message.getAttachmentList().add(attachment);

        final BlancoMailMessageAttachment attachment2 = new BlancoMailMessageAttachment();
        attachment2.setData("添付ファイル2".getBytes());
        attachment2.setFileName("添付ファイル２.txt");
        message.getAttachmentList().add(attachment2);

        message.setSubject("添付ファイルありサンプル(BlancoMailSenderSample)");
        message.setText("こんにちは。\nごきげんよう。\nさようなら。");

        BlancoMailSenderFactory.getInstance().send(setting, message);
    }
}
