-1

I found that my Application will send email to me twice when my Email Address is in both TO List and CC List.

How to control the email only sent to the same email address by once but keeping the address is in TO and CC List?

transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.CC));
transport.close();
user207421
  • 305,947
  • 44
  • 307
  • 483
Jbisgood9999999
  • 253
  • 1
  • 5
  • 15
  • You told it to send to: and cc: the same person, so it does that. You're also sending it twice instead of once, so any opportunity for anybody to 'optimise' it is gone. Just use `send(Message)`, once. But what you're asking doesn't make sense. It also isn't due to JavaMail but the mail server. – user207421 Dec 24 '15 at 02:43
  • *Application will send email to me twice* but that's exactly what you said it to do. My question is what's wrong with that ? Why do you want to avoid that ? – akash Dec 24 '15 at 02:44

4 Answers4

0

You could write a line of code to check that the TO isn't the CC before sending the mail to the CC.

Or only use sendMessage once as here: Send Mail to multiple Recipients in java I'm unsure if it will de-duplicate, if not, then store the String addresses in a Set (which de-duplicates) before adding.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
0

It shouldn't matter if your recipients are duplicated in the TO or CC part. The mailing server should take care to filter and not send repeated message. In order to achieve that do mark 2 points in your code.

  1. Unique message-ID
  2. Date Header

These will help you to avoid duplication.

mimeMessage.setSentDate(new Date()); // Date header
mimeMessage.saveChanges();           // If no unique message-ID is set, then this will set it for you
Saurabh Talreja
  • 335
  • 2
  • 6
-1

code send mail in java and call Email from Properties file.

Properties prop = new Properties(); 
InputStream input   = SentmailAttachFile.class.getResourceAsStream("/Sendmail.properties");
                prop.load(input);

String receiver  = prop.getProperty("MAILADDRESS");
String mailCC        = prop.getProperty("MAILCC"); 

Properties props = new Properties();
    props.put("mail.smtp.host" , host);
    props.put("mail.smtp.auth" , "true" );
    props.put("mail.transport.protocol", "smtp");
    Session ss     = Session.getInstance(props,null);
    MimeMessage ms = new MimeMessage(ss);
    ms.addRecipient(Message.RecipientType.TO,new InternetAddress(receiver));
    ms.addRecipient(Message.RecipientType.CC, new InternetAddress(mailCC));
Cherryishappy
  • 160
  • 1
  • 6
  • It is to be assumed that he's already got this far, otherwise his message wouldn't have any TO or CC recipients, – user207421 Dec 24 '15 at 06:33
-1

Sorry, i solved the problem by :

    transport.connect();
    transport.sendMessage(message, message.getAllRecipients());
    transport.close();

I don't know there is a function : message.getAllrecipients() because i tried to use other's coding. I think the problem has been solved.

Jbisgood9999999
  • 253
  • 1
  • 5
  • 15