Search in sources :

Example 11 with SMTPMessage

use of com.sun.mail.smtp.SMTPMessage in project zm-mailbox by Zimbra.

the class SmtpTransportTest method nullMailFrom.

@Test(timeout = 3000)
public void nullMailFrom() 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";
    SMTPMessage msg = new SMTPMessage(session, new SharedByteArrayInputStream(raw.getBytes(Charsets.ISO_8859_1)));
    // this should override the previously set mail.smtp.from
    msg.setEnvelopeFrom("<>");
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    server.shutdown(1000);
    Assert.assertEquals("EHLO localhost\r\n", server.replay());
    Assert.assertEquals("MAIL FROM:<>\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());
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) Transport(javax.mail.Transport) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) Test(org.junit.Test)

Example 12 with SMTPMessage

use of com.sun.mail.smtp.SMTPMessage in project zm-mailbox by Zimbra.

the class Notification method interceptIfNecessary.

/**
     * If <tt>zimbraInterceptAddress</tt> is specified, sends a message to that
     * address with the given message attached.
     *
     * @param operation name of the operation being performed (send, add message, save draft, etc.)
     * @param folder the folder that the message was filed into, or <tt>null</tt>
     */
void interceptIfNecessary(Mailbox mbox, MimeMessage msg, String operation, Folder folder) throws ServiceException {
    // Don't do anything if intercept is turned off.
    Account account = mbox.getAccount();
    String[] interceptAddresses = account.getMultiAttr(Provisioning.A_zimbraInterceptAddress);
    if (interceptAddresses.length == 0) {
        return;
    }
    for (String interceptAddress : interceptAddresses) {
        try {
            ZimbraLog.mailbox.info("Sending intercept of message %s to %s.", msg.getMessageID(), interceptAddress);
            // Fill templates
            String folderName = "none";
            String folderId = "none";
            if (folder != null) {
                folderName = folder.getName();
                folderId = Integer.toString(folder.getId());
            }
            Map<String, String> vars = new HashMap<String, String>();
            vars.put("ACCOUNT_DOMAIN", getDomain(account.getName()));
            vars.put("ACCOUNT_ADDRESS", account.getName());
            vars.put("MESSAGE_SUBJECT", Mime.getSubject(msg));
            vars.put("OPERATION", operation);
            vars.put("FOLDER_NAME", folderName);
            vars.put("FOLDER_ID", folderId);
            vars.put("NEWLINE", "\r\n");
            String from = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptFrom), vars);
            String subject = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptSubject), vars);
            String bodyText = StringUtil.fillTemplate(account.getAttr(Provisioning.A_zimbraInterceptBody), vars);
            // Assemble outgoing message
            MimeMessage attached = msg;
            boolean headersOnly = account.getBooleanAttr(Provisioning.A_zimbraInterceptSendHeadersOnly, false);
            if (headersOnly) {
                attached = new MimeMessageWithId(msg.getMessageID());
                Enumeration e = msg.getAllHeaderLines();
                while (e.hasMoreElements()) {
                    attached.addHeaderLine((String) e.nextElement());
                }
                attached.setContent("", msg.getContentType());
                attached.saveChanges();
            }
            SMTPMessage out = new SMTPMessage(JMSession.getSmtpSession());
            out.setHeader("Auto-Submitted", "auto-replied (zimbra; intercept)");
            InternetAddress address = new JavaMailInternetAddress(from);
            out.setFrom(address);
            address = new JavaMailInternetAddress(interceptAddress);
            out.setRecipient(javax.mail.Message.RecipientType.TO, address);
            String charset = getCharset(account, subject);
            out.setSubject(subject, charset);
            charset = getCharset(account, bodyText);
            MimeMultipart multi = new ZMimeMultipart();
            // Add message body
            MimeBodyPart part = new ZMimeBodyPart();
            part.setText(bodyText, charset);
            multi.addBodyPart(part);
            // Add original message
            MimeBodyPart part2 = new ZMimeBodyPart();
            part2.setContent(attached, MimeConstants.CT_MESSAGE_RFC822);
            multi.addBodyPart(part2);
            out.setContent(multi);
            String envFrom = "<>";
            out.setEnvelopeFrom(envFrom);
            out.saveChanges();
            Transport.send(out);
            // clean up after ourselves...
            multi.removeBodyPart(part2);
        } catch (MessagingException e) {
            ZimbraLog.lmtp.warn("Unable to send intercept message to %s.", interceptAddress, e);
        }
    }
}
Also used : Account(com.zimbra.cs.account.Account) SMTPMessage(com.sun.mail.smtp.SMTPMessage) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) Enumeration(java.util.Enumeration) HashMap(java.util.HashMap) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MessagingException(javax.mail.MessagingException) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) MimeMultipart(javax.mail.internet.MimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart)

Aggregations

SMTPMessage (com.sun.mail.smtp.SMTPMessage)12 MessagingException (javax.mail.MessagingException)8 InternetAddress (javax.mail.internet.InternetAddress)8 JavaMailInternetAddress (com.zimbra.common.mime.shim.JavaMailInternetAddress)6 Date (java.util.Date)6 ZMimeBodyPart (com.zimbra.common.zmime.ZMimeBodyPart)5 ZMimeMultipart (com.zimbra.common.zmime.ZMimeMultipart)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MimeMessage (javax.mail.internet.MimeMessage)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 Account (com.zimbra.cs.account.Account)3 Locale (java.util.Locale)3 Address (javax.mail.Address)3 Session (javax.mail.Session)3 IOException (java.io.IOException)2 DataHandler (javax.activation.DataHandler)2 Transport (javax.mail.Transport)2 AddressException (javax.mail.internet.AddressException)2 ServiceException (com.zimbra.common.service.ServiceException)1 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)1