Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在不安装SMTP服务器的情况下测试Greenmail_Java_Spring_Email_Greenmail - Fatal编程技术网

Java 在不安装SMTP服务器的情况下测试Greenmail

Java 在不安装SMTP服务器的情况下测试Greenmail,java,spring,email,greenmail,Java,Spring,Email,Greenmail,我正在尝试使用Greenmail在本地主机上对电子邮件功能进行单元测试。问题是我还没有安装SMTP服务器,我觉得安装一个服务器太麻烦了。我的期望是,应该有一个免费的库,允许我保持发送邮件代码不变,但不是真正将电子邮件发送到SMTP服务器,而是将其发送到我的本地计算机,以便我可以检索它们(为了单元测试) 我曾经在使用Grails开发时使用过Greenmail,体验非常好。但是我找不到类似于Spring框架的东西。在页面中,它说有一个与JBoss捆绑的模拟SMTP服务器。但我不想运行JBoss,因为

我正在尝试使用Greenmail在本地主机上对电子邮件功能进行单元测试。问题是我还没有安装SMTP服务器,我觉得安装一个服务器太麻烦了。我的期望是,应该有一个免费的库,允许我保持发送邮件代码不变,但不是真正将电子邮件发送到SMTP服务器,而是将其发送到我的本地计算机,以便我可以检索它们(为了单元测试)

我曾经在使用Grails开发时使用过Greenmail,体验非常好。但是我找不到类似于Spring框架的东西。在页面中,它说有一个与JBoss捆绑的模拟SMTP服务器。但我不想运行JBoss,因为目前我的web应用程序正在Tomcat上运行

Tomcat已经有类似的服务了吗?或者有没有更好的方法将测试电子邮件发送到localhost,在那里我可以检索发送的电子邮件

更新

我尝试使用拉尔夫的方法,但仍然产生相同的例外:

[SimpleAsyncTaskExecutor-1] 2012-03-13 10:19:39,475 - ERROR: com.test.my.service.emailing.impl.EmailServiceImpl - Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect. Failed messages: javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect; message exception details (1) are:
Failed message 1:
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 3025;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1391)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
    at javax.mail.Service.connect(Service.java:288)
    at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:389)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:340)
    at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:336)
    at com.test.my.service.emailing.impl.EmailServiceImpl.test(EmailServiceImpl.java:388)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:110)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
    at org.springframework.aop.interceptor.AsyncExecutionInterceptor$1.call(AsyncExecutionInterceptor.java:80)
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
    ... 19 more
您可以与任何Java程序一起使用,无论它是否使用Spring。您不需要任何特定于Spring的东西

它运行某种进程内邮件服务器

import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetupTest; ...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("ApplicationContext-Greenmail.xml")
public class EmailServiceIntegrationTest {

    /** Class under test */
    @Resource
    private EmailService emailService;

    private GreenMail greenMail;

    @Before
    public void startMailServer() {
        greenMail = new GreenMail(ServerSetupTest.SMTP);
        greenMail.start();
    }

    @After
    public void stopMailServer() {
        greenMail.stop();
    }

    @Test
    public void testPledgeReminder()
                throws InterruptedException, MessagingException {

        String mailText = "Hallo World";
        String mailSubject = "Hallo";
        String mailTo = "test@excaple.com";

        /** when: sending a mail */
        emailService.send(mailSubject, mailTo, mailText);

        assertTrue(greenMail.waitForIncomingEmail(5000, 1));
        Message[] messages = greenMail.getReceivedMessages();
        assertEquals(1, messages.length);
        assertEquals(mailText, messages[0].getSubject());       
        String body = GreenMailUtil.getBody(messages[0]).replaceAll("=\r?\n", "");
        assertEquals(mailText, body);       
    }

}
重要提示:使用端口3025

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="javaMailProperties">
        <util:properties>
            <prop key="mail.debug">false</prop>
            <prop key="mail.transport.protocol">smtp</prop>
            <prop key="mail.smtp.port">3025</prop>
            <prop key="mail.smtp.auth">true</prop>
            <prop key="mail.smtp.user">test@mail.extern</prop>
            <prop key="mail.smtp.host">localhost</prop>
            <prop key="mail.smtp.from">test@mail.extern</prop>
        </util:properties>
    </property>
    <property name="username" value="test"/>
    <property name="password" value="xxx"/>
    <property name="defaultEncoding" value="utf8" />
</bean>

假的
smtp
3025
真的
test@mail.extern
本地服务器
test@mail.extern

然后,此配置将配置Spring JavaMailSender,以将其邮件发送到由测试代码启动并完成的GreenMail服务器。

似乎有人曾经提出过这样的集成:

不知道它是否还在工作…

编辑(2017年11月):

Dumbster link已经死了,github上有一个叉

----原始消息----

看看Dumbster——我在所有单元测试中都使用它,因为它允许我针对实际发送的电子邮件进行断言


spring
测试中这样做对我来说很好:

ServerSetupTest.setPortOffset(<SOME_CUSTOM_PORT>);
testEmailServer = new GreenMail();
testEmailServer.start();
ServerSetupTest.setPortOffset();
testEmailServer=new GreenMail();
testEmailServer.start();

ServerSetupTest
类背后的主要思想是使用用户定义的端口偏移量创建一个
ServerSetup
,用于测试。有关详细信息,请参阅。

尝试手动设置GreenMail服务器的端口,如下所示:

 @Before
public void startMailServer()
    throws Exception
{
    ServerSetup setup = new ServerSetup(3025,"localhost","smtp");
    greenMail = new GreenMail(setup);
    greenMail.start();
}
然后将您的电子邮件发件人的端口设置为与上述相同,请注意协议/主机/端口:

email.protocol=smtp
email.host=localhost
email.port=3025
email.username=to@localhost.com
email.password=to@localhost.com
email.auth=true
email.systemEmail=from@localhost.com
使用bean配置:

<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
    <property name="protocol" value="${email.protocol}" />
    <property name="host" value="${email.host}" />
    <property name="port" value="${email.port}" />
    <property name="username" value="${email.username}" />
    <property name="password" value="${email.password}" />
    <property name="javaMailProperties">
        <props>
            <prop key="mail.${email.protocol}.auth">${email.auth}</prop>
        </props>
    </property>
</bean>

${email.auth}

是否使用端口3025?在搜索之后,我没有听到任何关于这方面的消息,但似乎值得一看我在问题中的更新。我不能让它工作我的防火墙好像有问题。我现在工作很好。谢谢你的支持!关于3025神奇的数字:我终于发现它是Greenmail使用的默认SMTP端口的值。可以使用Greenmail类的构造函数来修改它。Greenmail类用于测试邮件发送函数。喜欢它,汉克斯,但你似乎误解了我的需要。我想使用JavaMail,但不知怎么配置greenmail来接收电子邮件。通过这种方式,我不需要来回更改代码(在测试时切换为Greenmail类,在生产中切换回JavaMail以发送真正的电子邮件),我更喜欢Greenmail,因为我在Grails上有很好的使用经验。但如果事情不顺利,我会让邓布斯特试试。谢谢