私は電子メールの送信を模擬しようとしており (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: com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934) で接続が拒否されました com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638) javax.mail.Service.connect(Service.java:295) で javax.mail.Service.connect(Service.java:176) で javax.mail.Service.connect(Service.java:125) で javax.mail. org.mainco.subco.email.service.EmailServiceImpl.sendEmail(EmailServiceImpl.java:62) の javax.mail.Transport.send(Transport.java:124) の Transport.send0(Transport.java:194) .subco.email.service.EmailServiceTest.testSendEmail(EmailServiceTest.java:23) で、sun.reflect.NativeMethodAccessorImpl.invoke0(ネイティブ メソッド) で、sun.reflect.NativeMethodAccessorImpl.org.junit.runners.model.FrameworkMethod で java.lang.reflect.Method.invoke(Method.java:597) で sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) で (NativeMethodAccessorImpl.java:39) を呼び出す$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. java:38) org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) で org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) で組織で。org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) での eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 原因: java.net。 ConnectException: java.net.PlainSocketImpl.socketConnect(Native Method) で java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:382) で java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:241) で接続が拒否されました。 .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)