Search in sources :

Example 91 with MimeMessage

use of javax.mail.internet.MimeMessage in project webofneeds by researchstudio-sat.

the class MailFileNeedProducer method readNeedFromFile.

@Override
public synchronized Dataset readNeedFromFile(final File file) throws IOException {
    logger.debug("processing as mail file: {} ", file);
    FileInputStream fis = new FileInputStream(file);
    DefaultNeedModelWrapper needModelWrapper = new DefaultNeedModelWrapper("no:uri");
    try {
        MimeMessage emailMessage = new MimeMessage(null, fis);
        MimeMessageParser parser = new MimeMessageParser(emailMessage);
        parser.parse();
        needModelWrapper.setTitle(needContentPropertyType, parser.getSubject());
        String content = null;
        if (parser.hasPlainContent()) {
            content = parser.getPlainContent();
        } else if (parser.hasHtmlContent()) {
            Document doc = Jsoup.parse(parser.getHtmlContent());
            content = doc.text();
        }
        if (content != null) {
            needModelWrapper.setDescription(needContentPropertyType, content);
        }
        logger.debug("mail subject          : {}", parser.getSubject());
        logger.debug("mail has plain content: {}", parser.hasPlainContent());
        logger.debug("mail has html content : {}", parser.hasHtmlContent());
        logger.debug("mail has attachments  : {}", parser.hasAttachments());
        logger.debug("mail plain content    : {}", StringUtils.abbreviate(parser.getPlainContent(), 200));
        logger.debug("mail html content     : {}", StringUtils.abbreviate(parser.getHtmlContent(), 200));
        return needModelWrapper.copyDataset();
    } catch (Exception e) {
        logger.error("could not parse email from file {} ", file, e);
    } finally {
        if (fis != null)
            fis.close();
    }
    return null;
}
Also used : DefaultNeedModelWrapper(won.protocol.util.DefaultNeedModelWrapper) MimeMessage(javax.mail.internet.MimeMessage) MimeMessageParser(org.apache.commons.mail.util.MimeMessageParser) Document(org.jsoup.nodes.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException)

Example 92 with MimeMessage

use of javax.mail.internet.MimeMessage in project webofneeds by researchstudio-sat.

the class MailCommandAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof MailCommandEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        MimeMessage message = ((MailCommandEvent) event).getMessage();
        String referenceId = MailContentExtractor.getMailReference(message);
        WonURI wonUri = botContextWrapper.getWonURIForMailId(referenceId);
        // determine if the mail is referring to some other mail/need/connection or not
        if (wonUri != null) {
            processReferenceMailCommands(message, wonUri);
        } else {
            processNonReferenceMailCommand(message);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) MailCommandEvent(won.bot.framework.eventbot.event.impl.mail.MailCommandEvent)

Example 93 with MimeMessage

use of javax.mail.internet.MimeMessage in project webofneeds by researchstudio-sat.

the class SubscribeUnsubscribeAction method doRun.

@Override
protected void doRun(final Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof SubscribeUnsubscribeEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        // save the new subscription status of the user to the bot context
        SubscribeUnsubscribeEvent subscribeEvent = (SubscribeUnsubscribeEvent) event;
        SubscribeStatus subscribeStatus = subscribeEvent.getSubscribeStatus();
        String senderMailAddress = MailContentExtractor.getMailSender(subscribeEvent.getMessage());
        botContextWrapper.setSubscribeStatusForMailAddress(senderMailAddress, subscribeStatus);
        // depending on the new subscribe status of the user publish his cached mails as needs or delete the cache
        if (SubscribeStatus.SUBSCRIBED.equals(subscribeStatus)) {
            EventBus bus = getEventListenerContext().getEventBus();
            Collection<MimeMessage> cachedMessages = botContextWrapper.loadCachedMailsForMailAddress(senderMailAddress);
            cachedMessages.stream().forEach(message -> bus.publish(new CreateNeedFromMailEvent(message)));
        } else if (SubscribeStatus.UNSUBSCRIBED.equals(subscribeStatus)) {
            botContextWrapper.removeCachedMailsForMailAddress(senderMailAddress);
        }
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) SubscribeStatus(won.bot.framework.eventbot.action.impl.mail.model.SubscribeStatus) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) SubscribeUnsubscribeEvent(won.bot.framework.eventbot.event.impl.mail.SubscribeUnsubscribeEvent) EventBus(won.bot.framework.eventbot.bus.EventBus) CreateNeedFromMailEvent(won.bot.framework.eventbot.event.impl.mail.CreateNeedFromMailEvent)

Example 94 with MimeMessage

use of javax.mail.internet.MimeMessage in project webofneeds by researchstudio-sat.

the class Connect2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof ConnectFromOtherNeedEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Connection con = ((ConnectFromOtherNeedEvent) event).getCon();
        URI responseTo = con.getNeedURI();
        URI remoteNeedUri = con.getRemoteNeedURI();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Someone issued a connect for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createConnectMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(con.getConnectionURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) Connection(won.protocol.model.Connection) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) ConnectFromOtherNeedEvent(won.bot.framework.eventbot.event.impl.wonmessage.ConnectFromOtherNeedEvent)

Example 95 with MimeMessage

use of javax.mail.internet.MimeMessage in project webofneeds by researchstudio-sat.

the class Hint2MailParserAction method doRun.

@Override
protected void doRun(Event event, EventListener executingListener) throws Exception {
    EventListenerContext ctx = getEventListenerContext();
    if (event instanceof HintFromMatcherEvent && ctx.getBotContextWrapper() instanceof MailBotContextWrapper) {
        MailBotContextWrapper botContextWrapper = (MailBotContextWrapper) ctx.getBotContextWrapper();
        Match match = ((HintFromMatcherEvent) event).getMatch();
        WonMessage message = ((HintFromMatcherEvent) event).getWonMessage();
        URI responseTo = match.getFromNeed();
        URI remoteNeedUri = match.getToNeed();
        MimeMessage originalMail = botContextWrapper.getMimeMessageForURI(responseTo);
        logger.debug("Found a hint for URI: " + responseTo + " sending a mail to the creator: " + MailContentExtractor.getFromAddressString(originalMail));
        WonMimeMessage answerMessage = mailGenerator.createHintMail(originalMail, remoteNeedUri);
        botContextWrapper.addMailIdWonURIRelation(answerMessage.getMessageID(), new WonURI(message.getReceiverURI(), UriType.CONNECTION));
        sendChannel.send(new GenericMessage<>(answerMessage));
    }
}
Also used : EventListenerContext(won.bot.framework.eventbot.EventListenerContext) HintFromMatcherEvent(won.bot.framework.eventbot.event.impl.wonmessage.HintFromMatcherEvent) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) MailBotContextWrapper(won.bot.framework.bot.context.MailBotContextWrapper) MimeMessage(javax.mail.internet.MimeMessage) WonMessage(won.protocol.message.WonMessage) WonURI(won.bot.framework.eventbot.action.impl.mail.model.WonURI) URI(java.net.URI) Match(won.protocol.model.Match)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70