【发布时间】:2014-01-17 03:43:10
【问题描述】:
我有以下代码,可以从 gmail 发送邮件。问题是我应该在哪个部分添加抄送和密送收件人,以便他们也可以接收邮件。
我进行了搜索并在这里找到了一些帮助,但它在我的程序上不起作用,我在密件抄送和抄送列表上收到错误。
InternetAddress[] myToList = InternetAddress.parse("gopi.mani@xyz.com,Maimsa.SF@xyz.com"); InternetAddress[] myBccList = InternetAddress.parse("Usha.B@xyz.com"); InternetAddress[] myCcList = InternetAddress.parse("NEHA.SIVA@xyz.com"); message.setRecipients(Message.RecipientType.TO,myToList); message.addRecipient(Message.RecipientType.BCC,myBccList); message.addRecipient(Message.RecipientType.CC,myCcList);
代码
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class SendEmail {
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final int SMTP_HOST_PORT = 587;
private static final String SMTP_AUTH_USER = "sender@gmail.com";
private static final String SMTP_AUTH_PWD = "";
public static void main(String[] args) throws Exception {
SendEmail se = new SendEmail();
se.mail();
}
public void mail() throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing SMTP From Java");
message.setContent("Hello world", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("receiver@gmail.com"));
transport.connect(SMTP_HOST_NAME, SMTP_HOST_PORT, SMTP_AUTH_USER, SMTP_AUTH_PWD);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
}
【问题讨论】:
-
您遇到什么错误?
-
线程“主”java.lang.RuntimeException 中的异常:无法编译的源代码 - 错误的符号类型:javax.mail.internet.MimeMessage.addRecipient
-
这是一个编译错误。修复它。