Transport.send(消息)在以下代码中不起作用.. 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

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

问题描述

我曾尝试编写使用Java发送电子邮件的代码。但是这段代码不起作用。当代码执行时,它会卡在transport.send(消息)上。它永远停留在那里。此外,我不确定其余代码是否正确。

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协议访问SMTP over SSL,则所有属性将被命名为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 /处理抓住而不是让流程挂起。

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(消息)在以下代码中不起作用.. netbeans卡在运行部分。它不会继续下去......它永远挂在那里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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