Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用应用程序上下文设置spring电子邮件_Java_Spring_Annotations_Jakarta Mail - Fatal编程技术网

Java 使用应用程序上下文设置spring电子邮件

Java 使用应用程序上下文设置spring电子邮件,java,spring,annotations,jakarta-mail,Java,Spring,Annotations,Jakarta Mail,我希望我的应用程序能够按需发送电子邮件。我目前正在使用教程,其中详细介绍了如何设置gmail设置 但是,我无法将配置设置放入带注释的spring控制器:我希望避免使用以下选项: ApplicationContext context = new FileSystemXmlApplicationContext(email-context.xml"); MailMail mm = (MailMail) context.getBean("mailMail"); mm.send(message); 从我

我希望我的应用程序能够按需发送电子邮件。我目前正在使用教程,其中详细介绍了如何设置gmail设置

但是,我无法将配置设置放入带注释的spring控制器:我希望避免使用以下选项:

ApplicationContext context = new FileSystemXmlApplicationContext(email-context.xml");
MailMail mm = (MailMail) context.getBean("mailMail");
mm.send(message);
从我的调查来看,这似乎相当令人不快。我尝试了几种方法,但似乎都没有找到正确的bean并产生nullpointerexception。是否有办法将此添加到任何必要的控制器,例如
@Property(mailMail)
@Autowired private mailMail
?或者我应该将设置从电子邮件上下文移动到java本身中吗

我的档案如下:

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";
}
电子邮件上下文.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>

真的
真的
(注意,我更改了此处的用户名和密码,它们在系统中是正确的…)

非常感谢