I'm trying to send a simple mail using spring.
Here is my mail sender bean definition in java configuration.
#Bean
public JavaMailSender javaMailService() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setSession(getMailSession());
return mailSender;
}
public Session getMailSession() {
JndiTemplate template = new JndiTemplate();
Session session = null;
try {
session = (Session) template.lookup("java:jboss/mail/Default");
} catch (NamingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return session;
}
I'm running is jboss wildfly and the beans are created without any issue.
Here is my code to send the email.
#Autowired
private JavaMailSender mailSender;
#Override
public void sendMail(String mailTo, String subject, String content) throws MessagingException{
MimeMessage message = mailSender.createMimeMessage();
message.setSubject(subject);
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mailTo,false));
mailSender.send(message);
}
the JavaMailSender is injected correctly. when I debug the execution happens till mailsender.send() method. and it starts to hang.
It seems all the configurations in the jboss is correct. I also tried specifying the mail server parameters in the bean it self. but still it's not working.
What am I doing wrong here?
Though the issue is already resolved, I faced similar issue, and none of the steps mentioned above and on other threads worked. I had to additionally specify the protocol as SMTPS for this to work. So here is my working code snippet.
#Bean
JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost(props.getHost());
mailSender.setPort(props.getPort());
mailSender.setUsername(props.getUsername());
mailSender.setPassword(props.getPassword());
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.auth", props.getSmtp().isAuth());
mailProperties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
mailProperties.put("mail.smtp.starttls.enable", props.getSmtp().isStarttlsEnable());
mailSender.setJavaMailProperties(mailProperties);
mailSender.setProtocol("smtps");
return mailSender;
}
Follow these instructions to send an email with Spring and Gmail: Email with Spring and Gmail
You're completely missing the authentication part. And make sure the Gmail smtp server address is correct.
Taken from the above link these must be the configurations;
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="username" />
<property name="password" value="password" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
Related
I have a mail sender set up in config file: spring/servlet-context.xml
which has username and password set.
I've come up with a specific use-case where I need to send mail from a different email account.
Is it possible to set this up in this same config file.
At first, I thought this would mean simply to add another bean id having the other email account's username and password set within, but then that didn't make sense to me how is the JavaMailSender going to tell which sender I want each time?!
My code:
In servlet-context.xml:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="default_email#gmail.com" />
<property name="password" value="***1***" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">false</prop>
<prop key="mail.smtp.sendpartial">true</prop>
</props>
</property>
</bean>
[ I thought to add here:
<bean id="anotherMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
...
<property name="username" value="another_email#gmail.com" />
<property name="password" value="***2***" />
...
</bean>
]
And a Java Class responsible for email sending:
public class MailService {
private static JavaMailSender mailSender;
#SuppressWarnings("static-access")
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(final String aSubject, final String aContent, final String toMail, final List<String> attachedFileUrls, String aFilename) {
MimeMessage message = mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, true,"UTF-8");
helper.setFrom(simpleMailMessage.getFrom());
helper.setTo(toMail);
helper.setSubject("A subject");
helper.setText("some content", true);
} catch (Exception e) {...}
Thread thread = new SendMail1(message);
thread.start();
}
class SendMail1 extends Thread {
MimeMessage message;
SendMail1(MimeMessage message) {
this.message = message;
}
public void run() {
mailSender.send(message);
}
}
}
(It doesn't help changing setFrom and setTo functions, because they only set the visual "to" and "from" in recipent's mail box)
At the moment mailSender "knows" somehow by the config settings above to send email to the email set in servlet-context.xml .
I would like to add sendMailFromSpecialSender function which will send email from other sender.
Is this possible?
If it is, then how?
UPDATE:
After posting this question I found a partial answer to my question by Bill Shannon:
The simple solution is to use a separate Session for each sender and send each message one at a time.
So...
1. How do I create a separate Session for my other sender case?
2. Does the configuration in servlet-context.xml enable having a separate session with other configuration, or can I leave that as it is?
Thank-you
Your question seems to be about Spring's dependency injection, and how to inject two beans of the same class.
One good way to do this is to use a #Qualifier:
public void setPrimaryMailSender(#Qualifier("primary") JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void setSecondaryMailSender(#Qualifier("secondary") JavaMailSender mailSender) {
this.secondaryMailSender = mailSender;
}
and then in your bean definitions:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<qualifier value="primary"/>
...
</bean>
<bean id="anotherMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<qualifier value="secondary"/>
...
</bean>
In this way, you can easily inject two fields of the same type.
You just have to use another mailSender in your sendMail() method.
In that method you can have conditional check something like
if(condtion){
MimeMessage=mailSender.createMimeMessage();
else {
MimeMessage=otherSender.createMimeMessage()
}
Similar check in your inner class can help ypu to decide which sender to use.
I am new in spring MVC i have got an issue while send email through spring . no exception will occur but mail not send.
my applicationContext.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="username" value="uname" />
<property name="password" value="pass" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.socketFactory.port">465</prop>
<prop key="mail.smtp.socketFactory.class">javax.net.ssl.SSLSocketFactory</prop>
<prop key="mail.smtp.port">465</prop>
</props>
</property>
</bean>
my controller class
#Controller
public class WebController {
// System.out.println("suceees");
#Autowired
private JavaMailSender mailSender;
#RequestMapping(value = "/index", method = RequestMethod.GET)
public String index() {
return "index";
}
#RequestMapping(value = "/redirect", method = RequestMethod.GET)
public String redirect() {
sendMail();
return "redirect:finalPage";
}
#RequestMapping(value = "/finalPage", method = RequestMethod.GET)
public String finalPage() {
return "final";
}
public void sendMail() {
try {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom("sender");
helper.setTo("receiver");
helper.setSubject("hi");
helper.setText("welcome");
// attach the file
FileSystemResource file = new FileSystemResource(new File("/home/ajmal/Pictures/messi.jpg"));
helper.addAttachment("messi.jpg", file);//image will be sent by this name
mailSender.send(message);
} catch (MailException | MessagingException ex) {
System.err.println("error");
}
}
}
thanks in advance .. no exception will occur. but mail not send ?
We had into the same problem sometime back, with Spring Boot 1.2.5. Looks like with the latest version of Java Mail, now another property is needed - spring.mail.properties.mail.smtp.ssl.enable to be set as true. See this post for details.
Also, when I tested my application, I saw that merely giving the regular gmail password didn't anymore work. I needed a 2-step verified account, and had to use an application password.
I need to send a letter with the body:
Lector {LectorName} had created a new course
----------------------------------------------------
Name: {CourseName}
Category: {CourseCategory}
Description: {CourseDescription}
Links: {CourseLinks}
----------------------------------------------------
Please, review this course at {CourseApproveLink}
I made on a page freemarker
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<p> ${LectorName} had created a new course</p>
<p>----------------------------------------------------</p>
<p>Name: ${Course.title}</p>
<p>Category: ${Course.category.name}</p>
<p>Description: ${Course.descr}</p>
<p>Links: ${Course.links}</p>
<p>----------------------------------------------------</p>
<p>Please, review this course at ${CourseApproveLink}</p>
</body>
</html>
how to fill it and pass the values in the method of sending the letter?
Here is my code. My method sendMail and Bean "mailSender" with my settings.
It is necessary to do so new MimeMessage(session) ?
How do I get the settings from the bean into the session?
#Service("mailService")
public class MailService {
#Autowired
private MailSender mailSender;
#Autowired
private SimpleMailMessage alertMailMessage;
#Resource(name = "freemarkerEmailConfig")
private Configuration emailConfiguration;
public void sendMail(String from, String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(from);
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
public void sendAlertMail(String alert) {
SimpleMailMessage mailMessage = new SimpleMailMessage(alertMailMessage);
mailMessage.setText(alert);
mailSender.send(mailMessage);
}
}
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.mail.ru" />
<property name="port" value="465" />
<property name="username" value="user#mail.ru" />
<property name="password" value="***********" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
<bean id="alertMailMessage" class="org.springframework.mail.SimpleMailMessage">
<property name="from">
<value>dima4nolimit#mail.ru</value>
</property>
<property name="to">
<value>bizanrus#mail.ru</value>
</property>
<property name="subject"
value="Alert - Exception occurred. Please investigate" />
</bean>
You need to pass in a map to the method that sends email with freemarker template. In your case the map will look something like:
Map<Object, Object> map = new HashMap<Object, Object>();
map.put("LectorName", "...");
map.put("Course", course);
map.put("CourseApproveLink", "...");
Freemarker will resolve variable names based on the keys you passed in.
If you use Spring, then configure the template directory in applicationContext.xml like this:
<!-- FreeMarker Configuration -->
<bean id="freemarkerEmailConfig" class="freemarker.template.Configuration">
<property name="directoryForTemplateLoading" value="templates/email" />
<property name="objectWrapper">
<bean class="freemarker.template.DefaultObjectWrapper"/>
</property>
</bean>
Put your template under templates/email folder (relative to your webapp). Inject the freemarkerEmailConfig bean defined in applicationContext into your service class:
#Resource(name = "freemarkerEmailConfig")
private Configuration emailConfiguration;
Now in your service class you can use emailConfiguration to retrieve the template and then process it with the map above like this:
Template template = emailConfiguration.getEmailTemplate(templateName);
String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
FreeMarkerTemplateUtils is a class from Spring. Now text will contain the html with the all the variables substituted by values from the map. Just send the email with text as html content:
MimeMessage msg = mailSender.createMimeMessage();
msg.setFrom(new InternetAddress(EMAIL_SENDER_ADDRESS, EMAIL_SENDER_PERSONAL));
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setContent(text, "text/html; charset=UTF-8");
for (EmailInternetAddress emailInternetAddress :emailInternetAddresses) {
msg.addRecipient(emailInternetAddress.getRecipientType(),
emailInternetAddress.getInternetAddress());
}
mailSender.send(msg);
I want my application to be able to send email on demand. I am currently using the tutorial from www.mykong.com which details setting up gmail settings.
However, I can't get the configuration settings into my annotated spring controllers: I want to avoid using the following:
ApplicationContext context = new FileSystemXmlApplicationContext(email-context.xml");
MailMail mm = (MailMail) context.getBean("mailMail");
mm.send(message);
From my investigation this seems rather frowned upon. I've tried several things, but none of them seem to find the correct bean and produce a nullpointerexception. Is there a way I can add this to any neccessary controllers, e.g. #Property(mailMail) or #Autowired private mailMail? Or should I just move the settings from my email-context into the java itself?
My files are below:
EmailSender.java
public class EmailSender {
#Autowired
private MailSender mailSender;
public void sendMail(String subject, String msg) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom("beesden#*.com");
message.setTo("beesden#*.com");
message.setSubject(subject);
message.setText(msg);
mailSender.send(message);
}
}
PageController
#RequestMapping(value = { "/{name}" }, method = RequestMethod.GET)
public String showPage(#PathVariable("name") String name, HttpServletRequest request, Model model) {
logger.debug("Page request: " + name);
mm.sendMail("Hi","Test");
return "webpage";
}
email-context.xml
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="toby#gmail.com" />
<property name="password" value="tobytobytoby" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="mailMail" class="org.system.EmailSender">
<property name="mailSender" ref="mailSender" />
</bean>
(note I changed the username and password for here, they are correct in the system...)
Many thanks
I have successfully sent simple email using this:
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setTo("someone#abc.example");
mailMessage.setSubject("This is the test message for testing gmail smtp server using spring mail");
mailMessage.setFrom("abc#gmail.com");
mailMessage.setText("This is the test message for testing gmail smtp server using spring mail. \n" +
"Thanks \n Regards \n Saurabh ");
mailSender.send(mailMessage);
What setting I need to chnage so that I can send HTML emails
import javax.mail.internet.MimeMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, "utf-8");
String htmlMsg = "<h3>Hello World!</h3>";
//mimeMessage.setContent(htmlMsg, "text/html"); /** Use this or below line **/
helper.setText(htmlMsg, true); // Use this or above line.
helper.setTo("someone#abc.example");
helper.setSubject("This is the test message for testing gmail smtp server using spring mail");
helper.setFrom("abc#gmail.com");
mailSender.send(mimeMessage);
In Spring this should be done this way:
Your email class:
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
public class HTMLMail
{
private JavaMailSender mailSender;
public void setMailSender(JavaMailSender mailSender) {
this.mailSender = mailSender;
}
public void sendMail(String from, String to, String subject, String msg) {
try {
MimeMessage message = mailSender.createMimeMessage();
message.setSubject(subject);
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(to);
helper.setText(msg, true);
mailSender.send(message);
} catch (MessagingException ex) {
Logger.getLogger(HTMLMail.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
beans:(Spring-Mail.xml)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="youremail#gmail.com" />
<property name="password" value="yourpassword" />
<property name="javaMailProperties">
<props>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean id="htmlMail" class="com.mohi.common.HTMLMail">
<property name="mailSender" ref="mailSender" />
</bean>
</beans>
Usage:
ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Mail.xml");
HTMLMail mm = (HTMLMail) context.getBean("htmlMail");
String html="<p>Hi!</p>Link text";
mm.sendMail("sender#gmail.com",
"receiver#gmail.com",
"test html email",
html);
Full example here .
I don't think that SimpleMailMessage class has such options.
I'm sure that you can do it with JavaMailSender and MimeMessagePreparator, because you need to set MIME content type for HTML.
See this link for help:
http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/mail.html
You might be interested in checking this article: "Rich HTML email in Spring with Thymeleaf" http://www.thymeleaf.org/doc/articles/springmail.html
It uses Thymeleaf as a templating view layer, but the concepts and Spring-specific code explained there are common to all Spring applications.
Besides, it has a companion example application which source code you can use as a base for your needs.
Regards,
Daniel.
Class Level:
public String sendEmailToUsers(String emailId,String subject, String name){
String result =null;
MimeMessage message =mailSender.createMimeMessage();
try {
MimeMessageHelper helper = new MimeMessageHelper(message, false, "utf-8");
String htmlMsg = "<body style='border:2px solid black'>"
+"Your onetime password for registration is "
+ "Please use this OTP to complete your new user registration."+
"OTP is confidential, do not share this with anyone.</body>";
message.setContent(htmlMsg, "text/html");
helper.setTo(emailId);
helper.setSubject(subject);
result="success";
mailSender.send(message);
} catch (MessagingException e) {
throw new MailParseException(e);
}finally {
if(result !="success"){
result="fail";
}
}
return result;
}
XML Level:
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="********#gmail.com" />
<property name="password" value="********" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
String emailMessage = report.toString();
Map velocityContext = new HashMap();
velocityContext.put("firstName", "messi");
velocityContext.put("Date",date );
velocityContext.put("Exception",emailMessage );
String text = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "VelocityTemplate.vm","UTF-8", velocityContext);
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper;
helper = new MimeMessageHelper(message, true);
helper.setTo("abc#gmail.com");
helper.setFrom("xyz#gmail.com");
helper.setSubject("new email");
helper.setText(text, true);
mailSender.send(message);