2014-07-22 46 views
3

我的應用程序需要使用Spring API從用戶輸入的用戶名和密碼發送郵件。這是我的應用程序上下文文件的外觀。用Spring用戶輸入的用戶名和密碼發送郵件

<?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> 

我的控制器看起來像下面

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

現在,我的要求是

  • 我不希望使用屬性文件。
  • 我想輸入用戶名和密碼。

我只找到使用屬性文件的解決方案。但在我的情況下,我不想使用該文件。

有沒有這樣做的方法?

回答

2

您可以通過從用戶獲取數據在你MailController設置(設置或覆蓋根據您的要求)

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

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

    .... 
    jMailSender.send(mail); 

    .... 
} 
+0

好吧得到了解決。 –

+0

@Darshan Lila如果上述答案解決了您的問題,請接受爲答案。謝謝 :) – vinayknl

相關問題