Search in sources :

Example 46 with JavaMailInternetAddress

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);
}
Also used : JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) Server(com.zimbra.cs.account.Server) Mailbox(com.zimbra.cs.mailbox.Mailbox) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) FixedMimeMessage(com.zimbra.cs.mime.Mime.FixedMimeMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Aggregations

JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)46 InternetAddress (javax.mail.internet.InternetAddress)31 MimeMessage (javax.mail.internet.MimeMessage)25 AddressException (javax.mail.internet.AddressException)17 Date (java.util.Date)15 MessagingException (javax.mail.MessagingException)14 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)13 Account (com.zimbra.cs.account.Account)12 MailSender (com.zimbra.cs.mailbox.MailSender)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 ArrayList (java.util.ArrayList)9 Address (javax.mail.Address)9 ServiceException (com.zimbra.common.service.ServiceException)8 Locale (java.util.Locale)8 SMTPMessage (com.sun.mail.smtp.SMTPMessage)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)6 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)6 IOException (java.io.IOException)6 MimeMultipart (javax.mail.internet.MimeMultipart)6