Transport.send(message) 在下面的代码中不起作用.. netbeans 卡在运行部分.它不会继续……它永远挂在那里 [英] Transport.send(message) not working in the below code.. netbeans gets stuck at the running part. it doesn't proceed furthur.. it hangs there forever

查看:35
本文介绍了Transport.send(message) 在下面的代码中不起作用.. netbeans 卡在运行部分.它不会继续……它永远挂在那里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试编写代码以使用 Java 发送电子邮件.但是这段代码不起作用.执行代码时,它会卡在 transport.send(message) 处.它永远卡在那里.另外我不确定其余的代码是否正确.

I have tried to write a code to send email using Java. But this code is not working. When the code is executed it gets stuck at transport.send(message). It's stuck there forever. Also I am not sure if the rest of the code is correct or not.

  //first from, to, subject, & text values are set
    public class SendMail {
    private String from;
    private String to;
    private String subject;
    private String text;


    public SendMail(String from, String to, String subject, String text){
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    //send method is called in the end 
    public void send(){

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");

        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);

        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {
            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        try {
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
                    simpleMessage.setText(text);
            Transport.send(simpleMessage);  // this is where code hangs     
        } catch (MessagingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }       
    }
}

推荐答案

我遇到了完全相同的问题,其他人报告了间歇性故障.原因是使用 SMTP 的 Transport.send 有两个无限超时,这可能导致您的进程挂起!

I ran into exactly the same problem and others have reported intermittent failures. The reason is that Transport.send with SMTP has two infinite timeouts which can result in your process just hanging!

来自 SUN 文档:

mail.smtp.connectiontimeout int 以毫秒为单位的套接字连接超时值.默认为无限超时.

mail.smtp.connectiontimeout int Socket connection timeout value in milliseconds. Default is infinite timeout.

mail.smtp.timeout int 以毫秒为单位的套接字 I/O 超时值.默认为无限超时.

mail.smtp.timeout int Socket I/O timeout value in milliseconds. Default is infinite timeout.

为了不永远挂起",您可以明确设置这些:

To not "hang" forever, you can set these explicitly:

来自 SUN:属性始终设置为字符串;Type 列描述了如何解释字符串.例如,使用

From SUN: The properties are always set as strings; the Type column describes how the string is interpreted. For example, use

    props.put("mail.smtp.port", "888");

请注意,如果您使用smtps"协议通过 SSL 访问 SMTP,则所有属性都将命名为mail.smtps.*".

Note that if you're using the "smtps" protocol to access SMTP over SSL, all the properties would be named "mail.smtps.*".

所以,如果你设置了两个超时,你应该得到一个MessagingException",你可以用 try/catch 来处理它,而不是让进程挂起.

So, if you set the two timeouts, you should get a "MessagingException" which you can process with a try/catch rather than having the process just hang.

假设您使用的是 smtp,请添加以下内容,其中 t1 和 t2 是以毫秒为单位的超时时间:

Assuming that you are using smtp, add the following where t1 and t2 are your timeouts in ms:

    props.put("mail.smtp.connectiontimeout", "t1");
    props.put("mail.smtp.timeout", "t2");

当然,这不会解决超时的根本原因,但可以让您优雅地处理问题.

Of course, this will not fix the root-cause for the timeouts, but it will let you handle the issues gracefully.

感谢 Siegfried Goeschl 在 Apache Commons

Thanks to Siegfried Goeschl's post on Apache Commons

PS:我遇到问题的根本原因似乎与我旅行时使用的网络连接有关.显然,该连接导致 SMTP 超时,而我在其他任何连接中都没有.

PS: The root cause of problem I experienced seems tied to the network connection I was using while traveling. Apparently the connection was causing an SMTP timeout which I have not had with any other connections.

这篇关于Transport.send(message) 在下面的代码中不起作用.. netbeans 卡在运行部分.它不会继续……它永远挂在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