com.vicky.common.utils.sendemail.SendEmailImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.vicky.common.utils.sendemail.SendEmailImpl.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.vicky.common.utils.sendemail;

import com.vicky.common.utils.statusmsg.StatusMsgException;
import javax.mail.internet.MimeUtility;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.HtmlEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ???
 *
 * @author Vicky
 */
public class SendEmailImpl implements SendEmailable {

    private final Logger logger = LoggerFactory.getLogger(SendEmailImpl.class);

    @Override
    public void send(Mail mail) throws Exception {
        try {
            HtmlEmail htmlEmail = new HtmlEmail();
            htmlEmail.setHostName(mail.getHost());
            htmlEmail.setCharset(Mail.ENCODEING);
            htmlEmail.addTo(mail.getReceiverEmailAddress());
            htmlEmail.setFrom(mail.getSenderEmailAddress(), mail.getName());
            htmlEmail.setAuthentication(mail.getSenderUsername(), mail.getSenderPassword());
            htmlEmail.setSubject(mail.getSubject());
            htmlEmail.setMsg(mail.getMessage());
            for (MailAttach mailFile : mail.getMailAttachs()) {
                EmailAttachment attachment = new EmailAttachment();//  
                attachment.setPath(mailFile.getFilePath());//?    
                //attachment.setURL(new URL("http://www.baidu.com/moumou"));//?  
                attachment.setDisposition(EmailAttachment.ATTACHMENT);
                attachment.setName(MimeUtility.encodeText(mailFile.getFileName()));//??  
                htmlEmail.attach(attachment);//,?
            }
            htmlEmail.send();
        } catch (Exception exception) {
            exception.printStackTrace();
            this.logger.error("toString" + exception.toString());
            this.logger.error("getMessage" + exception.getMessage());
            this.logger.error("exception", exception);
            throw new StatusMsgException(
                    "??,??,?,?V!");
        }
    }

}