You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Jacques Desodt <jd...@gmail.com> on 2012/04/17 18:45:03 UTC

In webapp, sending multipart email with Javamail : noname attachment instead of message

Hi,

I try to send a Multipart email in a simple java method, using Javamail.
When i use the method in a local java app, everything is fine.
When i use the same method in a webapp hosted by Tomcat,
i got no message, but a file "noname" in attachment.

Here is the method :

public void sendTestMultipartMail(String host, final String username, final
String password, String from, String to) throws Exception {

Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.port", "587");

Session session = Session.getInstance(properties,
  new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
  });

Message msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject("Test MultiPart Mail");

Multipart multipart = new MimeMultipart();

BodyPart part1 = new MimeBodyPart();
part1.setText("This is multipart mail and u read part1......");

BodyPart part2 = new MimeBodyPart();
part2.setText("This is multipart mail and u read part2......");

multipart.addBodyPart(part1);
multipart.addBodyPart(part2);

msg.setContent(multipart);

Transport.send(msg);
}

And here are the results (i use gmail) :

[image: Images intégrées 1]

The first (and wrong) email came from the Tomcat server.
It has no message, and an attachment with "noname" name.
The other email is good and came from my java app.

Thanks in advance,
Jacques

Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/4/18 Konstantin Kolinko <kn...@gmail.com>:
> 2012/4/18 Jacques Desodt <jd...@gmail.com>:
>> Well, a little bit disappointed...
>> I changed my code :
>>
>>(...)
>> ... and that's all...
>> Why ? I don't understand. Same method, same smtp server.
>> Any idea ?
>
> Different libraries?
>
> Try to check, what implementation is returned by those calls,
> Session.getInstance(..)
>
> Try running with a debugger,
>
> http://wiki.apache.org/tomcat/FAQ/Developing#Debugging


BTW, you have not told us, what Tomcat x.y.z version you are using.

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Jacques Desodt <jd...@gmail.com>.
Hi Konstantin,

Good news, your solution is GREAT.
I looked at the libraries, and I saw geronimo-javamail_1.4_spec-1.7.1.jar
in the Tomcat lib folder (installed with CXF).
Removing it, everything works fine !

Thanks a lot,
Best regards,

Jacques

...and i'm using Tomcat 6.0.33 for this app :)



2012/4/18 Konstantin Kolinko <kn...@gmail.com>

> 2012/4/18 Jacques Desodt <jd...@gmail.com>:
> > Well, a little bit disappointed...
> > I changed my code :
> >
> >    public void sendTestMultipartMail(String host, final String username,
> > final String password, String from, String to) throws Exception {
> >
> >        Properties properties = new Properties();
> >        properties.put("mail.smtp.host", host);
> >        properties.put("mail.smtp.auth", "true");
> >        properties.put("mail.smtp.starttls.enable", "true");
> >        properties.put("mail.smtp.port", "587");
> >
> >        Session session = Session.getInstance(properties,
> >          new javax.mail.Authenticator() {
> >            protected PasswordAuthentication getPasswordAuthentication() {
> >                return new PasswordAuthentication(username, password);
> >            }
> >          });
> >
> >        Message msg = new MimeMessage(session);
> >
> >        msg.setFrom(new InternetAddress(from));
> >        msg.addRecipient(Message.RecipientType.TO, new
> InternetAddress(to));
> >        msg.setSubject("Test MultiPart Mail");
> >
> >        Multipart multipart = new MimeMultipart();
> >
> >        BodyPart part1 = new MimeBodyPart();
> >        part1.setText("This is multipart mail and u read part1......");
> >
> >        BodyPart part2 = new MimeBodyPart();
> >        part2.setText("This is multipart mail and u read part2......");
> >
> >        multipart.addBodyPart(part1);
> >        multipart.addBodyPart(part2);
> >
> >        msg.setContent(multipart);
> >
> >        Transport.send(msg);
> >    }
> >
> > -----------------------------------
> > Here is the raw message, sended by my local java app:
> >
> >(...)
> > ... and that's all...
> > Why ? I don't understand. Same method, same smtp server.
> > Any idea ?
>
> Different libraries?
>
> Try to check, what implementation is returned by those calls,
> Session.getInstance(..)
>
> Try running with a debugger,
>
> http://wiki.apache.org/tomcat/FAQ/Developing#Debugging
>
> Best regards,
> Konstantin Kolinko
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/4/18 Jacques Desodt <jd...@gmail.com>:
> Well, a little bit disappointed...
> I changed my code :
>
>    public void sendTestMultipartMail(String host, final String username,
> final String password, String from, String to) throws Exception {
>
>        Properties properties = new Properties();
>        properties.put("mail.smtp.host", host);
>        properties.put("mail.smtp.auth", "true");
>        properties.put("mail.smtp.starttls.enable", "true");
>        properties.put("mail.smtp.port", "587");
>
>        Session session = Session.getInstance(properties,
>          new javax.mail.Authenticator() {
>            protected PasswordAuthentication getPasswordAuthentication() {
>                return new PasswordAuthentication(username, password);
>            }
>          });
>
>        Message msg = new MimeMessage(session);
>
>        msg.setFrom(new InternetAddress(from));
>        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
>        msg.setSubject("Test MultiPart Mail");
>
>        Multipart multipart = new MimeMultipart();
>
>        BodyPart part1 = new MimeBodyPart();
>        part1.setText("This is multipart mail and u read part1......");
>
>        BodyPart part2 = new MimeBodyPart();
>        part2.setText("This is multipart mail and u read part2......");
>
>        multipart.addBodyPart(part1);
>        multipart.addBodyPart(part2);
>
>        msg.setContent(multipart);
>
>        Transport.send(msg);
>    }
>
> -----------------------------------
> Here is the raw message, sended by my local java app:
>
>(...)
> ... and that's all...
> Why ? I don't understand. Same method, same smtp server.
> Any idea ?

Different libraries?

Try to check, what implementation is returned by those calls,
Session.getInstance(..)

Try running with a debugger,

http://wiki.apache.org/tomcat/FAQ/Developing#Debugging

Best regards,
Konstantin Kolinko

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Jacques Desodt <jd...@gmail.com>.
Well, a little bit disappointed...
I changed my code :

    public void sendTestMultipartMail(String host, final String username,
final String password, String from, String to) throws Exception {

        Properties properties = new Properties();
        properties.put("mail.smtp.host", host);
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.starttls.enable", "true");
        properties.put("mail.smtp.port", "587");

        Session session = Session.getInstance(properties,
          new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
          });

        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(from));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
        msg.setSubject("Test MultiPart Mail");

        Multipart multipart = new MimeMultipart();

        BodyPart part1 = new MimeBodyPart();
        part1.setText("This is multipart mail and u read part1......");

        BodyPart part2 = new MimeBodyPart();
        part2.setText("This is multipart mail and u read part2......");

        multipart.addBodyPart(part1);
        multipart.addBodyPart(part2);

        msg.setContent(multipart);

        Transport.send(msg);
    }

-----------------------------------
Here is the raw message, sended by my local java app:

Return-Path: <jd...@gmail.com>
Received: from Jack-PC (xxx-1-166-xxx.w90-58.xxx.fr. [xx.xx.xx.237])
        by mx.google.com with ESMTPS id
fz9sm35717987wib.3.2012.04.18.09.55.23
        (version=TLSv1/SSLv3 cipher=OTHER);
        Wed, 18 Apr 2012 09:55:24 -0700 (PDT)
Date: Wed, 18 Apr 2012 09:55:24 -0700 (PDT)
From: jdesodt@gmail.com To: jdesodt@gmail.com
Message-ID: <59...@Jack-PC>
Subject: Test MultiPart Mail
MIME-Version: 1.0

Content-Type: multipart/mixed;
	boundary="----=_Part_0_1395193582.1334768140066"

------=_Part_0_1395193582.1334768140066
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is multipart mail and u read part1......
------=_Part_0_1395193582.1334768140066
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

This is multipart mail and u read part2......
------=_Part_0_1395193582.1334768140066--


------------------------------------
and here is the raw message, sended by my webapp via tomcat :

Return-Path: <jd...@gmail.com>
Received: from Jack-PC (xxx-1-166-xxx.w90-58.xxx.fr. [xx.xx.xx.237])
        by mx.google.com with ESMTPS id e6sm35753185wix.8.2012.04.18.10.00.33
        (version=TLSv1/SSLv3 cipher=OTHER);
        Wed, 18 Apr 2012 10:00:33 -0700 (PDT)
Date: Wed, 18 Apr 2012 10:00:33 -0700 (PDT)
From: jdesodt@gmail.com
To: jdesodt@gmail.com
Message-ID: <12...@Jack-PC>
Subject: Test MultiPart Mail
MIME-Version: 1.0
Content-Type: multipart/mixed;
	boundary="----=_Part_0_623107838.1334768449974"


... and that's all...
Why ? I don't understand. Same method, same smtp server.
Any idea ?

Jacques



2012/4/18 Jacques Desodt <jd...@gmail.com>

> Hi Chris,
>
> Thanks a lot for your quick reply !
> My apologies for the image posted, I didn't know
> it could be stripped by the mailing list.
> OK, I will test your solution tonight.
>
> I agree with you, Javamail API is really complicated,
> I always used Apache Commons Email.
> I will have a look at your great project.
>
> Jacques
>
>
> 2012/4/18 Christopher Schultz <ch...@christopherschultz.net>
>
>> -----BEGIN PGP SIGNED MESSAGE-----
>> Hash: SHA1
>>
>> Jacques,
>>
>> On 4/17/12 12:45 PM, Jacques Desodt wrote:
>> > I try to send a Multipart email in a simple java method, using
>> > Javamail. When i use the method in a local java app, everything is
>> > fine. When i use the same method in a webapp hosted by Tomcat, i
>> > got no message, but a file "noname" in attachment.
>>
>> Your code looks reasonable. I wonder if you are missing some system
>> property when running under Tomcat, since you use the system
>> properties as a basis for your JavaMail session setup.
>>
>> Try using an empty Properties object instead of taking
>> System.getProperties() and mutating that. You might find that there is
>> some property available in your standalone environment that you are
>> missing in the Tomcat one.
>>
>> > And here are the results (i use gmail) :
>> >
>> > Images intégrées 1
>>
>> (I'm not sure what that means... if you attached something to your
>> email, it was stripped by the mailing list).
>>
>> > The first (and wrong) email came from the Tomcat server. It has no
>> > message, and an attachment with "noname" name. The other email is
>> > good and came from my java app.
>>
>> What are the differences when you look at the actual source code of
>> the message? You can learn a lot from looking at the raw MIME message.
>>
>> BTW, if you intend to move from a simple multipart text message as you
>> have above to something more complicated (such as text + HTML +
>> attachments) be aware that the JavaMail API is a complete main in the
>> neck to use. I would highly recommend using a wrapper API such as
>> commons-email or my own emailer wrapper you can find at
>> http://christopherschultz.net/projects/java/
>>
>> I happen to like mine better ;)
>>
>> - -chris
>> -----BEGIN PGP SIGNATURE-----
>> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
>> Comment: GPGTools - http://gpgtools.org
>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>>
>> iEYEARECAAYFAk+OHRMACgkQ9CaO5/Lv0PDk6wCgtsLID0HPf6nNH2bFc/ThfrTl
>> J58AnjuTcjIFhj1QOgESVmgMFK5jWOBC
>> =QXQU
>> -----END PGP SIGNATURE-----
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>

Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Jacques Desodt <jd...@gmail.com>.
Hi Chris,

