Search in sources :

Example 16 with MessagingException

use of javax.mail.MessagingException 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 17 with MessagingException

use of javax.mail.MessagingException 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 18 with MessagingException

use of javax.mail.MessagingException in project spring-framework by spring-projects.

the class JavaMailSenderImpl method send.

@Override
public void send(MimeMessagePreparator... mimeMessagePreparators) throws MailException {
    try {
        List<MimeMessage> mimeMessages = new ArrayList<>(mimeMessagePreparators.length);
        for (MimeMessagePreparator preparator : mimeMessagePreparators) {
            MimeMessage mimeMessage = createMimeMessage();
            preparator.prepare(mimeMessage);
            mimeMessages.add(mimeMessage);
        }
        send(mimeMessages.toArray(new MimeMessage[mimeMessages.size()]));
    } catch (MailException ex) {
        throw ex;
    } catch (MessagingException ex) {
        throw new MailParseException(ex);
    } catch (Exception ex) {
        throw new MailPreparationException(ex);
    }
}
Also used : MailPreparationException(org.springframework.mail.MailPreparationException) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) ArrayList(java.util.ArrayList) MailParseException(org.springframework.mail.MailParseException) MailException(org.springframework.mail.MailException) MailParseException(org.springframework.mail.MailParseException) MessagingException(javax.mail.MessagingException) MailAuthenticationException(org.springframework.mail.MailAuthenticationException) MailSendException(org.springframework.mail.MailSendException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) NoSuchProviderException(javax.mail.NoSuchProviderException) MailPreparationException(org.springframework.mail.MailPreparationException) MailException(org.springframework.mail.MailException)

Example 19 with MessagingException

use of javax.mail.MessagingException in project spring-framework by spring-projects.

the class MimeMessageHelper method parseAddress.

private InternetAddress parseAddress(String address) throws MessagingException {
    InternetAddress[] parsed = InternetAddress.parse(address);
    if (parsed.length != 1) {
        throw new AddressException("Illegal address", address);
    }
    InternetAddress raw = parsed[0];
    try {
        return (getEncoding() != null ? new InternetAddress(raw.getAddress(), raw.getPersonal(), getEncoding()) : raw);
    } catch (UnsupportedEncodingException ex) {
        throw new MessagingException("Failed to parse embedded personal name to correct encoding", ex);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) AddressException(javax.mail.internet.AddressException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 20 with MessagingException

use of javax.mail.MessagingException in project spring-boot by spring-projects.

the class MailHealthIndicatorTests method smtpIsDown.

@Test
public void smtpIsDown() throws MessagingException {
    willThrow(new MessagingException("A test exception")).given(this.mailSender).testConnection();
    Health health = this.indicator.health();
    assertThat(health.getStatus()).isEqualTo(Status.DOWN);
    assertThat(health.getDetails().get("location")).isEqualTo("smtp.acme.org:25");
    Object errorMessage = health.getDetails().get("error");
    assertThat(errorMessage).isNotNull();
    assertThat(errorMessage.toString().contains("A test exception")).isTrue();
}
Also used : MessagingException(javax.mail.MessagingException) Test(org.junit.Test)

Aggregations

MessagingException (javax.mail.MessagingException)324 MimeMessage (javax.mail.internet.MimeMessage)126 IOException (java.io.IOException)125 InternetAddress (javax.mail.internet.InternetAddress)64 MimeMultipart (javax.mail.internet.MimeMultipart)60 MimeBodyPart (javax.mail.internet.MimeBodyPart)58 Message (javax.mail.Message)45 Session (javax.mail.Session)43 InputStream (java.io.InputStream)41 Properties (java.util.Properties)38 Date (java.util.Date)35 ArrayList (java.util.ArrayList)33 PackageException (com.axway.ats.action.objects.model.PackageException)32 Address (javax.mail.Address)32 NoSuchMimePackageException (com.axway.ats.action.objects.model.NoSuchMimePackageException)31 PublicAtsApi (com.axway.ats.common.PublicAtsApi)31 ServiceException (com.zimbra.common.service.ServiceException)30 ByteArrayInputStream (java.io.ByteArrayInputStream)25 DataHandler (javax.activation.DataHandler)23 BodyPart (javax.mail.BodyPart)23