我正在使用java發送電子郵件。我想發送郵件作爲bcc和cc選項也在地址中,它怎麼可能。我正在使用下面的代碼。在使用java發送郵件時在地址中添加cc和bcc
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);
}
}
既然你已經使用'在你的代碼Message.RecipientType.TO'做CC和BCC應該是很直觀 – rajesh 2013-03-15 06:20:09
message.addRecipients(Message.RecipientType .CC,收件人); //收件人是Address []的一個實例。同樣,對於BCC。 – 2013-03-15 06:20:14