Transport.send(message) not working in the below c

2020-06-16 02:38发布

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();
        }       
    }
}

8条回答
SAY GOODBYE
2楼-- · 2020-06-16 03:31

I had AVG Free installed and it was blocking the email being sent.

Open AVG, go to Menu > Settings > Basic Protection > Email Shield

Uncheck "Scan Outbound Emails (SMTP)" and try run your program again.

查看更多
够拽才男人
3楼-- · 2020-06-16 03:32

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!

From SUN documentation:

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

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

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

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");

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

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.

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.

Thanks to Siegfried Goeschl's post on Apache Commons

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.

查看更多
登录 后发表回答