⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 importmailet.java

📁 开源项目CRM之OpenCustomer
💻 JAVA
字号:
package org.opencustomer.mail.james;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Part;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimePart;

import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
import org.apache.mailet.MailetContext;
import org.opencustomer.application.db.dao.crm.ContactDAO;
import org.opencustomer.application.db.dao.crm.PersonContactDAO;
import org.opencustomer.application.db.dao.crm.PersonDAO;
import org.opencustomer.application.db.vo.crm.ContactVO;
import org.opencustomer.application.db.vo.crm.PersonContactVO;
import org.opencustomer.application.db.vo.crm.PersonVO;
import org.opencustomer.db.Database;

public class ImportMailet extends GenericMailet
{
    private ContactVO contact;

    private boolean contentTypetext = false;

    public void init()
    {
        Database.doInit();
    }

    public void service(Mail mail)
    {
        try
        {
            System.out.println("james2oc");
            String recipient;
            PersonContactVO pc;
            List<PersonContactVO> pcs = new ArrayList<PersonContactVO>();

            MimeMessage message = mail.getMessage();
            MailetContext mailetContext = getMailetContext();
            contact = new ContactVO();

            contact.setBoundType(ContactVO.BoundType.IN);
            contact.setContactType(ContactVO.ContactType.EMAIL);
            contact.setContactTimestamp(new Date());
            contact.setImportType(ContactVO.ImportType.NEW);

            // Subject setzen
            if (message.getSubject() != null)
                contact.setSubject(message.getSubject());
            else
                contact.setSubject("Kein Betreff angegeben");

            // Content Setzen
            insertContent(message);
            if (contentTypetext)
                contact.setContentType(ContactVO.ContentType.PLAINTEXT);
            else
                contact.setContentType(ContactVO.ContentType.HTML);

            // Absender
            List<PersonVO> senders = new PersonDAO().getByMail(mail.getSender().toString());
            if (!senders.isEmpty())
                for (PersonVO person : senders)
                {
                    pc = new PersonContactVO(person, contact);
                    pc.setRelationType(PersonContactVO.Type.SENDER);
                    pcs.add(pc);
                    if ((mailetContext.isLocalUser(mail.getSender().getUser())) && mailetContext.isLocalServer(mail.getSender().getHost()))
                    {
                        contact.setBoundType(ContactVO.BoundType.OUT);
                    }
                }
            else
                contact.setContactName("From: " + mail.getSender().toString());

            // Emfaenger TO
            Address[] addresses = message.getRecipients(MimeMessage.RecipientType.TO);
            for (Address address : addresses)
            {
                recipient = address.toString();
                List<PersonVO> recipients = new PersonDAO().getByMail(recipient);
                if (!recipients.isEmpty())
                    for (PersonVO person : recipients)
                    {
                        pc = new PersonContactVO(person, contact);
                        pc.setRelationType(PersonContactVO.Type.TO);
                        pcs.add(pc);
                    }
                else if (contact.getContactName() == null)
                    contact.setContactName("To: " + recipient);
                else
                    contact.setContactName(contact.getContactName() + ", To: " + recipient);
            }

            // Emfaenger CC
            addresses = message.getRecipients(MimeMessage.RecipientType.CC);
            if (addresses != null)
                for (Address address : addresses)
                {
                    recipient = address.toString();
                    List<PersonVO> recipients = new PersonDAO().getByMail(recipient);
                    if (!recipients.isEmpty())
                        for (PersonVO person : recipients)
                        {
                            pc = new PersonContactVO(person, contact);
                            pc.setRelationType(PersonContactVO.Type.CC);
                            pcs.add(pc);
                        }
                    else if (contact.getContactName() == null)
                        contact.setContactName("CC: " + recipient);
                    else
                        contact.setContactName(contact.getContactName() + ", CC: " + recipient);
                }

            // Emfaenger BCC
            addresses = message.getRecipients(MimeMessage.RecipientType.BCC);
            if (addresses != null)
                for (Address address : addresses)
                {
                    recipient = address.toString();
                    List<PersonVO> recipients = new PersonDAO().getByMail(recipient);
                    if (!recipients.isEmpty())
                        for (PersonVO person : recipients)
                        {
                            pc = new PersonContactVO(person, contact);
                            pc.setRelationType(PersonContactVO.Type.BCC);
                            pcs.add(pc);
                        }
                    else if (contact.getContactName() == null)
                        contact.setContactName("BCC: " + recipient);
                    else
                        contact.setContactName(contact.getContactName() + ", BCC: " + recipient);
                }

            // Datenbank Eintrag
            if (!pcs.isEmpty())
            {
                new ContactDAO().insert(contact, -1);
                for (PersonContactVO pce : pcs)
                {
                    pce.setContact(contact);
                    new PersonContactDAO().insert(pce);
                }
                mail.getMessage().addHeader("Filter: ", "james2oc"); // header
                                                                        // setzten
                mail.setAttribute("james2oc", true); // mailattribut setzen
            }
        }
        catch (Throwable e)
        {
            System.out.println("Fehler: " + e);
        }
    }

    public String getMailetInfo()
    {
        return "James to Opencustomer Mailet";
    }

    public void destroy()
    {
        Database.closeFactory();
    }

    private void insertContent(MimePart part) throws MessagingException, IOException
    {
        boolean rc = false;
        String disposition = part.getDisposition();
        if (disposition == null)
        {
            if (part.isMimeType("text/plain"))
            {
                contact.setContent(part.getContent().toString());
                contentTypetext = true;
            }
            else if (part.isMimeType("text/html"))
            {
                if (!contentTypetext)
                    contact.setContent(part.getContent().toString());
            }
            else if (part.isMimeType("multipart/*"))
            {
                MimeMultipart multipart = (MimeMultipart) part.getContent();
                for (int i = 0; i < multipart.getCount(); i++)
                {
                    MimeBodyPart mimeBodyPart = (MimeBodyPart) multipart.getBodyPart(i);
                    insertContent(mimeBodyPart);
                }
            }
            else
            {
                System.out.println("Unbekannter ContentType");
            }
        }
        else
        {
            if (disposition.equals(Part.ATTACHMENT) || disposition.equals(Part.INLINE))
                System.out.println("Anhang gefunden: " + part.getFileName());
            else
                System.out.println("Unbekannter ContentType");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -