簡體   English   中英

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

[英]How to make SMTP Server secure using Java

我正在使用Spring框架使用Java構建Java銀行應用程序,該應用程序涉及發送電子郵件(使用SMTP服務器),但是聽說它並不安全。 那么如何在Java中使SMTP安全呢? SSL層和/或HTTPS連接是否足夠? 請幫忙。

謝謝。

無需在Java應用程序中使SMTP安全,您需要對SMTP服務器進行配置更改,以便該服務器僅中繼來自特定ID的郵件,而忽略其他ID。

顯然,您可以在Spring上使用基於SSL的SMTP。 這是示例:

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

} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM