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();
}
}
}
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.
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.
Replace Session.getDefaultInstance with Session.getInstance.
If that doesn't solve the problem, read the JavaMail FAQ, which has debugging tips.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With