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

匿名 (未验证) 提交于 2019-12-03 03:10:03

问题:

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 ="adeshsingh86@gmail.com";     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("adeshsingh86@gmail.com"));                     message.setFrom(new InternetAddress(username));         message.setRecipients(Message.RecipientType.TO,             //InternetAddress.parse("kmukesh2008@gmail.com"));                             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);              }   } 

回答1:

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(             "your@email.com")); message.addRecipient(RecipientType.CC, new InternetAddress(             "yourOther@email.com")); 

more info: MimeMessage API



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!