0

我正在使用涉及发送电子邮件(使用 SMTP 服务器)的 Spring 框架在 Java 中构建银行应用程序,但我听说它不安全。那么如何在 Java 中使 SMTP 安全?SSL 层和/或 HTTPS 连接是否足够?请帮忙。

谢谢。

4

2 回答 2

0

您需要对 SMTP 服务器进行配置更改,以便服务器仅中继来自特定 ID 的邮件并忽略其他 ID,而不是在您的 Java 应用程序中使 SMTP 安全。

于 2013-09-21T11:53:41.230 回答
0

显然,您可以将 SMTP over SSL 与 Spring 一起使用。这是示例:

XML 资源

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="465" />
<property name="protocol" value="smtps" />
<property name="username" value="yourAccount@gmail.com"/>
<property name="password" value="yourPassword"/>
<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>

<bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" >
<property name="from" value="yourAccount@gmail.com" />
<property name="subject" value="Your Subject" />
</bean>

</beans>

测试班

package test.mail;

import org.springframework.mail.MailException;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.test.AbstractDependencyInjecti onSpringContextTests;

/**
* MailTest.
* @author jalarcon
*/
public class MailTest extends AbstractDependencyInjectionSpringContextTests {

private MailSender mailSender;
private SimpleMailMessage mailMessage; 

/* (non-Javadoc)
* @see org.springframework.test.AbstractSingleSpringConte xtTests#getConfigLocations()
*/
@Override
protected String[] getConfigLocations() {
return new String[] {"/beanDictionary/mail.xml"};
}

public void testSendMail() {
//Create a thread safe "sandbox" of the message
SimpleMailMessage msg = new SimpleMailMessage(this.mailMessage);
msg.setTo("yourAccount@gmail.com");
msg.setText("This is a test");
try{
mailSender.send(msg);
} catch(MailException ex) {
throw new RuntimeException(ex);
} 
}

// ---------------------------------------------------------- getters/setters

/**
* @return the mailSender
*/
public MailSender getMailSender() {
return mailSender;
}

/**
* @param mailSender the mailSender to set
*/
public void setMailSender(MailSender mailSender) {
this.mailSender = mailSender;
}

/**
* @return the mailMessage
*/
public SimpleMailMessage getMailMessage() {
return mailMessage;
}

/**
* @param mailMessage the mailMessage to set
*/
public void setMailMessage(SimpleMailMessage mailMessage) {
this.mailMessage = mailMessage;
}

} 
于 2013-09-21T12:00:51.947 回答