org.ktunaxa.referral.server.command.test.TestEmailCommand.java Source code

Java tutorial

Introduction

Here is the source code for org.ktunaxa.referral.server.command.test.TestEmailCommand.java

Source

/*
 * Ktunaxa Referral Management System.
 *
 * Copyright (C) see version control system
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.ktunaxa.referral.server.command.test;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.geomajas.command.Command;
import org.geomajas.global.ExceptionCode;
import org.geomajas.global.GeomajasException;
import org.ktunaxa.referral.server.command.dto.TestEmailRequest;
import org.ktunaxa.referral.server.command.dto.TestEmailResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.stereotype.Component;

/**
 * 
 * @author Jan De Moerloose
 *
 */
@Component(TestEmailRequest.COMMAND)
public class TestEmailCommand implements Command<TestEmailRequest, TestEmailResponse> {

    @Autowired
    private JavaMailSenderImpl mailSender;

    @Override
    public TestEmailResponse getEmptyCommandResponse() {
        return new TestEmailResponse();
    }

    @Override
    public void execute(TestEmailRequest request, TestEmailResponse response) throws Exception {
        final String from = "rms@ktunaxa.org";
        final String to = request.getTo();
        if (null == to) {
            throw new GeomajasException(ExceptionCode.PARAMETER_MISSING, "to");
        }
        MimeMessagePreparator preparator = new MimeMessagePreparator() {

            public void prepare(MimeMessage mimeMessage) throws Exception {

                mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                mimeMessage.setFrom(new InternetAddress(from));
                mimeMessage.setSubject("Test");
                mimeMessage.setText("Testing RMS");
            }
        };
        mailSender.send(preparator);
    }

}