Search in sources :

Example 41 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class JMSessionTest method testRelayMta.

// @Test
public void testRelayMta() throws Exception {
    Provisioning prov = Provisioning.getInstance();
    Server server = prov.getLocalServer();
    server.setShareNotificationMtaHostname("mta02.zimbra.com");
    server.setShareNotificationMtaPort(25);
    server.setShareNotificationMtaAuthRequired(true);
    server.setShareNotificationMtaConnectionType(ShareNotificationMtaConnectionType.STARTTLS);
    server.setShareNotificationMtaAuthAccount("test-jylee");
    server.setShareNotificationMtaAuthPassword("test123");
    SMTPMessage out = new SMTPMessage(JMSession.getRelaySession());
    InternetAddress address = new JavaMailInternetAddress("test-jylee@zimbra.com");
    out.setFrom(address);
    address = new JavaMailInternetAddress("test-jylee@zimbra.com");
    out.setRecipient(javax.mail.Message.RecipientType.TO, address);
    out.setSubject("test mail");
    out.setText("hello world");
    out.saveChanges();
    ZimbraLog.smtp.setLevel(Level.trace);
    Transport.send(out);
}
Also used : SMTPMessage(com.sun.mail.smtp.SMTPMessage) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) InternetAddress(javax.mail.internet.InternetAddress) Server(com.zimbra.cs.account.Server) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MockProvisioning(com.zimbra.cs.account.MockProvisioning) Provisioning(com.zimbra.cs.account.Provisioning)

Example 42 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class ZimbraMailAdapter method handleIDN.

private List<String> handleIDN(String headerName, String[] headers) {
    List<String> hdrs = new ArrayList<String>();
    for (String header : headers) {
        boolean altered = false;
        if (header.contains(IDNUtil.ACE_PREFIX)) {
            // handle multiple addresses in a header
            StringTokenizer st = new StringTokenizer(header, ",;", true);
            StringBuffer addrs = new StringBuffer();
            while (st.hasMoreTokens()) {
                String address = st.nextToken();
                String delim = st.hasMoreTokens() ? st.nextToken() : "";
                try {
                    javax.mail.internet.InternetAddress inetAddr = new JavaMailInternetAddress(address);
                    String addr = inetAddr.getAddress();
                    String unicodeAddr = IDNUtil.toUnicode(addr);
                    if (unicodeAddr.equalsIgnoreCase(addr)) {
                        addrs.append(address).append(delim);
                    } else {
                        altered = true;
                        // put the unicode addr back to the address
                        inetAddr.setAddress(unicodeAddr);
                        addrs.append(inetAddr.toString()).append(delim);
                    }
                } catch (AddressException e) {
                    ZimbraLog.filter.warn("handleIDN encountered invalid address " + address + "in header " + headerName);
                    // put back the orig address
                    addrs.append(address).append(delim);
                }
            }
            // if altered, add the altered value
            if (altered) {
                String unicodeAddrs = addrs.toString();
                ZimbraLog.filter.debug("handleIDN added value " + unicodeAddrs + " for header " + headerName);
                hdrs.add(unicodeAddrs);
            }
        }
        // always put back the orig value
        hdrs.add(header);
    }
    return hdrs;
}
Also used : StringTokenizer(java.util.StringTokenizer) AddressException(javax.mail.internet.AddressException) ArrayList(java.util.ArrayList) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress)

Example 43 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class FilterUtil method reply.

public static void reply(OperationContext octxt, Mailbox mailbox, ParsedMessage parsedMessage, String bodyTemplate) throws MessagingException, ServiceException {
    MimeMessage mimeMessage = parsedMessage.getMimeMessage();
    if (isMailLoop(mailbox, mimeMessage, new String[] { HEADER_FORWARDED })) {
        String error = String.format("Detected a mail loop for message %s.", Mime.getMessageID(mimeMessage));
        throw ServiceException.FAILURE(error, null);
    }
    if (isDeliveryStatusNotification(mimeMessage)) {
        ZimbraLog.filter.debug("Not auto-replying to a DSN message");
        return;
    }
    Account account = mailbox.getAccount();
    MimeMessage replyMsg = new Mime.FixedMimeMessage(JMSession.getSmtpSession(account));
    // add the forwarded header account names to detect the mail loop between accounts
    for (String headerFwdAccountName : Mime.getHeaders(mimeMessage, HEADER_FORWARDED)) {
        replyMsg.addHeader(HEADER_FORWARDED, headerFwdAccountName);
    }
    replyMsg.addHeader(HEADER_FORWARDED, account.getName());
    String to = mimeMessage.getHeader("Reply-To", null);
    if (StringUtil.isNullOrEmpty(to))
        to = Mime.getSender(mimeMessage);
    if (StringUtil.isNullOrEmpty(to))
        throw new MessagingException("Can't locate the address to reply to");
    replyMsg.setRecipient(javax.mail.Message.RecipientType.TO, new JavaMailInternetAddress(to));
    String subject = mimeMessage.getSubject();
    if (subject == null) {
        subject = "";
    }
    String replySubjectPrefix = L10nUtil.getMessage(L10nUtil.MsgKey.replySubjectPrefix, account.getLocale());
    if (!subject.toLowerCase().startsWith(replySubjectPrefix.toLowerCase())) {
        subject = replySubjectPrefix + " " + subject;
    }
    replyMsg.setSubject(subject, getCharset(account, subject));
    // getVarsMap() result is now being passed to replaceVariables(), so following call is redundant
    // Map<String, String> vars = getVarsMap(mailbox, parsedMessage, mimeMessage);
    replyMsg.setText(bodyTemplate, getCharset(account, bodyTemplate));
    String origMsgId = mimeMessage.getMessageID();
    if (!StringUtil.isNullOrEmpty(origMsgId))
        replyMsg.setHeader("In-Reply-To", origMsgId);
    replyMsg.setSentDate(new Date());
    replyMsg.saveChanges();
    MailSender mailSender = mailbox.getMailSender();
    mailSender.setReplyType(MailSender.MSGTYPE_REPLY);
    mailSender.setDsnNotifyOptions(MailSender.DsnNotifyOption.NEVER);
    mailSender.sendMimeMessage(octxt, mailbox, replyMsg);
}
Also used : Account(com.zimbra.cs.account.Account) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) MailSender(com.zimbra.cs.mailbox.MailSender) Date(java.util.Date)

Example 44 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class ForwardCalendarItem method makeFwdMsg.

private static Pair<MimeMessage, MimeMessage> makeFwdMsg(Account senderAcct, Invite inv, MimeMessage mmInv, ZVCalendar cal, MimeMessage mmFwdWrapper, MimeBodyPart plainDesc, MimeBodyPart htmlDesc, boolean useFwdText) throws ServiceException, MessagingException, IOException {
    // Set SENT-BY to sender's email address.  Required by Outlook.
    // Also, set ATTENDEEs to the forwardees.  For consistency with Outlook.
    setSentByAndAttendees(cal, AccountUtil.getFriendlyEmailAddress(senderAcct).getAddress(), mmFwdWrapper.getAllRecipients());
    // From: and Sender: headers
    Address from = null;
    Address sender = null;
    sender = AccountUtil.getFriendlyEmailAddress(senderAcct);
    ZOrganizer org = inv.getOrganizer();
    if (org != null) {
        if (org.hasCn())
            from = new JavaMailInternetAddress(org.getAddress(), org.getCn(), MimeConstants.P_CHARSET_UTF8);
        else
            from = new JavaMailInternetAddress(org.getAddress());
    } else {
        from = sender;
    }
    MimeMessage mm;
    if (useFwdText) {
        String plainDescStr = null;
        String htmlDescStr = null;
        if (plainDesc != null)
            plainDescStr = (String) plainDesc.getContent();
        if (htmlDesc != null)
            htmlDescStr = (String) htmlDesc.getContent();
        setDescProps(cal, plainDescStr, htmlDescStr);
        mm = createMergedMessage(senderAcct, from, sender, mmInv, inv, cal, plainDesc, htmlDesc);
    } else {
        mm = CalendarMailSender.createCalendarMessage(senderAcct, from, sender, null, mmInv, inv, cal, false);
    }
    // Copy recipient headers from forward wrapper msg.
    RecipientType[] rcptTypes = { RecipientType.TO, RecipientType.CC, RecipientType.BCC };
    for (RecipientType rcptType : rcptTypes) {
        Address[] rcpts = mmFwdWrapper.getRecipients(rcptType);
        mm.setRecipients(rcptType, rcpts);
    }
    mm.setSubject(mmFwdWrapper.getSubject());
    mm.saveChanges();
    // Create a Forward Notification message if the invitation is originally from ZCS user.
    MimeMessage notifyMimeMsg = null;
    if (org != null) {
        String orgAddress = org.getAddress();
        Account orgAccount = Provisioning.getInstance().getAccountByName(orgAddress);
        if (orgAccount != null && !orgAccount.getId().equals(senderAcct.getId())) {
            notifyMimeMsg = CalendarMailSender.createForwardNotifyMessage(senderAcct, orgAccount, orgAddress, mmFwdWrapper.getAllRecipients(), inv);
        }
    }
    return new Pair<MimeMessage, MimeMessage>(mm, notifyMimeMsg);
}
Also used : Account(com.zimbra.cs.account.Account) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) ZOrganizer(com.zimbra.cs.mailbox.calendar.ZOrganizer) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) RecipientType(javax.mail.Message.RecipientType) Pair(com.zimbra.common.util.Pair)

