Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add cc and bcc in the address while sending the mail using java

Tags:

java

email

I am sending the email using the java . I want to send the mail as bcc and cc options also in the address how is it possible. I am using the following code.

   public String sendemail(String xtomail,String xsub,String xbody)
    {
   final String username ="[email protected]";
    final String password ="passwordhere";


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

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

    try {

        Message message = new MimeMessage(session);
        //message.setFrom(new InternetAddress("[email protected]"));
                    message.setFrom(new InternetAddress(username));
        message.setRecipients(Message.RecipientType.TO,
            //InternetAddress.parse("[email protected]"));
                            InternetAddress.parse(xtomail));
        //message.setSubject("Testing Subject");
                    message.setSubject(xsub);
 // message.setText("Dear Mail Crawler,"
//          + "\n\n No spam to my email, please!");

                        message.setText(xbody);

        Transport.send(message);

        return "Y";

    } catch (MessagingException e) {
                return "N";
        //throw new RuntimeException(e);

            }


}
like image 650
adesh singh Avatar asked Mar 15 '13 06:03

adesh singh


People also ask

How do I send an email with CC and BCC?

How to Use CC and BCC When Writing an Email. In most email clients, you'll find the CC and BCC fields next to or below the “To” field whenever you compose a new message. Using either CC or BCC is simply a matter of adding your recipients' email addresses into the respective fields.

How do I send a bulk and CC email in BCC?

Click Cc/Bcc . Insert the email address or type the name of a contact that already exists in OnePageCRM that you want to Cc or Bcc in each corresponding field → Send All. Note: Scroll through all emails using the left and right toggles to edit the other emails.

How do I send an email to multiple addresses in Java?

Sending Email to Multiple RecipientsFor adding Email in 'TO' field, you may use Message.RecipientType.To . Similarly for adding Email in 'CC' and 'BCC' fields, you will have to use Message.RecipientType.CC and Message. RecipientType. BCC.

Can we add CC in mail?

To add a CC recipient, click on the downward arrow on the top right corner in the To address box, as shown below. This will display the CC and BCC fields. In the CC field, enter the mail addresses of the recipients who'll receive a copy of the email. Compose your message and hit Send.


1 Answers

You set your recipients with the setter method. Look at how you add it, you'll see you add a Message.RecipientType.TO. Same can be done with CC and BCC. You could use the addRecipient method for this too.

ex:

message.addRecipient(RecipientType.BCC, new InternetAddress(
            "[email protected]"));
message.addRecipient(RecipientType.CC, new InternetAddress(
            "[email protected]"));

more info: MimeMessage API

like image 109
Qkyrie Avatar answered Nov 11 '22 14:11

Qkyrie