HTTP状态500-javax.mail.MessagingException:无法连接到SMTP主机:localhost,端口:25,响应:421

时间:2018-07-21 15:47:51

标签: java spring jsp servlets

这是我的发送电子邮件代码。 发送电子邮件时出现错误。

package cs520.email;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/Email")
public class Email extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public Email()
    {
        super();
    }

    protected void doPost( HttpServletRequest request,
        HttpServletResponse response ) throws ServletException, IOException
    {
        String from = request.getParameter( "from" );
        String to = request.getParameter( "to" );
        String subject = request.getParameter( "subject" );
        String content = request.getParameter( "content" );

        Properties props = System.getProperties();
        props.put( "mail.smtp.host", "localhost" );
        Session session = Session.getInstance( props );

        Message msg = new MimeMessage( session );
        try
        {
            msg.setFrom( new InternetAddress( from ) );
            msg.setRecipient( RecipientType.TO, new InternetAddress( to ) );
            msg.setSubject( subject );
            msg.setText( content );

            Transport.send( msg );
        }
        catch( Exception e )
        {
            throw new ServletException( e );
        }

        response.sendRedirect( "Email.html" );
    }

}

在这里我附上关于我的输出的图像。 enter image description here

0 个答案:

没有答案