数据库中的Spring Javamail属性

时间:2014-02-21 04:50:14

标签: java database spring spring-mvc javamail

我正在尝试编写Spring(我使用3.0)应用程序并使用Spring包含的Javamail。 我希望将javamail属性(stmp服务器,端口,用户名,传递等)存储在数据库中以便于更新/更改。我已经看到了在applicationContext.xml or in a properties文件中设置Javamail属性的示例。

但是is there a way to use a database to store the email properties and retrieve them every time I need to send a e-mail?

我的ApplicationContex.xml

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="defaultEncoding" value="UTF-8" />
    <property name="host" value="smtp.gmail.com" />
    <property name="port" value="465" />
    <property name="protocol" value="smtps" />
    <property name="username" value="test@gmail.com" />
    <property name="password" value="***********" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.smtps.auth">true</prop>
            <prop key="mail.smtps.starttls.enable">true</prop>
            <prop key="mail.smtps.debug">true</prop>
        </props>
    </property>
</bean>

感谢您的帮助。

3 个答案:

答案 0 :(得分:6)

当然,你可以。

1)第一种方式

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"/>

在控制器中

@Autowired 
JavaMailSender mailSender


@PostConstruct
public void init(){
    // connect to database
    // obtain properties
    JavaMailSenderImpl ms = (JavaMailSenderImpl) mailSender;
    ms.set...
    ms.set...
    ms.set...
}

2)第二种方式

public class DatabaseBasedMailSender extends JavaMailSenderImpl{

    public DatabaseBasedMailSender(){
        // connect to database
        // obtain properties

        setHost(...)
        setProtocol(...)
        ...

    }


}

<bean id="mailSender" class="my.project.DatabaseBasedMailSender"/>

我确信有可能找到另一种方法来做到这一点。     

答案 1 :(得分:1)

一种方法是创建一个单例bean来初始化您所有与电子邮件相关的属性:

  1. 从数据库获取所有与电子邮件相关的属性,并将其设置为 系统/环境属性
  2. 使用System.getProperty("propertyName")

    按照下面的要求获取所有这些属性。
    @Configuration
    public class EmailConfig {
    
    @Bean
    public JavaMailSender getJavaMailSender() {
    
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("");
        mailSender.setPort(Integer.valueOf("");
        mailSender.setDefaultEncoding("");
        mailSender.setUsername("");
        mailSender.setPassword("");
    
        Properties props = mailSender.getJavaMailProperties();
        props.put("spring.mail.protocol","");
        props.put("mail.smtps.starttls.enable","");
        props.put("mail.smtp.auth","");
    
        return mailSender;
       }
    }
    

答案 2 :(得分:0)

你也可以创建一个属性文件,从那里你可以获得所有的值,如主机,用户名,密码,我不认为存储在数据库中是个好主意,但如果你想要你可以使用..无论何时想要更改邮件设置,都可以进入属性 文件并在那里更改,我创建了单独的util包用于发送邮件

public void sendMailWithAttachments(String to, String subject,
        String content, String[] attachFiles) {

    try {
        final String userName = com.sheel.property.Properties.userName;
        final String password = com.sheel.property.Properties.password;
        String from = com.sheel.property.Properties.mailFrom;
        Properties props = new Properties();
        props.put("mail.smtp.host",
                com.sheel.property.Properties.SMTP_HOST);
        props.put("mail.smtp.socketFactory.port",
                com.sheel.property.Properties.SMTP_PORT);
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port",
                com.sheel.property.Properties.SMTP_PORT);

        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication 

                    getPasswordAuthentication() {
        return new    PasswordAuthentication(userName,
                                password);
                    }
                });

        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress(from));
        msg.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse(to));
        msg.setSubject(subject);
        msg.setSentDate(new Date());

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setContent(content, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(messageBodyPart);

        if (attachFiles != null && attachFiles.length > 0) {
            for (String filePath : attachFiles) {
                MimeBodyPart attachPart = new MimeBodyPart();

                try {
                    attachPart.attachFile(filePath);
                } catch (IOException ex) {
                    ex.printStackTrace();
                    throw ex;
                }

                multipart.addBodyPart(attachPart);
            }
        }

        msg.setContent(multipart);

        Transport.send(msg);

        System.out.println("Mail sent");
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

这是属性文件

public interface Properties {
String SMTP_HOST = "smtpout.secureserver.net";
String SMTP_PORT = "465";
String incoming_server_host = "pop.secureserver.net";
String incoming_server_port = "995";
String userName = "test@gmail.com";
String password = "password";
}