Configure SMTP and send email notifications for contact form submissions
Portfolio Hub API sends email notifications when visitors submit contact forms. This guide covers SMTP configuration and the email notification system.
The EmailServiceImpl handles sending contact notifications. From EmailServiceImpl.java:
@Service@RequiredArgsConstructor@Slf4jpublic class EmailServiceImpl implements EmailService { private final JavaMailSender mailSender; @Value("${spring.mail.username}") private String fromEmail; @Async @Override public void sendContactNotification(Profile recipientProfile, ContactMessage message) { try { SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom(fromEmail); mail.setTo(recipientProfile.getContactEmail()); mail.setSubject("Nuevo Mensaje de Contacto de: " + message.getName()); String text = String.format(""" Has recibido un nuevo mensaje a través de tu portafolio '%s': De: %s Email: %s Mensaje: %s""", recipientProfile.getFullName(), message.getName(), message.getEmail(), message.getMessage() ); mail.setText(text); mailSender.send(mail); log.info("Email de contacto enviado exitosamente a {}", recipientProfile.getContactEmail()); } catch (MailException e) { log.error("Error al enviar email de contacto a {}: {}", recipientProfile.getContactEmail(), e.getMessage(), e); } }}
The notification email sent to the portfolio owner looks like this:Subject:
Nuevo Mensaje de Contacto de: Alice Johnson
Body:
Has recibido un nuevo mensaje a través de tu portafolio 'John Doe':De: Alice JohnsonEmail: [email protected]Mensaje:Hi John, I'd love to discuss a potential collaboration on a new project. Are you available for a call next week?
To customize the email content, edit the template in EmailServiceImpl.java:38-50:
String text = String.format(""" Has recibido un nuevo mensaje a través de tu portafolio '%s': De: %s Email: %s Mensaje: %s""", recipientProfile.getFullName(), message.getName(), message.getEmail(), message.getMessage());
You can:
Change the language
Add more fields
Include HTML formatting (use MimeMessage instead of SimpleMailMessage)
Ensure async processing is enabled in your Spring Boot application:
import org.springframework.context.annotation.Configuration;import org.springframework.scheduling.annotation.EnableAsync;@Configuration@EnableAsyncpublic class AsyncConfig { // Async is now enabled}
@Asyncpublic void sendAutoReply(ContactMessage message) { try { SimpleMailMessage mail = new SimpleMailMessage(); mail.setFrom(fromEmail); mail.setTo(message.getEmail()); mail.setSubject("Thanks for contacting me!"); mail.setText( "Hi " + message.getName() + ",\n\n" + "Thank you for your message! I'll get back to you as soon as possible.\n\n" + "Best regards" ); mailSender.send(mail); log.info("Auto-reply sent to {}", message.getEmail()); } catch (MailException e) { log.error("Error sending auto-reply: {}", e.getMessage()); }}
log.info("Email de contacto enviado exitosamente a {}", recipientProfile.getContactEmail());log.error("Error al enviar email de contacto a {}: {}", recipientProfile.getContactEmail(), e.getMessage(), e);
In production, consider:
Sending logs to a monitoring service (Datadog, Sentry, etc.)