001    /*
002     * Copyright 2010 the original author or authors.
003     * 
004     *  Licensed under the Apache License, Version 2.0 (the "License");
005     *  you may not use this file except in compliance with the License.
006     *  You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     *  Unless required by applicable law or agreed to in writing, software
011     *  distributed under the License is distributed on an "AS IS" BASIS,
012     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     *  See the License for the specific language governing permissions and
014     *  limitations under the License.
015     */
016    package com.hs.mail.smtp.message;
017    
018    import java.util.Date;
019    
020    import javax.mail.Message;
021    import javax.mail.MessagingException;
022    import javax.mail.Multipart;
023    import javax.mail.Session;
024    import javax.mail.internet.ContentType;
025    import javax.mail.internet.InternetAddress;
026    import javax.mail.internet.MimeBodyPart;
027    import javax.mail.internet.MimeMessage;
028    import javax.mail.internet.MimeMultipart;
029    
030    import com.hs.mail.container.config.Config;
031    
032    /**
033     * 
034     * @author Won Chul Doh
035     * @since Jun 17, 2010
036     *
037     */
038    public class DeliveryStatusNotifier {
039            
040            private static String SORRY_FOR_DELIVERY_ERROR = "I'm afraid I wasn't able to deliver your message to the following addresses.\r\n"
041                            + "This is a permanent error; I've given up.\r\n"
042                            + "Below I included the list of recipients and the reason why I was unable to deliver your message.\r\n\r\n";
043            
044            public static void dsnNotify(String from, MailAddress to, MimeMessage msg,
045                            String textMessage) {
046                    // To prevent loops in error reporting, we specify reverse-path to null.
047                    SmtpMessage dsnmsg = new SmtpMessage(new MailAddress());
048                    dsnmsg.addRecipient(new Recipient(to.getMailbox(), false));
049                    try {
050                            dsnmsg.setContent(createMimeMessage(from, to, msg, textMessage));
051                            dsnmsg.store();
052                            dsnmsg.createTrigger();
053                    } catch (Exception e) {
054                            dsnmsg.dispose();
055                    }
056            }
057            
058            private static MimeMessage createMimeMessage(String from, MailAddress to,
059                            MimeMessage msg, String textMessage) throws MessagingException {
060                    Session session = Session.getInstance(System.getProperties(), null);
061                    MimeMessage dsn = new MimeMessage(session);
062                    dsn.setFrom(new InternetAddress((from != null) ? from : Config
063                                    .getPostmaster()));
064                    InternetAddress[] toAddr = { to.toInternetAddress() };
065                    dsn.setRecipients(Message.RecipientType.TO, toAddr);
066                    String subject = msg.getHeader("Subject", null);
067                    if (subject != null) {
068                            if (from != null && !subject.regionMatches(true, 0, "Re: ", 0, 4))
069                                    dsn.setHeader("Subject", "Re: " + subject);
070                            else if (from == null
071                                            && !subject.regionMatches(true, 0, "[Err] ", 0, 6))
072                                    dsn.setHeader("Subject", "[Err] " + subject);
073                    }
074                    dsn.setSentDate(new Date());
075                    Multipart mp = new MultipartReport((from != null) ? textMessage
076                                    : SORRY_FOR_DELIVERY_ERROR + textMessage, msg);
077                    dsn.setContent(mp);
078                    return dsn;
079            }
080    
081            public static class MultipartReport extends MimeMultipart {
082                    public MultipartReport(String text, MimeMessage msg)
083                                    throws MessagingException {
084                            super("report");
085                            ContentType ct = new ContentType(contentType);
086                            ct.setParameter("report-type", "delivery-status");
087                            contentType = ct.toString();
088                            MimeBodyPart mbp = new MimeBodyPart();
089                            mbp.setText(text);
090                            addBodyPart(mbp);
091                            // TODO delivery-status part must be included.
092                            if (msg != null) {
093                                    // Attach the original message.
094                                    mbp = new MimeBodyPart();
095                                    mbp.setContent(msg, "message/rfc822");
096                                    addBodyPart(mbp);
097                            }
098                    }
099            }
100    
101    }