Example 45 with JavaMailInternetAddress

use of com.zimbra.common.mime.shim.JavaMailInternetAddress in project zm-mailbox by Zimbra.

the class MessageBuilder method create.

public String create() throws MessagingException, ServiceException, IOException {
    if (toRecipient == null) {
        toRecipient = "user1";
    }
    if (from == null) {
        from = "jspiccoli";
    }
    if (date == null) {
        date = new Date();
    }
    if (contentType == null) {
        contentType = MimeConstants.CT_TEXT_PLAIN;
    }
    if (body == null) {
        body = MessageBuilder.DEFAULT_MESSAGE_BODY;
    }
    from = TestUtil.addDomainIfNecessary(from);
    toRecipient = TestUtil.addDomainIfNecessary(toRecipient);
    sender = TestUtil.addDomainIfNecessary(sender);
    MimeMessage msg = addMessageIdHeader ? new ZMimeMessage(JMSession.getSession()) : new MimeMessageWithNoId();
    msg.setRecipient(RecipientType.TO, new JavaMailInternetAddress(toRecipient));
    if (ccRecipient != null) {
        ccRecipient = TestUtil.addDomainIfNecessary(ccRecipient);
        msg.setRecipient(RecipientType.CC, new JavaMailInternetAddress(ccRecipient));
    }
    msg.setFrom(new JavaMailInternetAddress(from));
    if (sender != null) {
        msg.setSender(new JavaMailInternetAddress(sender));
    }
    msg.setSentDate(date);
    msg.setSubject(subject);
    if (attachment == null) {
        // Need to specify the data handler explicitly because JavaMail
        // doesn't know what to do with text/enriched.
        msg.setDataHandler(new DataHandler(new ByteArrayDataSource(body.getBytes(), contentType)));
    } else {
        MimeMultipart multi = new ZMimeMultipart("mixed");
        MimeBodyPart body = new ZMimeBodyPart();
        body.setDataHandler(new DataHandler(new ByteArrayDataSource(this.body.getBytes(), contentType)));
        multi.addBodyPart(body);
        MimeBodyPart attachment = new ZMimeBodyPart();
        attachment.setContent(this.attachment, attachmentContentType);
        attachment.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);
        multi.addBodyPart(attachment);
        msg.setContent(multi);
    }
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    msg.writeTo(out);
    return new String(out.toByteArray());
}
Also used : ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) DataHandler(javax.activation.DataHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(java.util.Date) ZMimeMessage(com.zimbra.common.zmime.ZMimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) JavaMailInternetAddress(com.zimbra.common.mime.shim.JavaMailInternetAddress) ZMimeMultipart(com.zimbra.common.zmime.ZMimeMultipart) ZMimeBodyPart(com.zimbra.common.zmime.ZMimeBodyPart) MimeBodyPart(javax.mail.internet.MimeBodyPart) ByteArrayDataSource(javax.mail.util.ByteArrayDataSource)

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