All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mycomm.IMails.MyEmailProcessor.msg.impl.MySmartEmailProcessor Maven / Gradle / Ivy

There is a newer version: 1.0.5
Show newest version
/*
 * Copyright 2018 jw362j.
 *
 * 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 com.mycomm.IMails.MyEmailProcessor.msg.impl;

import com.mycomm.IMails.MyEmailProcessor.ListenerGenerator;
import com.mycomm.IMails.MyEmailProcessor.account.EmailAuthNAccount;
import com.mycomm.IMails.MyEmailProcessor.log.ProjectLogProcessor;
import com.mycomm.IMails.MyEmailProcessor.msg.MyMessage;
import com.mycomm.itool.MyPublicTool.SystemUtil;
import java.io.File;
import java.io.IOException;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

/**
 *
 * @author jw362j
 */
public class MySmartEmailProcessor implements ListenerGenerator.MyEmailProcessor {

    private final ListenerGenerator.AccountGenerator accountGenerator;

    public MySmartEmailProcessor(ListenerGenerator.AccountGenerator accountGenerator) {
        this.accountGenerator = accountGenerator;
    }

    @Override
    public MyMessage recEmail(ListenerGenerator.EmailEvent emailEvent) {
        return null;
    }

    @Override
    public void sendMe(final MyMessage mailMessage) {
        send(mailMessage);
    }

    private void send(MyMessage mailMessage) {
        if (mailMessage == null || !SystemUtil.isEmailFormat(mailMessage.getTo())) {
            ProjectLogProcessor.getInstance().getLogger().infor("myMessage is invalide..give up!");
            return;
        }
        if (accountGenerator == null) {
            ProjectLogProcessor.getInstance().getLogger().error("accountGenerator is null..give up!");
            return;
        }
        EmailAuthNAccount account = accountGenerator.AccountGen();
        if (account == null || "".equals(account.getAccountID()) || "".equals(account.getAccountPWD()) || account.getAccountID() == null || account.getAccountPWD() == null) {
            ProjectLogProcessor.getInstance().getLogger().error("EmailAuthNAccount is invalide..give up!account is:" + account);
            return;
        }
        HtmlEmail email = new HtmlEmail();
        try {
            email.setHostName(account.getHost());            
            email.setAuthentication(account.getAccountID(),accountGenerator.getPasswordProcessor().decodePassword(account.getAccountPWD()));
            email.setCharset(MyMessage.ENCODEING);
            email.setFrom(account.getAccountID().equals(mailMessage.getFrom()) ? mailMessage.getFrom() : account.getAccountID());
            email.addTo(mailMessage.getTo());

            if (mailMessage.getCc() != null) {
                String[] cc_ = mailMessage.getCc();
                if (cc_ != null & cc_.length > 0) {
                    for (String c : cc_) {
                        if (SystemUtil.isEmailFormat(c)) {
                            email.addCc(c);
                        } else {
                            ProjectLogProcessor.getInstance().getLogger().infor(c + " is not a valide cc email address!");
                        }
                    }
                }
            }
            email.setSubject(mailMessage.getTitle());
            ProjectLogProcessor.getInstance().getLogger().infor("the mail content is:" + mailMessage.getMail_content());
            email.setMsg(mailMessage.getMail_content());
            if (!"".equals(mailMessage.getMailAttachmentAbsFilePath()) && mailMessage.getMailAttachmentAbsFilePath() != null) {
                //append attachment 
                File attachment = new File(mailMessage.getMailAttachmentAbsFilePath());
                if (attachment != null && attachment.exists() && attachment.isFile() && attachment.length() < MyMessage.attachment_file_size) {
                    EmailAttachment attachment_ = new EmailAttachment();
                    attachment_.setPath(mailMessage.getMailAttachmentAbsFilePath());
                    attachment_.setDisposition(EmailAttachment.ATTACHMENT);
                    attachment_.setDescription("Picture of John");
                    email.attach(attachment_);
                } else {
                    ProjectLogProcessor.getInstance().getLogger().infor("attachment is invalide !");
                }
            }
            email.send();
            if (mailMessage.getMailSendListener() != null) {
                mailMessage.getMailSendListener().onSend(true);
            }
            ProjectLogProcessor.getInstance().getLogger().infor(account.getAccountID() + " send email to: " + mailMessage.getTo());
        } catch (EmailException e) {
            ProjectLogProcessor.getInstance().getLogger().infor(account.getAccountID() + " send the mail to " + mailMessage.getTo()
                    + " is failed, reason:" + e.getMessage());
            if (mailMessage.getMailSendListener() != null) {
                mailMessage.getMailSendListener().onSend(false);
            }
        }
    }

    @Override
    public void dismiss() {
        ProjectLogProcessor.getInstance().getLogger().infor("email sending is done,dismissing....");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy