Skip to main content

JavaMail 1.5.0 or gmail bug?

  9 posts   Feedicon  
Replies: 8 - Last Post: November 11, 2013 09:32
by: Tiziano_it
showing 1 - 9 of 9
Posted: November 05, 2013 10:02 by Tiziano_it
I'm trying to send emails using a class wrapping javax.mail (JavaMail 1.5.0). It sends using and it works, but works way too well, since when i send an email with multiple recipients every email is sent (TO+CC) times so if i send an email to 3 recipients i get a total 9 emails (every one repeated 3 times).

This is not good... Setting Session debug shows the email is correctly sent 1 time with the correct recipients. I'm on jre6u45.

What is the problem? Does anyone have any hint? Have I done a stupid error i'm not seeing?

Here is my code:
import java.util.Calendar;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mailer 
    public static boolean sendMail(
        final String username,
        final String password,
        final String from, 
        final String _tos,
        final String _ccs,
        final String _bccs,
        final String subject,
        final String text,
        final File[] attachments)
        Properties   p           = null;
        Session      s           = null;
        boolean      debug       = true;
        boolean      res         = false;
        boolean      isSecured   = false;
        boolean      isSupported = true;
        String       host        = null;     
        final String charset     = "UTF-8";

        String fromProvider = from.split("@")[1];
        if(fromProvider.compareTo("") == 0)
            isSecured = true;
            host      = "";
            p         = System.getProperties();
            p.put("",                host);
            p.put("mail.smtps.auth",               "true");
            p.put("mail.smtp.port",                "465");
            p.put("mail.smtp.starttls.enable",     "true");
            p.put("mail.smtp.socketFactory.port",  "465");
            p.put("mail.smtp.socketFactory.class", "");

            s = Session.getInstance(p, null);
        else {isSupported = false;}


                // init
                MimeMessage email = new MimeMessage(s);
                Multipart   mp    = new MimeMultipart();
                InternetAddress[] tos  = {};
                InternetAddress[] ccs  = {};
                InternetAddress[] bccs = {};
                // common
                if(_tos  != null) {tos  = InternetAddress.parse(_tos);}
                if(_ccs  != null) {ccs  = InternetAddress.parse(_ccs);}
                if(_bccs != null) {bccs = InternetAddress.parse(_bccs);}
                email.addRecipients(RecipientType.TO,  tos);
                email.addRecipients(RecipientType.CC,  ccs);
                email.addRecipients(RecipientType.BCC, bccs);

                email.setSubject   (subject, charset);
                email.setSentDate  (Calendar.getInstance().getTime());

                // text part
                MimeBodyPart mbpText = new MimeBodyPart();
                mbpText.setText(text, charset, "html");

                // attachments
                if(attachments != null && attachments.length > 0)
                    for(int i=0; i<attachments.length; i++)
                        MimeBodyPart mbpAttachment = new MimeBodyPart();
                // build elements
                Address[] addresses = email.getAllRecipients();                
                // send
                Transport.send(email, addresses, username, password);

                res = true;
            catch(MessagingException ex) {ex.printStackTrace();}
            catch(IOException ex)        {ex.printStackTrace();}
        return res;
Posted: November 05, 2013 22:40 by Bill Shannon
I don't see anything that would explain why you're getting multiple copies of the message. Does each copy have a different Message-ID? If so, you're probably calling your sendMail method multiple times.

Also, there's a few places you could clean up your code...

You don't need the socketFactory properties.

The mail.smtps.auth property isn't doing anything since you're using the "smtp" protocol.

You could use Transport.send(email, username, password) and avoid the need to call getAllRecipients yourself.
Posted: November 06, 2013 15:59 by Tiziano_it
No, i call sendmail 1 time, i run it in netbeans with breakpoints and check email accounts before returning, i tried using Transport.send(email, username, password) too with same result, I used that signature to check the recipients list.
Duplicate emails message-id are identical.

How can i debug this? Any hint?
Posted: November 06, 2013 21:22 by Bill Shannon
If you're sending the message only once and you're getting three copies of the same message in each INBOX for each recipient, then the problem is in the server.

Does the protocol trace provide any clues?
Posted: November 07, 2013 10:20 by Tiziano_it
this is the debug trace, (email address are editorialized), the is a bcc i just can't see any problem...

DEBUG: setDebug: JavaMail version 1.5.0
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "", port 25, isSSL false
220 ESMTP b6sm1665132bko.16 - gsmtp
DEBUG SMTP: connected to host "", port: 465

EHLO admin-HP at your service, []
250-SIZE 35882577
DEBUG SMTP: Found extension "SIZE", arg "35882577"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: use8bit false
MAIL FROM:<admin@admin-HP>
250 2.1.0 OK b6sm1665132bko.16 - gsmtp
250 2.1.5 OK b6sm1665132bko.16 - gsmtp
250 2.1.5 OK b6sm1665132bko.16 - gsmtp
250 2.1.5 OK b6sm1665132bko.16 - gsmtp
DEBUG SMTP: Verified Addresses
354 Go ahead b6sm1665132bko.16 - gsmtp
Date: Thu, 7 Nov 2013 11:13:09 +0100 (CET)
Message-ID: <22629283.2.1383819190367.JavaMail.admin@admin-HP>
Subject: Test
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_0_2831732.1383819189919"

Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

Content-Type: text/plain; charset=us-ascii; name=attachment.txt
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename=attachment.txt

uno due tre
250 2.0.0 OK 1383819197 b6sm1665132bko.16 - gsmtp
221 2.0.0 closing connection b6sm1665132bko.16 - gsmtp
Posted: November 08, 2013 06:50 by Bill Shannon
That all looks perfectly normal.

How are you determining that the message appears 3 times in each INBOX?

If you send the message to 4 recipients, does it appear 4 times?
Posted: November 08, 2013 09:41 by Tiziano_it
I send the multi RCPT email using this class to some of my work emails addresses (on, then (since this is office protocol...) i retrieve them through outlook 2007 client (even if it's MS it never failed at this level), In my example I sent 2 TOs, 1 CC and 1 BCC. Due to other experiments i proved that emails are sent multiple times as (TOs+CCs). BCCs are not part of this formula. I tried with (TOs+CCs) = 2 and (TOs+CCs) = 3 and every email appears in my inbox that number of times.

Is there an incompatibiliti with Gmail? Or perhaps with any particular setting? I'm not very proficient at emails matters (excuse for my poor English) so, can you suggest where to look to try to debug this problem? Do you know of JavaMail 1.5.0 sending correctly through Gmail in these days so I can pinpoint the problem to my code or machine or account? If I send a "manual" email from Gmail I see no issues.

I tried to send from a fresh Gmail account too, the only option modified is the one to let pop work.
Posted: November 09, 2013 01:11 by Bill Shannon
You sent the messages using the Gmail SMTP server, but you sent them to work email addresses that are not on Gmail, right? Then you used Outlook to view the messages from your work email server?

Is it possible that you're sending from a machine with a firewall or anti-virus product that's examining your messages before they're sent to the Gmail server? Since things work correctly when you send directly from the Gmail web user interface itself, that implies that the problem is between your application and the Gmail server.

Since the Bcc addresses don't have any effect (although I assume the message is still being sent to the Bcc addresses), it's as if something is looking at the message you're sending and re-sending it to the addresses in the headers of the message. The Bcc addresses aren't in the headers of the message, they're only part of the SMTP protocol.

This problem is definitely something related to your environment, not JavaMail and not your code. You can verify that it's not your code by using the JavaMail sample program for testing.
Posted: November 11, 2013 09:32 by Tiziano_it
By paragraphs:

1) a:yes, b:yes
2) avg free, but it does not check outgoing emails (standard behaviour)
3) yes bcc is deliverd

4) Yes I'll do in these days and will post the results for reference.

Many thanks for your time and kind attencion.
showing 1 - 9 of 9
Replies: 8 - Last Post: November 11, 2013 09:32
by: Tiziano_it
Please Confirm