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());
}
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());
}
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;
}
Aggregations