java mail cc,在使用java发送邮件时,在地址中添加cc和bcc

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);

}

}

解决方案

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"));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值