Cc preasent or not in java mail

A common question!

In Java Mail, the CC (Carbon Copy) field is not a separate property, but rather a part of the Recipient object.

When sending an email using Java Mail, you can specify the recipients using the setRecipient() method of the MimeMessage object. The setRecipient() method takes a Recipient object as an argument, which can be one of the following:

  1. InternetAddress: represents a single email address.
  2. Recipient: represents a recipient with a name and email address.

To specify the CC recipients, you can use the addRecipient() method with the Recipient object, and set the Recipient.Type to Recipient.Type.CC.

Here's an example:

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

// Create a MimeMessage object
MimeMessage msg = new MimeMessage(session);

// Set the sender
msg.setFrom(new InternetAddress("[email protected]"));

// Set the subject
msg.setSubject("Test email");

// Set the recipients
msg.addRecipient(Recipient.Type.TO, new InternetAddress("[email protected]"));
msg.addRecipient(Recipient.Type.CC, new InternetAddress("[email protected]"));
msg.addRecipient(Recipient.Type.CC, new InternetAddress("[email protected]"));

// Set the content
msg.setText("Hello, this is a test email.");

// Send the email
Transport.send(msg);

In this example, we're setting the TO recipient to [email protected], and the CC recipients to [email protected] and [email protected].

Note that you can also use the setRecipients() method to set multiple recipients at once, including CC recipients.