Search in sources :

Example 1 with MailException

use of net.jforum.exceptions.MailException in project jforum2 by rafaelsteil.

the class POPMessage method extractMessageContents.

private void extractMessageContents(Message m) throws MessagingException {
    Part messagePart = m;
    if (this.message instanceof Multipart) {
        messagePart = ((Multipart) this.message).getBodyPart(0);
    }
    if (contentType.startsWith("text/html") || contentType.startsWith("text/plain")) {
        InputStream is = null;
        BufferedReader reader = null;
        try {
            is = messagePart.getInputStream();
            is.reset();
            reader = new BufferedReader(new InputStreamReader(is));
            StringBuffer sb = new StringBuffer(512);
            int c = 0;
            char[] ch = new char[2048];
            while ((c = reader.read(ch)) != -1) {
                sb.append(ch, 0, c);
            }
            this.messageContents = sb.toString();
        } catch (IOException e) {
            throw new MailException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                }
            }
        }
    }
}
Also used : Multipart(javax.mail.Multipart) InputStreamReader(java.io.InputStreamReader) Part(javax.mail.Part) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) MailException(net.jforum.exceptions.MailException) MessagingException(javax.mail.MessagingException) IOException(java.io.IOException) MailException(net.jforum.exceptions.MailException)

Example 2 with MailException

use of net.jforum.exceptions.MailException in project jforum2 by rafaelsteil.

the class Spammer method defineUserMessage.

private void defineUserMessage(User user) {
    try {
        this.templateParams.put("user", user);
        String text = this.processTemplate();
        this.defineMessageText(text);
    } catch (Exception e) {
        throw new MailException(e);
    }
}
Also used : MailException(net.jforum.exceptions.MailException) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 3 with MailException

use of net.jforum.exceptions.MailException in project jforum2 by rafaelsteil.

the class Spammer method dispatchMessages.

public boolean dispatchMessages() {
    try {
        int sendDelay = SystemGlobals.getIntValue(ConfigKeys.MAIL_SMTP_DELAY);
        if (SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_AUTH)) {
            if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
                boolean ssl = SystemGlobals.getBoolValue(ConfigKeys.MAIL_SMTP_SSL);
                Transport transport = this.session.getTransport(ssl ? "smtps" : "smtp");
                try {
                    String host = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
                    transport.connect(host, username, password);
                    if (transport.isConnected()) {
                        for (Iterator userIter = this.users.iterator(); userIter.hasNext(); ) {
                            User user = (User) userIter.next();
                            if (this.needCustomization) {
                                this.defineUserMessage(user);
                            }
                            Address address = new InternetAddress(user.getEmail());
                            logger.debug("Sending mail to: " + user.getEmail());
                            this.message.setRecipient(Message.RecipientType.TO, address);
                            transport.sendMessage(this.message, new Address[] { address });
                            if (sendDelay > 0) {
                                try {
                                    Thread.sleep(sendDelay);
                                } catch (InterruptedException ie) {
                                    logger.error("Error while Thread.sleep." + ie, ie);
                                }
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new MailException(e);
                } finally {
                    try {
                        transport.close();
                    } catch (Exception e) {
                    }
                }
            }
        } else {
            for (Iterator iter = this.users.iterator(); iter.hasNext(); ) {
                User user = (User) iter.next();
                if (this.needCustomization) {
                    this.defineUserMessage(user);
                }
                Address address = new InternetAddress(user.getEmail());
                logger.debug("Sending mail to: " + user.getEmail());
                this.message.setRecipient(Message.RecipientType.TO, address);
                Transport.send(this.message, new Address[] { address });
                if (sendDelay > 0) {
                    try {
                        Thread.sleep(sendDelay);
                    } catch (InterruptedException ie) {
                        logger.error("Error while Thread.sleep." + ie, ie);
                    }
                }
            }
        }
    } catch (MessagingException e) {
        logger.error("Error while dispatching the message." + e, e);
    }
    return true;
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) User(net.jforum.entities.User) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Iterator(java.util.Iterator) MailException(net.jforum.exceptions.MailException) Transport(javax.mail.Transport) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 4 with MailException

use of net.jforum.exceptions.MailException in project jforum2 by rafaelsteil.

the class Spammer method prepareMessage.

/**
 * Prepares the mail message for sending.
 *
 * @param subject the subject of the email
 * @param messageFile the path to the mail message template
 * @throws MailException
 */
protected void prepareMessage(String subject, String messageFile) throws MailException {
    if (this.messageId != null) {
        this.message = new IdentifiableMimeMessage(session);
        ((IdentifiableMimeMessage) this.message).setMessageId(this.messageId);
    } else {
        this.message = new MimeMessage(session);
    }
    this.templateParams.put("forumName", SystemGlobals.getValue(ConfigKeys.FORUM_NAME));
    try {
        this.message.setSentDate(new Date());
        this.message.setFrom(new InternetAddress(SystemGlobals.getValue(ConfigKeys.MAIL_SENDER)));
        this.message.setSubject(subject, SystemGlobals.getValue(ConfigKeys.MAIL_CHARSET));
        if (this.inReplyTo != null) {
            this.message.addHeader("In-Reply-To", this.inReplyTo);
        }
        this.createTemplate(messageFile);
        this.needCustomization = this.isCustomizationNeeded();
        // then build the generic text right now
        if (!this.needCustomization) {
            String text = this.processTemplate();
            this.defineMessageText(text);
        }
    } catch (Exception e) {
        throw new MailException(e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MailException(net.jforum.exceptions.MailException) Date(java.util.Date) MessagingException(javax.mail.MessagingException) MailException(net.jforum.exceptions.MailException)

Example 5 with MailException

use of net.jforum.exceptions.MailException in project jforum2 by rafaelsteil.

the class POPConnector method openConnection.

/**
 * Opens a connection to the pop server.
 * The method will try to retrieve the <i>INBOX</i> folder in
 * <i>READ_WRITE</i> mode
 */
public void openConnection() {
    try {
        Session session = Session.getDefaultInstance(new Properties());
        this.store = session.getStore(this.mailIntegration.isSSL() ? "pop3s" : "pop3");
        this.store.connect(this.mailIntegration.getPopHost(), this.mailIntegration.getPopPort(), this.mailIntegration.getPopUsername(), this.mailIntegration.getPopPassword());
        this.folder = this.store.getFolder("INBOX");
        if (folder == null) {
            throw new Exception("No Inbox");
        }
        this.folder.open(Folder.READ_WRITE);
    } catch (Exception e) {
        throw new MailException(e);
    }
}
Also used : MailException(net.jforum.exceptions.MailException) Properties(java.util.Properties) MailException(net.jforum.exceptions.MailException) Session(javax.mail.Session)

Aggregations

MailException (net.jforum.exceptions.MailException)5 MessagingException (javax.mail.MessagingException)4 InternetAddress (javax.mail.internet.InternetAddress)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 Properties (java.util.Properties)1 Address (javax.mail.Address)1 Multipart (javax.mail.Multipart)1 Part (javax.mail.Part)1 Session (javax.mail.Session)1 Transport (javax.mail.Transport)1 MimeMessage (javax.mail.internet.MimeMessage)1 User (net.jforum.entities.User)1