Если вы хотите отправить детали заказа пользователю, то я думаю, что он должен быть в удобном для человека формате. Вы можете попробовать что-то вроде ниже:
Учитывая, что у вас есть Order
класс, подобный этому, вы можете переопределить его toString()
метод, чтобы преобразовать детали вашего заказа в String
public class Order {
private long id;
private String productName;
private int quantity;
private double price;
@Override
public String toString() {
return "Order [id=" + id + ", productName=" + productName + ", quantity=" + quantity + ", price=" + price + "]";
}
}
И затем в вашем методе sendEmail вы можете использовать этот toString()
метод.
public static void main(String[] args){
final String username = "MCVJ.PizzaOrdering@gmail.com";
final String password = "";
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
System.out.println(dateFormat.format(date));
String email = "";
String firstName = "Joe";
String content = "";
List<Order> userOrders = null;
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(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("MCVJ.PizzaOrdering@gmail.com")); // same email id
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("" + email + ""));// whome u have to send mails that person id
message.setSubject("Thank You For Placing an Order with us! Below is your Order Receipt!");
userOrders = databaseRepo.getUserOrders(userId); // You can replace this line however you are extracting user's data in your project
content = "..........."+ "\n"+
dateFormat.format((date)) + "\n" +
"............." + "\n" +
"Dear "+ firstName +" ,Below is your order receipt " + "\n";
for(Order order : userOrders){
content += ""+order.toString()+"\n";
}
content += "\n\n No spam to my email, please!";
message.setText(content);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}