如何在spring config中创建javax.mail.Session

时间:2016-06-24 06:04:57

标签: java spring email spring-mvc

id MainScree()
{

id mainScreen = [UIScreen mainScreen];
    CGRect bounds = [mainScreen bounds];
    id containingView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];

UIView * mainView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, bounds.size.width, bounds.size.height)];
mainView.backgroundColor = [UIColor whiteColor];
[containingView addSubview:mainView];



 // Here is where all my images, buttons and labels are created and then added to the mainView. 

}

我只想为此会话配置一个spring bean配置。如何在spring xml ??

中创建javax.mail.Authenticator()bean

2 个答案:

答案 0 :(得分:3)

我找到了解决方案..

<bean id="passwordAuth" class="javax.mail.PasswordAuthentication">
        <constructor-arg>
            <value>${sender.email}</value>
        </constructor-arg>
        <constructor-arg>
            <value>${sender.password}</value>
        </constructor-arg>
    </bean>

<bean id="authenticator" class="javax.mail.Authenticator">
    <lookup-method name="getPasswordAuthentication" bean="passwordAuth" />
</bean>

<bean id="emailSession" class="javax.mail.Session">
    <constructor-arg>
        <props>
            <prop key="mail.smtp.auth">${mail.smtp.auth}</prop>
            <prop key="mail.smtp.starttls.enable">${mail.smtp.starttls.enable}</prop>
            <prop key="mail.smtp.host">${mail.smtp.host}</prop>
            <prop key="mail.smtp.socketFactory.port">${mail.smtp.port}</prop>
            <prop key="mail.smtp.socketFactory.class">${mail.smtp.socketFactory.class}</prop>
            <prop key="mail.smtp.port">${mail.smtp.port}</prop>
        </props>
    </constructor-arg>
    <constructor-arg ref="authenticator" />
</bean>

答案 1 :(得分:0)

Spring提供了org.springframework.mail包,用于封装Java Mail bean并简化了电子邮件组件。

例如 -

http://www.mkyong.com/spring/spring-sending-e-mail-via-gmail-smtp-server-with-mailsender/