我试图模拟发送电子邮件(出于 JUnit v4.8.1 测试的目的)并决定使用我通过 SO 找到的 Dumbster。我使用的是 1.6 版。我的 JUnit 测试中有这个……</p>
SimpleSmtpServer server = SimpleSmtpServer.start();
boolean ret = m_emailSvc.sendEmail("me@me.com",
"you@you.com",
"localhost",
"Test",
"Test Body");
Assert.assertTrue(ret);
server.stop();
我用这种方式发送电子邮件……</p>
public boolean sendEmail(final String toEmail,
final String fromEmail,
final String smtpHost,
final String subject,
final String body)
{
boolean ret = true;
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("mail.smtp.host", smtpHost);
// Get the default Session object.
Session session = Session.getDefaultInstance(properties);
try{
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);
// Set From: header field of the header.
message.setFrom(new InternetAddress(fromEmail));
// Set To: header field of the header.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(toEmail));
// Set Subject: header field
message.setSubject(subject);
// Now set the actual message
message.setText(body);
// Send message
Transport.send(message);
}catch (MessagingException mex) {
ret = false;
LOG.error(mex.getMessage(), mex);
} // try
return ret;
} // sendEmail
这会失败,但以下例外。有谁知道我做错了什么,或者有没有更简单的方法来模拟在 JUnit 测试中发送电子邮件?
java.net.BindException: Permission denied
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.PlainSocketImpl.socketBind(PlainSocketImpl.java:521)
at java.net.PlainSocketImpl.bind(PlainSocketImpl.java:414)
at java.net.ServerSocket.bind(ServerSocket.java:326)
at java.net.ServerSocket.<init>(ServerSocket.java:192)
at java.net.ServerSocket.<init>(ServerSocket.java:104)
at com.dumbster.smtp.SimpleSmtpServer.run(Unknown Source)
at java.lang.Thread.run(Thread.java:680)
[ERROR]: org.mainco.subco.email.service.EmailServiceImpl - Could not connect to SMTP host: localhost, port: 25
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
嵌套异常是: java.net.ConnectException: Connection denied at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)在 javax.mail.Service.connect(Service.java:295) 在 javax.mail.Service.connect(Service.java:176) 在 javax.mail.Service.connect(Service.java:125) 在 javax.mail。 Transport.send0(Transport.java:194) 在 javax.mail.Transport.send(Transport.java:124) 在 org.mainco.subco.email.service.EmailServiceImpl.sendEmail(EmailServiceImpl.java:62) 在 org.mainco .subco.email.service.EmailServiceTest.testSendEmail(EmailServiceTest.java:23) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl。invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.junit.runners.model.FrameworkMethod $1.runReflectiveCall(FrameworkMethod.java:44) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 在 org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner .java:71) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner .java:191) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 在 org.junit.runners.ParentRunner。在 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.run(JUnit4TestReference.java:50) 处运行 (ParentRunner.java:236) java:38) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)在组织。eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 原因:java.net。 ConnectException:在 java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241) 在 java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382) 在 java.net.PlainSocketImpl.socketConnect(Native Method) 的连接被拒绝.PlainSocketImpl.connect(PlainSocketImpl.java:228) 在 java.net.SocksSocketImpl.connect(SocksSocketImpl.java:384) 在 java.net.Socket.connect(Socket.java:527) 在 java.net.Socket.connect( Socket.java:476) 在 com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288) 在 com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)在 com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)