Thanks a lot for your quick reply !
My apologies for the image posted, I didn't know
it could be stripped by the mailing list.
OK, I will test your solution tonight.

I agree with you, Javamail API is really complicated,
I always used Apache Commons Email.
I will have a look at your great project.

Jacques


2012/4/18 Christopher Schultz <ch...@christopherschultz.net>

> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Jacques,
>
> On 4/17/12 12:45 PM, Jacques Desodt wrote:
> > I try to send a Multipart email in a simple java method, using
> > Javamail. When i use the method in a local java app, everything is
> > fine. When i use the same method in a webapp hosted by Tomcat, i
> > got no message, but a file "noname" in attachment.
>
> Your code looks reasonable. I wonder if you are missing some system
> property when running under Tomcat, since you use the system
> properties as a basis for your JavaMail session setup.
>
> Try using an empty Properties object instead of taking
> System.getProperties() and mutating that. You might find that there is
> some property available in your standalone environment that you are
> missing in the Tomcat one.
>
> > And here are the results (i use gmail) :
> >
> > Images intégrées 1
>
> (I'm not sure what that means... if you attached something to your
> email, it was stripped by the mailing list).
>
> > The first (and wrong) email came from the Tomcat server. It has no
> > message, and an attachment with "noname" name. The other email is
> > good and came from my java app.
>
> What are the differences when you look at the actual source code of
> the message? You can learn a lot from looking at the raw MIME message.
>
> BTW, if you intend to move from a simple multipart text message as you
> have above to something more complicated (such as text + HTML +
> attachments) be aware that the JavaMail API is a complete main in the
> neck to use. I would highly recommend using a wrapper API such as
> commons-email or my own emailer wrapper you can find at
> http://christopherschultz.net/projects/java/
>
> I happen to like mine better ;)
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
> Comment: GPGTools - http://gpgtools.org
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAk+OHRMACgkQ9CaO5/Lv0PDk6wCgtsLID0HPf6nNH2bFc/ThfrTl
> J58AnjuTcjIFhj1QOgESVmgMFK5jWOBC
> =QXQU
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>

Re: In webapp, sending multipart email with Javamail : noname attachment instead of message

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Jacques,

On 4/17/12 12:45 PM, Jacques Desodt wrote:
> I try to send a Multipart email in a simple java method, using
> Javamail. When i use the method in a local java app, everything is
> fine. When i use the same method in a webapp hosted by Tomcat, i
> got no message, but a file "noname" in attachment.

Your code looks reasonable. I wonder if you are missing some system
property when running under Tomcat, since you use the system
properties as a basis for your JavaMail session setup.

Try using an empty Properties object instead of taking
System.getProperties() and mutating that. You might find that there is
some property available in your standalone environment that you are
missing in the Tomcat one.

> And here are the results (i use gmail) :
> 
> Images intégrées 1

(I'm not sure what that means... if you attached something to your
email, it was stripped by the mailing list).

> The first (and wrong) email came from the Tomcat server. It has no
> message, and an attachment with "noname" name. The other email is
> good and came from my java app.

What are the differences when you look at the actual source code of
the message? You can learn a lot from looking at the raw MIME message.

BTW, if you intend to move from a simple multipart text message as you
have above to something more complicated (such as text + HTML +
attachments) be aware that the JavaMail API is a complete main in the
neck to use. I would highly recommend using a wrapper API such as
commons-email or my own emailer wrapper you can find at
http://christopherschultz.net/projects/java/

I happen to like mine better ;)

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk+OHRMACgkQ9CaO5/Lv0PDk6wCgtsLID0HPf6nNH2bFc/ThfrTl
J58AnjuTcjIFhj1QOgESVmgMFK5jWOBC
=QXQU
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org