【问题标题】:Sending email by Spring via Google ()Spring 通过 Google 发送电子邮件 ()
【发布时间】:2011-08-17 13:23:02
【问题描述】:

下面的代码运行良好

但是,我的问题是如何以编程方式设置“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="from@gmail.com"/>
    <property name="to" value="to@gmail.com"/>              
    <property name="subject" value="subject"/>
    <property name="text" value="hello"/>               
 </bean>

(我不想用javax.mail的方式,有人问过)

【问题讨论】:

    标签: java spring gmail


    【解决方案1】:

    用途:

    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。

    【讨论】:

    • 我们遇到了一些问题,没有 getJavaMailProperties(),但是 setJavaMailProperties 退出了 :) 感谢您的努力和您的时间
    • @cscsaba 我为JamaMailSenderImpl 类添加了相关的源代码。 getJavaMailProperties() 是一个公共方法,应该可供您使用。
    • Marcelo: 等一下,看看这个 url mvnrepository.com/artifact/springframework,我知道 Spring 3 已经出来了,maven repo 没有更新版本,所以我用 Springframework 2.0 版本制作了这个测试用例
    • @cscsaba 您实际上应该查看此链接mvnrepository.com/artifact/org.springframework。最新版本是 3.0.5.RELEASE。
    • 我设法得到了 Spring 3,而你的 getJavaMailProperties 就在那里,很抱歉造成这种混乱
    【解决方案2】:

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

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

    【讨论】:

    • 我可以对 Marcelo 说同样的话(见上文)我们遇到了一些问题,没有 getJavaMailProperties(),但 setJavaMailProperties 退出了 :) 感谢您的帮助
    猜你喜欢
    • 2021-04-18
    • 2016-08-29
    • 2017-10-15
    • 1970-01-01
    • 2015-07-12
    • 2015-08-29
    • 2010-10-19
    • 1970-01-01
    • 2020-12-15
    相关资源
    最近更新 更多