Reputation: 101
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.io.IOException;
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;
@Autowired
@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");
helper.setSubject(subject);
helper.setFrom(from);
helper.setTo(to);
sender.setHost("smtp.gmail.com");
sender.setUsername("emailServerUserName");
sender.setPassword("emailServerPassword");
sender.setPort(587);
Properties props = new Properties();
props.put("mail.smtp.auth",true);
props.put("mail.smtp.starttls.enable","true");
sender.setJavaMailProperties(props);
sender.send(mimeMessage);
} catch(Exception e){
e.printStackTrace();
}
}
}
Can anyone tell me how to send mail to admin users also at a time?
Upvotes: 1
Views: 1367
Reputation: 3709
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
Upvotes: 1