Este dilema se puede abordar de diversas maneras, por lo tanto te compartimos la respuesta más completa en nuestra opinión.
Ejemplo 1: correo de primavera
// of course you would use DI in any real-world casesJavaMailSenderImpl sender =newJavaMailSenderImpl();
sender.setHost("mail.host.com");MimeMessage message = sender.createMimeMessage();MimeMessageHelper helper =newMimeMessageHelper(message);
helper.setTo("[email protected]");
helper.setText("Thank you for ordering!");
sender.send(message);
Ejemplo 2: correo de primavera
importjavax.mail.Message;importjavax.mail.MessagingException;importjavax.mail.internet.InternetAddress;importjavax.mail.internet.MimeMessage;importjavax.mail.internet.MimeMessage;importorg.springframework.mail.MailException;importorg.springframework.mail.javamail.JavaMailSender;importorg.springframework.mail.javamail.MimeMessagePreparator;publicclassSimpleOrderManagerimplementsOrderManagerprivateJavaMailSender mailSender;publicvoidsetMailSender(JavaMailSender mailSender)this.mailSender = mailSender;publicvoidplaceOrder(finalOrder order)// Do the business calculations...// Call the collaborators to persist the order...MimeMessagePreparator preparator =newMimeMessagePreparator()publicvoidprepare(MimeMessage mimeMessage)throwsException
mimeMessage.setRecipient(Message.RecipientType.TO,newInternetAddress(order.getCustomer().getEmailAddress()));
mimeMessage.setFrom(newInternetAddress("[email protected]"));
mimeMessage.setText("Dear "+ order.getCustomer().getFirstName()+" "+ order.getCustomer().getLastName()+", thank you for placing order. Your order number is "+ order.getOrderNumber());;trythis.mailSender.send(preparator);catch(MailException ex)// simply log it and go on...System.err.println(ex.getMessage());
Reseñas y calificaciones
Recuerda que puedes dar difusión a esta crónica si te ayudó.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)