org.homiefund.api.support.MailServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for org.homiefund.api.support.MailServiceImpl.java

Source

/**
 * Copyright  2016 REPLACE ME OWNER (REPLACE ME YEAR)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.homiefund.api.support;

import lombok.extern.log4j.Log4j2;
import org.homiefund.api.dto.InvitationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.scheduling.annotation.Async;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 25.9.2016.
 */
@Log4j2
public class MailServiceImpl implements MailService {
    private MailSender mailSender;
    private SimpleMailMessage templateMessage;

    @Autowired
    public MailServiceImpl(MailSender mailSender, SimpleMailMessage templateMessage) {
        this.mailSender = mailSender;
        this.templateMessage = templateMessage;
    }

    @Override
    @Async
    public void sendInvite(InvitationDTO invitationDTO, String email) {
        SimpleMailMessage msg = new SimpleMailMessage(this.templateMessage);
        msg.setTo(email);
        msg.setText(invitationDTO.getHash());

        try {
            mailSender.send(msg);
        } catch (MailException e) {
            log.fatal(e);
        }
    }
}