Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
        }       
    }
}
like image 729
user1155707 Avatar asked Jan 18 '12 08:01

user1155707


4 Answers

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.

like image 72
Deane Kane Avatar answered Nov 17 '22 10:11

Deane Kane


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.

like image 25
DrDave Avatar answered Nov 17 '22 09:11

DrDave


Replace Session.getDefaultInstance with Session.getInstance.

If that doesn't solve the problem, read the JavaMail FAQ, which has debugging tips.

like image 2
Bill Shannon Avatar answered Nov 17 '22 10:11

Bill Shannon


I had the exact same issue. I took me multiple hours to find the source of the problem. I was using the wrong dependency / dependency combination...

     <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.3</version>
    </dependency>

Changing it to ...

     <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
        <version>1.5.3</version>
    </dependency>

... solved the issue.

like image 2
user9960800 Avatar answered Nov 17 '22 11:11

user9960800