java Stirng[]无法转换为字符串
我不能纠正我的错误。我想要一个向多人发送电子邮件的程序。这是我目前的代码
public static void sendEmailWithAttachments(String host, String port,
final String userName, final String password, String[] toAddress,
String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP server properties
Properties properties = new Properties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.port", port);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
};
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(userName));
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
在{new Internet Address(toAddress)}
中的toAddress
表示String[]不能转换为String
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
提前感谢您的帮助:)
# 1 楼答案
只需迭代toAddress,将每个字符串包装在InternetAddress中,并将其放入toAddress中
# 2 楼答案
尝试先创建internet地址数组,然后分配一个值: