简体   繁体   English

如何使用Java使SMTP服务器安全

[英]How to make SMTP Server secure using Java

I am building a banking application in Java using Spring framework that involved sending email (using SMTP server) but I heard that it's not secure. 我正在使用Spring框架使用Java构建Java银行应用程序,该应用程序涉及发送电子邮件(使用SMTP服务器),但是听说它并不安全。 So how can I make SMTP secure in Java? 那么如何在Java中使SMTP安全呢? Is SSL layer and/or HTTPS connection sufficient? SSL层和/或HTTPS连接是否足够? Please help. 请帮忙。

Thanks. 谢谢。

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

Apparently you can use SMTP over SSL with Spring. 显然,您可以在Spring上使用基于SSL的SMTP。 Here's the sample: 这是示例:

XML Resource 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>

Test Class 测试班

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;
}

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM