错误信息如下:
Exception in thread "Thread-0" java.lang.RuntimeException: java.net.BindException: Permission denied (Bind failed)
at com.icegreen.greenmail.smtp.SmtpServer.run(SmtpServer.java:54)
Caused by: java.net.BindException: Permission denied (Bind failed)
at java.base/java.net.PlainSocketImpl.socketBind(Native Method)
at java.base/java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:436)
at java.base/java.net.ServerSocket.bind(ServerSocket.java:395)
at java.base/java.net.ServerSocket.(ServerSocket.java:257)
at com.icegreen.greenmail.AbstractServer.openServerSocket(AbstractServer.java:51)
at com.icegreen.greenmail.smtp.SmtpServer.run(SmtpServer.java:48)
问题分析:
Permission denied(Bind failed):权限限制(绑定失败)
由于java脚本中启动greenmail时,配置了使用25端口。在实际中java程序绑定低于1024端口都会报出上述错误信息。
解决方法:使用ServerSetup类配置使用3025端口来启动SMTP。
import com.icegreen.greenmail.util.GreenMail;
import com.icegreen.greenmail.util.GreenMailUtil;
import com.icegreen.greenmail.util.ServerSetup;
public class GreenMailTest
{
private GreenMail greenMail;
@Before
public void startMailServer()
throws Exception
{
ServerSetup setup = new ServerSetup(3025,"localhost","smtp");
greenMail = new GreenMail(setup);
greenMail.setUser("test@test.com","123456");
greenMail.start();
}
...
}
当然,向greenmail server发送邮件时,也需要连接3025端口。