java-在Spring中使用用户输入的用户名和密码发送邮件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在Spring中使用用户输入的用户名和密码发送邮件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2958字,纯文字阅读大概需要5分钟。
内容图文
我的应用程序需要使用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");
}
}
现在我的要求是
>我不想使用属性文件.
>我希望用户名和密码成为用户输入的用户名和密码.
我仅使用属性文件找到了解决方案.但就我而言,我不想使用该文件.
有什么办法吗?
解决方法:
您可以通过从用户获取数据来在MailController中进行设置(根据需要进行设置或覆盖)
public String sendMailToIt(HttpServletRequest request)
{
.....
JavaMailSenderImpl jMailSender = (JavaMailSenderImpl)sender;
jMailSender.setUsername(userName);
jMailSender.setPassword(password);
....
jMailSender.send(mail);
....
}
内容总结
以上是互联网集市为您收集整理的java-在Spring中使用用户输入的用户名和密码发送邮件全部内容,希望文章能够帮你解决java-在Spring中使用用户输入的用户名和密码发送邮件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。