Reputation: 5868
My application requires sending a mail from the user entered user name and password using Spring API. Here's how my Application Context file look.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:component-scan base-package="net.mail" />
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="25"/>
<property name="username" value="[email protected]" />
<property name="password" value="xyz" />
<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">true</prop>
</props>
</property>
</bean>
</beans>
And my Controller looks like following
package net.mail;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MailController
{
@Autowired
MailSender sender;
@ResponseBody
@RequestMapping("/send")
public String sendMailToIt(HttpServletRequest request)
{
try
{
String from=request.getParameter("from");
String to=request.getParameter("to");
String sub=request.getParameter("subject");
String username=request.getParameter("username");
String password=request.getParameter("password");
//username and password should be used for authentication
SimpleMailMessage mail=new SimpleMailMessage();
mail.setFrom(from);
mail.setTo(to);
mail.setSubject(sub);
mail.setText("hello");
sender.send(mail);
}
catch(Exception e)
{
e.printStackTrace();
return "Exception Occured";
}
return new String("mail sent");
}
}
Now my requirements are
I only found solutions using the properties file. but in my case i do not want to use that file.
Is there any way of doing so?
Upvotes: 3
Views: 5908
Reputation: 1252
You can set in your MailController by getting data from the user ( set or override based on your requirement)
public String sendMailToIt(HttpServletRequest request)
{
.....
JavaMailSenderImpl jMailSender = (JavaMailSenderImpl)sender;
jMailSender.setUsername(userName);
jMailSender.setPassword(password);
....
jMailSender.send(mail);
....
}
Upvotes: 3