Search in sources :

Example 21 with SendFailedException

use of javax.mail.SendFailedException in project zm-mailbox by Zimbra.

the class SmtpTransportTest method rcptToError.

@Test(timeout = 3000)
public void rcptToError() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("550 error").recvLine().build().start(PORT);
    Session session = JMSession.getSession();
    session.getProperties().setProperty("mail.smtp.sendpartial", "true");
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException e) {
        Assert.assertEquals(0, e.getValidSentAddresses().length);
        Assert.assertEquals(0, e.getValidUnsentAddresses().length);
        Assert.assertEquals(1, e.getInvalidAddresses().length);
    } finally {
        transport.close();
    }
    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("QUIT\r\n", server.replay());
    Assert.assertNull(server.replay());
}
Also used : SendFailedException(javax.mail.SendFailedException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 22 with SendFailedException

use of javax.mail.SendFailedException in project zm-mailbox by Zimbra.

the class SmtpTransportTest method dataException.

@Test(timeout = 3000)
public void dataException() throws Exception {
    server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("354 OK").swallowUntil("\r\n.\r\n").build().start(PORT);
    Session session = JMSession.getSession();
    Transport transport = session.getTransport("smtp");
    transport.connect("localhost", PORT, null, null);
    String raw = "From: sender@zimbra.com\nTo: rcpt@zimbra.com\nSubject: test\n\ntest";
    MimeMessage msg = new ZMimeMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1))) {

        @Override
        public void writeTo(OutputStream os, String[] ignoreList) throws MessagingException {
            // exception while encoding
            throw new MessagingException();
        }
    };
    try {
        transport.sendMessage(msg, msg.getAllRecipients());
        Assert.fail();
    } catch (SendFailedException expected) {
    } finally {
        transport.close();
    }
    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<sender@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("RCPT TO:<rcpt@zimbra.com>\r\n", server.replay());
    Assert.assertEquals("DATA\r\n", server.replay());
    Assert.assertNull(server.replay());
}
Also used : SendFailedException(javax.mail.SendFailedException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) OutputStream(java.io.OutputStream) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 23 with SendFailedException

use of javax.mail.SendFailedException in project pentaho-platform by pentaho.

the class Emailer method send.

public boolean send() {
    String from = props.getProperty("mail.from.default");
    String fromName = props.getProperty("mail.from.name");
    String to = props.getProperty("to");
    String cc = props.getProperty("cc");
    String bcc = props.getProperty("bcc");
    boolean authenticate = "true".equalsIgnoreCase(props.getProperty("mail.smtp.auth"));
    String subject = props.getProperty("subject");
    String body = props.getProperty("body");
    logger.info("Going to send an email to " + to + " from " + from + " with the subject '" + subject + "' and the body " + body);
    try {
        // Get a Session object
        Session session;
        if (authenticate) {
            session = Session.getInstance(props, authenticator);
        } else {
            session = Session.getInstance(props);
        }
        // if debugging is not set in the email config file, then default to false
        if (!props.containsKey("mail.debug")) {
            // $NON-NLS-1$
            session.setDebug(false);
        }
        final MimeMessage msg;
        if (EMBEDDED_HTML.equals(attachmentMimeType)) {
            // Message is ready
            msg = attachment != null ? new MimeMessage(session, attachment) : new MimeMessage(session);
            if (body != null) {
                // We need to add message to the top of the email body
                final MimeMultipart oldMultipart = (MimeMultipart) msg.getContent();
                final MimeMultipart newMultipart = new MimeMultipart("related");
                for (int i = 0; i < oldMultipart.getCount(); i++) {
                    BodyPart bodyPart = oldMultipart.getBodyPart(i);
                    final Object content = bodyPart.getContent();
                    // Main HTML body
                    if (content instanceof String) {
                        final String newContent = body + "<br/><br/>" + content;
                        final MimeBodyPart part = new MimeBodyPart();
                        part.setText(newContent, "UTF-8", "html");
                        newMultipart.addBodyPart(part);
                    } else {
                        // CID attachments
                        newMultipart.addBodyPart(bodyPart);
                    }
                }
                msg.setContent(newMultipart);
            }
        } else {
            // construct the message
            msg = new MimeMessage(session);
            Multipart multipart = new MimeMultipart();
            if (body != null) {
                MimeBodyPart bodyMessagePart = new MimeBodyPart();
                bodyMessagePart.setText(body, LocaleHelper.getSystemEncoding());
                multipart.addBodyPart(bodyMessagePart);
            }
            if (attachment != null) {
                ByteArrayDataSource dataSource = new ByteArrayDataSource(attachment, attachmentMimeType);
                // attach the file to the message
                MimeBodyPart attachmentBodyPart = new MimeBodyPart();
                attachmentBodyPart.setDataHandler(new DataHandler(dataSource));
                attachmentBodyPart.setFileName(attachmentName);
                multipart.addBodyPart(attachmentBodyPart);
            }
            // add the Multipart to the message
            msg.setContent(multipart);
        }
        if (from != null) {
            msg.setFrom(new InternetAddress(from, fromName));
        } else {
            // There should be no way to get here
            // $NON-NLS-1$
            logger.error("Email.ERROR_0012_FROM_NOT_DEFINED");
        }
        if ((to != null) && (to.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
        }
        if ((cc != null) && (cc.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc, false));
        }
        if ((bcc != null) && (bcc.trim().length() > 0)) {
            msg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(bcc, false));
        }
        if (subject != null) {
            msg.setSubject(subject, LocaleHelper.getSystemEncoding());
        }
        // $NON-NLS-1$
        msg.setHeader("X-Mailer", Emailer.MAILER);
        msg.setSentDate(new Date());
        Transport.send(msg);
        return true;
    } catch (SendFailedException e) {
        // $NON-NLS-1$
        logger.error("Email.ERROR_0011_SEND_FAILED -" + to, e);
    } catch (AuthenticationFailedException e) {
        // $NON-NLS-1$
        logger.error("Email.ERROR_0014_AUTHENTICATION_FAILED - " + to, e);
    } catch (Throwable e) {
        // $NON-NLS-1$
        logger.error("Email.ERROR_0011_SEND_FAILED - " + to, e);
    }
    return false;
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) Multipart(javax.mail.Multipart) MimeMultipart(javax.mail.internet.MimeMultipart) InternetAddress(javax.mail.internet.InternetAddress) SendFailedException(javax.mail.SendFailedException) AuthenticationFailedException(javax.mail.AuthenticationFailedException) DataHandler(javax.activation.DataHandler) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource) Session(javax.mail.Session)

Aggregations

SendFailedException (javax.mail.SendFailedException)23 MimeMessage (javax.mail.internet.MimeMessage)15 Session (javax.mail.Session)14 MessagingException (javax.mail.MessagingException)13 Transport (javax.mail.Transport)11 InternetAddress (javax.mail.internet.InternetAddress)11 IOException (java.io.IOException)9 MimeBodyPart (javax.mail.internet.MimeBodyPart)8 MimeMultipart (javax.mail.internet.MimeMultipart)8 Properties (java.util.Properties)7 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)6 JMSession (com.zimbra.cs.util.JMSession)5 Date (java.util.Date)5 DataHandler (javax.activation.DataHandler)5 Address (javax.mail.Address)5 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)5 Test (org.junit.Test)4 SMTPMessage (com.sun.mail.smtp.SMTPMessage)3 File (java.io.File)3 OutputStream (java.io.OutputStream)3