use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.
the class TestMailSender method testRejectRecipient.
@Test
public void testRejectRecipient() throws Exception {
String errorMsg = "Sender address rejected: User unknown in relay recipient table";
String bogusAddress = TestUtil.getAddress("bogus");
startDummySmtpServer(bogusAddress, errorMsg);
Server server = Provisioning.getInstance().getLocalServer();
server.setSmtpPort(TEST_SMTP_PORT);
String content = TestUtil.getTestMessage(NAME_PREFIX + " testRejectSender", bogusAddress, SENDER_NAME, null);
MimeMessage msg = new ZMimeMessage(JMSession.getSession(), new SharedByteArrayInputStream(content.getBytes()));
Mailbox mbox = TestUtil.getMailbox(SENDER_NAME);
// Test reject first recipient, get partial send value from LDAP.
boolean sendFailed = false;
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject first recipient, set partial send value explicitly.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
server.setSmtpSendPartial(true);
MailSender sender = mbox.getMailSender().setSendPartial(false);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test reject second recipient, get partial send value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
String validAddress = TestUtil.getAddress(RECIPIENT_NAME);
InternetAddress[] recipients = new InternetAddress[2];
recipients[0] = new JavaMailInternetAddress(validAddress);
recipients[1] = new JavaMailInternetAddress(bogusAddress);
msg.setRecipients(MimeMessage.RecipientType.TO, recipients);
server.setSmtpSendPartial(false);
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_ABORTED_ADDRESS_FAILURE, bogusAddress, errorMsg);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, get value from LDAP.
startDummySmtpServer(bogusAddress, errorMsg);
server.setSmtpSendPartial(true);
sendFailed = false;
try {
mbox.getMailSender().sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
// Test partial send, specify value explicitly.
server.setSmtpSendPartial(false);
startDummySmtpServer(bogusAddress, errorMsg);
sendFailed = false;
sender = mbox.getMailSender().setSendPartial(true);
try {
sender.sendMimeMessage(null, mbox, msg);
} catch (MailServiceException e) {
// Don't check error message. JavaMail does not give us the SMTP protocol error in the
// partial send case.
validateException(e, MailServiceException.SEND_PARTIAL_ADDRESS_FAILURE, bogusAddress, null);
sendFailed = true;
}
Assert.assertTrue(sendFailed);
}
Aggregations