我创建了一个带有动态附加行的html页面。我创建了一个字符串数组来将值放入 servlet。现在,我正在尝试向自己发送一封包含所有这些价值观的电子邮件。为此,我在我的 servlet 中使用 java 邮件 API。如何在 message.setText() 中给出数组字符串;
如果不可能,用字符串数组发送电子邮件的不同方法是什么。
String[] items = request.getParameterValues("Item");
String[] categories = request.getParameterValues("Category");
String[] names = request.getParameterValues("Name");
String[] quantities = request.getParameterValues("Quantity");
System.out.println("************ExperimentServlet************");
for (String item : items) {
System.out.println("Item is : "+item);
}
for(String category : categories) {
System.out.println("Category is : "+category);
}
for(String name : names) {
System.out.println("Name is : "+name);
}
for(String quantity : quantities) {
System.out.println("Quantity is : "+quantity);
}
for(int i=0; i<20; i++) {
System.out.println("Item 1 is :" + items[i]);
System.out.println("Category 1 is :"+ categories[i]);
System.out.println("Name 1 is :"+ names[i]);
System.out.println("Quantity 1 is : "+quantities[i]);
}
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 PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abcdef@gmail.com", "xyz");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("abcdef@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("jklm@gmail.com"));
message.setSubject("My eMail First Example");
message.setText("Dear Mail Crawler,"
+ "\n\n No spam to my email, please!"
+ "\n\n" + items[i] + " " + categories[i]+ " "+ names[i]+ " "+ quantities[i]
);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
如果我这样做,我可以发送值小于 20 组值的电子邮件,但问题是如果我提供的值组少于 20 组,它会显示数组索引超出范围异常。不能保证每个用户提供 20 组值。