本文介绍了在使用java发送邮件时,在地址中添加cc和bcc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用java发送电子邮件。我想发送邮件作为bcc和cc选项也在地址怎么可能。我使用以下代码。 public String sendemail(String xtomail,String xsub,String xbody)
{
final String username [email protected];
final String password =passwordhere;
属性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.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,
// +\\\
\\\
我的电子邮件中没有垃圾邮件!);
message.setText(xbody);
Transport.send(message);
返回Y;
} catch(MessagingException e){
returnN;
//抛出新的RuntimeException(e);
}
}
解决方案
使用setter方法设置收件人。看看你如何添加它,你会看到你添加一个Message.RecipientType.TO。 CC和BCC也可以完成。您可以使用addRecipient方法。
ex:
message.addRecipient(RecipientType.BCC,new InternetAddress(
[email protected]));
message.addRecipient(RecipientType.CC,new InternetAddress(
[email protected]));
更多信息:
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);
}
}
解决方案
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
这篇关于在使用java发送邮件时,在地址中添加cc和bcc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!