cocoon-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Niclas Hedhman <nic...@hedhman.org>
Subject Re: Attachments within sendmail
Date Wed, 29 Jan 2003 02:01:49 GMT

Here is my code for making multiple attachments (in Java though, not XSP and 
don't know if there are any "issues" of XSP involved)

htmlContent contains the HTML text.
xmlContent contains the XML text.
qtxcontent is a text file in proprietary format

                connectTransport();
                MimeMultipart mmp = new MimeMultipart();
                InternetHeaders headers;
                MimeBodyPart mbp;
                String[] images = getImageNames();

                mbp = new MimeBodyPart();
                mbp.setContent( description, "text/plain" );
                mmp.addBodyPart( mbp );

                mbp = new MimeBodyPart();
                mbp.setDataHandler(new DataHandler( new ByteArrayDataSource( 
htmlContent, "text/html")));
                mbp.setHeader( "Content-Disposition", 
"inline;\n\tfilename=\"ewarna.com.html\"" );
                mmp.addBodyPart( mbp );

                headers = new InternetHeaders();
                headers.setHeader( "Content-Type", 
"application/octet-stream;\n\tname=\"color.qtx\"" );
                headers.setHeader( "Content-Transfer-Encoding", 
"quoted-printable" );
                headers.setHeader( "Content-Disposition", 
"attachment;\n\tfilename=\"color.qtx\"" );

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                OutputStream out = MimeUtility.encode( baos, 
"quoted-printable" );
                out.write( qtxcontent.getBytes() );

                mbp = new MimeBodyPart( headers, baos.toByteArray() );
                mmp.addBodyPart( mbp );

                mbp = new MimeBodyPart();
                mbp.setDataHandler(new DataHandler( new ByteArrayDataSource( 
xmlContent, "text/xml")));
                mbp.setHeader( "Content-Disposition", 
"attachment;\n\tfilename=\"ewarna.com.xml\"" );
                mmp.addBodyPart( mbp );

                MimeMessage msg = new MimeMessage( m_Session );
                if( from != null )
                {
                    msg.setFrom( from );
                    msg.setReplyTo( new Address[] { from } );
                    m_Logger.debug( "Creating an email from " + 
msg.getFrom()[0] );
                }
                else
                {
                    m_Logger.warn( "FROM field was null." );
                    m_Logger.info( "Creating an email from <null>." );
                }
                msg.setRecipients( Message.RecipientType.TO, addresses );
                msg.setSentDate( sentDate );
                msg.setSubject( subject );
                msg.setContent( mmp );
                m_Logger.info( "Message composed." );
                if( m_Transport != null )
                    m_Transport.sendMessage( msg, addresses );
                m_Logger.info( "Message sent." );


    private void connectTransport()
        throws MessagingException
    {
        if( ! m_Transport.isConnected() )
        {
            String host = m_Session.getProperty( "mail.smtp.host" );
            if( host == null )
                host = m_Session.getProperty( "mail.host" );

            String user = m_Session.getProperty( "mail.smtp.user" );
            if( user == null )
                user = m_Session.getProperty( "mail.user" );

            String password = m_Session.getProperty( "mail.smtp.password" );
            if( password == null )
                password = m_Session.getProperty( "mail.password" );

            m_Transport.connect( host, user, password );
        }
    }



copyright disclaimer; This code is provide as-is, to be used in any way 
whatsoever, with no explicit nor implicit warranties.

I hope that helps....

Niclas

On Wednesday 29 January 2003 05:49, Thorsten Scherler wrote:
> You're right! But most of the time I was posting in regard of that:
>
> http://marc.theaimsgroup.com/?l=xml-cocoon-dev&m=103803474424981&w=2
> Bernhard Huber answered me:
> So you may want to enhance the
> src\java\org\apache\cocoon\components\language\markup\xsp\java\sendmail.xsl
> for adding attachments, and provide it as patch.
> As a hint you may have to study the javamail API for adding an
> attachment to the MimeMessage object
>
> So now I think I have to do that:
> In Java you have to do the following (according to
> http://www.developer.com/java/other/article.php/618471).
> ...
> if(null == fields.get("attachment"))
> msg.setText((String)fields.get("body"));
> else
> {
> BodyPart body = new MimeBodyPart(),
> attachment =
>     (BodyPart)fields.get("attachment");
> body.setText((String)fields.get("body"));
> MimeMultipart multipart = new MimeMultipart();
> multipart.addBodyPart(body);
> multipart.addBodyPart(attachment);
> msg.setContent(multipart);
> ...
>
> Geoff Howard wrote:
> > there were some messages on this a month or so ago.  Have you searched
> > the archives for it for both the dev and users list?
> >
> > Geoff Howard
> >
> >>-----Original Message-----
> >>From: Scherler, Thorsten [mailto:Thorsten.Scherler@weidmueller.de]
> >>Sent: Tuesday, January 28, 2003 12:04 PM
> >>To: cocoon-dev@xml.apache.org
> >>Subject: AW: Attachments within sendmail
> >>
> >>
> >>Thanks for the reply.
> >>
> >>i thought nobody but me would like to have that capability.
> >>
> >>I am working on it (at the moment without any success).
> >>
> >>-----Ursprüngliche Nachricht-----
> >>Von: Pier Fumagalli [mailto:pier@betaversion.org]
> >>Gesendet: Dienstag, 28. Januar 2003 17:59
> >>An: cocoon-dev@xml.apache.org
> >>Betreff: Re: Attachments within sendmail
> >>
> >>
> >>I'd like to have that capability (as I already use it on my employer's
> >>site), but I'll need to get acquainted with that part of the code before
> >>commenting on it... Maybe in a couple of weeks...
> >>
> >>    Pier
> >>
> >>"Scherler, Thorsten" <Thorsten.Scherler@weidmueller.de> wrote:
> >>>Am I the only one, who wants to attach files to sendmail?
> >>>
> >>>>Mit freundlichem Gruss,
> >>>>
> >>>>Thorsten Scherler
> >>>>Marketing / Telefonmarketing
> >>>>
> >>>>Weidmüller GmbH & Co.
> >>>>P.O. Box 2807
> >>>>33058 Paderborn
> >>>>Tel.:+ 49 - 5252-960-350
> >>>>Fax:+ 49 - 5252-960-116
> >>>>eMail: thorsten.scherler@weidmueller.de
> >>>>http://www.weidmueller.de
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> >>For additional commands, email: cocoon-dev-help@xml.apache.org
> >>
> >>
> >>---------------------------------------------------------------------
> >>To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> >>For additional commands, email: cocoon-dev-help@xml.apache.org
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> > For additional commands, email: cocoon-dev-help@xml.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
> For additional commands, email: cocoon-dev-help@xml.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: cocoon-dev-unsubscribe@xml.apache.org
For additional commands, email: cocoon-dev-help@xml.apache.org


Mime
View raw message