Spring 通过 Google 发送电子邮件 ()

发布于 2024-11-30 04:39:23 字数 1572 浏览 1 评论 0原文

下面的代码运行良好

但是,我的问题是如何以编程方式设置“javaMailProperties”? 因为我想从代码中设置 ssl/tsl 。我无法访问这些属性,我不知道为什么,感谢您的解决方案和解释。

SimpleMailMessage message=(SimpleMailMessage)SpringUtil.getContext().
getBean("templateMessage");

JavaMailSenderImpl mailSender = (JavaMailSenderImpl)SpringUtil.getContext()
.getBean("mailSender"); 

mailSender.send(message);

--applicationcontext.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="your gmail address"/>
   <property name="password" value="your password"/>        

   <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
</property>

 </bean>

<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="[email protected]"/>
    <property name="to" value="[email protected]"/>              
    <property name="subject" value="subject"/>
    <property name="text" value="hello"/>               
 </bean>

(我不想使用javax.mail方法,有人问)

The code below works well

But, my question is how can set the 'javaMailProperties' programaticaly ?
Because I would like to set ssl/tsl from the code. I could not access these properties, I dont know why, thanks for the solution and explanation.

SimpleMailMessage message=(SimpleMailMessage)SpringUtil.getContext().
getBean("templateMessage");

JavaMailSenderImpl mailSender = (JavaMailSenderImpl)SpringUtil.getContext()
.getBean("mailSender"); 

mailSender.send(message);

--applicationcontext.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="your gmail address"/>
   <property name="password" value="your password"/>        

   <property name="javaMailProperties">
    <props>
        <prop key="mail.smtp.auth">true</prop>
        <prop key="mail.smtp.starttls.enable">true</prop>
    </props>
</property>

 </bean>

<bean id="templateMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="[email protected]"/>
    <property name="to" value="[email protected]"/>              
    <property name="subject" value="subject"/>
    <property name="text" value="hello"/>               
 </bean>

(I dont want to use javax.mail approach, it was asked)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Bonjour°[大白 2024-12-07 04:39:23

用途:

mailSender.getJavaMailProperties().setProperty("mail.smtp.starttls.enable", "true");

与您想要以编程方式设置的其他属性相同。

编辑:

我检查了JavaMailSenderImpl类的源代码:

/**
 * Allow Map access to the JavaMail properties of this sender,
 * with the option to add or override specific entries.
 * <p>Useful for specifying entries directly, for example via
 * "javaMailProperties[mail.smtp.auth]".
 */
public Properties getJavaMailProperties() {
    return this.javaMailProperties;
}

如您所见,getJavaMailProperties是一个公共方法,应该可供您使用。我的Spring框架版本是3.0.5。

Use:

mailSender.getJavaMailProperties().setProperty("mail.smtp.starttls.enable", "true");

Same for other properties that you want to set programatically.

EDIT:

I checked the source code for the JavaMailSenderImpl class:

/**
 * Allow Map access to the JavaMail properties of this sender,
 * with the option to add or override specific entries.
 * <p>Useful for specifying entries directly, for example via
 * "javaMailProperties[mail.smtp.auth]".
 */
public Properties getJavaMailProperties() {
    return this.javaMailProperties;
}

As you can see, the getJavaMailProperties is a public method and should be available to you. My Spring framework version is 3.0.5.

无人问我粥可暖 2024-12-07 04:39:23

快速谷歌未验证。
在 mailSender.getJavaMailProperties.setProperty("mail.smtp.auth",true) 上调用 setProperty(String key, String value)

http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

A quick google not verified.
Call setProperty(String key, String value) on the mailSender.getJavaMailProperties.setProperty("mail.smtp.auth",true)

http://download.oracle.com/javase/6/docs/api/java/util/Properties.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文