使用用户在Spring中输入用户名和密码发送邮件

时间:2014-07-22 07:30:17

标签: java spring email

我的应用程序需要使用Spring API从用户输入的用户名和密码发送邮件。这是我的应用程序上下文文件的外观。

<?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="xyz@xyz.com" />  
    <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> 

我的控制器看起来像是

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");
    }
}

现在我的要求是

  • 我不想使用属性文件。
  • 我希望用户名和密码是用户输入的用户名和密码。

我只找到使用属性文件的解决方案。但在我的情况下,我不想使用该文件。

有没有办法这样做?

1 个答案:

答案 0 :(得分:3)

您可以通过从用户获取数据来设置MailController(根据您的要求设置或覆盖)

 public String sendMailToIt(HttpServletRequest request)
 {
     .....
     JavaMailSenderImpl jMailSender = (JavaMailSenderImpl)sender;

     jMailSender.setUsername(userName);
     jMailSender.setPassword(password);

     ....
     jMailSender.send(mail);

     ....
 }