Subbarami Reddy Subbarami Reddy - 1 year ago 100
Java Question

How do you send 2 emails to two different email addresses in Java?

I have created a module for contact us. Here one user can send a mail about his concerns to admin and admin can send a reply mail to that particular user. So I have created 2 different mail templates, But I don't know how to send both at the same time.

Now I have completed sending reply email to user, but I have not done sending mail to admin user.

My code for sending reply mail to user:

package com.spring.test.service;

import java.util.Properties;

import javax.mail.internet.InternetAddress;

import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.client.RestTemplate;

public class MailMail {

private MailSender mailSender;

@Qualifier(value = "mailSender")
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;

public void sendMail(String to, String subject, String msg) {
//creating message

System.out.println("mailsender == " + this.mailSender);

try {
String from = "emailFromAddress";

JavaMailSenderImpl sender = new JavaMailSenderImpl();
javax.mail.internet.MimeMessage mimeMessage = sender.createMimeMessage();
org.springframework.mail.javamail.MimeMessageHelper helper = new org.springframework.mail.javamail.MimeMessageHelper(mimeMessage, false, "utf-8");
mimeMessage.setContent(msg, "text/html");


Properties props = new Properties();

} catch(Exception e){

Can anyone tell me how to send mail to admin users also at a time?

Answer Source

Your implementation of sendMail should be completely driven from externalised parameters like email address to, email address from, subject , email template path and data with placeholder properties. Means none of the mentioned parameters be intialized, declared or manipulate in this method.

Probably with this your sendMail becomes a complete stateless service which you can call for sending various emails with different parameters

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download