use of javax.mail.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method mailSmtpFrom.
@Test(timeout = 3000)
public void mailSmtpFrom() 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").sendLine("250 OK").recvLine().sendLine("221 bye").build().start(PORT);
Session session = JMSession.getSession();
session.getProperties().setProperty("mail.smtp.from", "from@zimbra.com");
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)));
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
server.shutdown(1000);
Assert.assertEquals("EHLO localhost\r\n", server.replay());
Assert.assertEquals("MAIL FROM:<from@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.assertEquals("QUIT\r\n", server.replay());
Assert.assertNull(server.replay());
}
use of javax.mail.Session 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.Session 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.Session in project zm-mailbox by Zimbra.
the class SmtpTransportTest method rset.
@Test(timeout = 3000)
public void rset() throws Exception {
server = MockTcpServer.scenario().sendLine("220 test ready").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").recvLine().sendLine("250 OK").build().start(PORT);
Session session = JMSession.getSession();
SmtpTransport transport = (SmtpTransport) session.getTransport("smtp");
try {
transport.connect("localhost", PORT, null, null);
transport.mail("sender@zimbra.com");
transport.rset();
} 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("RSET\r\n", server.replay());
Assert.assertNull(server.replay());
}
use of javax.mail.Session in project zm-mailbox by Zimbra.
the class Notification method notifyIfNecessary.
/**
* If the recipient's account is set up for email notification, sends a notification
* message to the user's notification address.
*/
private void notifyIfNecessary(Account account, Message msg, String rcpt) throws MessagingException, ServiceException {
// Does user have new mail notification turned on
boolean notify = account.getBooleanAttr(Provisioning.A_zimbraPrefNewMailNotificationEnabled, false);
if (!notify) {
return;
}
// Validate notification address
String destination = account.getAttr(Provisioning.A_zimbraPrefNewMailNotificationAddress);
if (destination == null) {
nfailed("destination not set", null, rcpt, msg, null);
return;
}
try {
new JavaMailInternetAddress(destination);
} catch (AddressException ae) {
nfailed("invalid destination", destination, rcpt, msg, ae);
return;
}
// filter rules, we assume it's not interesting.
if (msg.inSpam()) {
nfailed("in spam", destination, rcpt, msg);
return;
}
try {
if (msg.inTrash()) {
nfailed("in trash", destination, rcpt, msg);
return;
}
} catch (ServiceException e) {
nfailed("call to Message.inTrash() failed", destination, rcpt, msg, e);
return;
}
// If precedence is bulk or junk
MimeMessage mm = msg.getMimeMessage();
String[] precedence = mm.getHeader("Precedence");
if (hasPrecedence(precedence, "bulk")) {
nfailed("precedence bulk", destination, rcpt, msg);
return;
}
if (hasPrecedence(precedence, "junk")) {
nfailed("precedence junk", destination, rcpt, msg);
return;
}
// Check for mail loop
String[] autoSubmittedHeaders = mm.getHeader("Auto-Submitted");
if (autoSubmittedHeaders != null) {
for (int i = 0; i < autoSubmittedHeaders.length; i++) {
String headerValue = autoSubmittedHeaders[i].toLowerCase();
if (headerValue.indexOf("notification") != -1) {
nfailed("detected a mail loop", destination, rcpt, msg);
return;
}
}
}
// Send the message
try {
Session smtpSession = JMSession.getSmtpSession();
// Assemble message components
MimeMessage out = assembleNotificationMessage(account, msg, rcpt, destination, smtpSession);
if (out == null) {
return;
}
String envFrom = "<>";
try {
if (!Provisioning.getInstance().getConfig().getBooleanAttr(Provisioning.A_zimbraAutoSubmittedNullReturnPath, true)) {
envFrom = account.getName();
}
} catch (ServiceException se) {
ZimbraLog.mailbox.warn("error encoutered looking up return path configuration, using null return path instead", se);
}
smtpSession.getProperties().setProperty("mail.smtp.from", envFrom);
Transport.send(out);
ZimbraLog.mailbox.info("notification sent dest='" + destination + "' rcpt='" + rcpt + "' mid=" + msg.getId());
} catch (MessagingException me) {
nfailed("send failed", destination, rcpt, msg, me);
}
}
Aggregations