sending java mail error+smtp.gmail.com
hi
i m facing an issue while sending mail.
the mail is actually sending(received in the To email) but an error is thrown
javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: Unsupported record version Unknown-50.49
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1462)
at com.sun.mail.smtp.SMTPTransport.close(SMTPTransport.java:645)
at javax.mail.Transport.send0(Transport.java:171)
at javax.mail.Transport.send(Transport.java:98)
at SmtpUnit.sendMail(SmtpUnit.java:51)
at SmtpUnit.main(SmtpUnit.java:18)
Caused by: javax.net.ssl.SSLException: Unsupported record version Unknown-50.49
at com.sun.net.ssl.internal.ssl.InputRecord.readV3Record(InputRecord.java:375)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:360)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:723)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:680)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:75)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1440)
... 5 more
we are using smtp.gmail.com
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false")
please help me for resolving this issue
Jimmy
Message was edited by:
jimmy@bang
# 1
You should be using SMTP + SSL (smpts) with GMAIL.
You set only these 3 properties (JavaMail 1.4) :
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
See
http://www.rgagnon.com/javadetails/java-0570.html
for an example
--
http://www.rgagnon.com/howto.html
# 2
i tried the same but still not effecting..
it actually sending mail but throwing exception also
the program was working fine till yesterday.. the mail was sending with out any exception.
i doubt there is some change when reading gmail response..
any one can try this code
Thanks
import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SmtpUnit {
public static void main(String args[])
{
try{
sendMail("fromid@ww.com","toid@ww.com","Test Mail","hai,Test mail,,,,");
}catch(Exception ex){ex.printStackTrace();}
}
public static void sendMail(String fromEmail,String toEmail,String subject,String msg)
throws MessagingException{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
//props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "false");
props.put("mail.debug", "false");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourid", "urpasswd");
}
});
//Session session = Session.getInstance(props,null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmail));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(toEmail));
message.setSubject(subject);
message.setText(msg);
// Send message
Transport.send(message);
}
# 3
I have no idea what causes this exception. What version of the JDK
are you using? You probably need to ask the JDK security people
what causes this.
# 4
we are using jdk 1.5..
# 5
I have the same exception as you with your code.
With my code (http://www.rgagnon.com/javadetails/java-0570.html), I have the same exception!
Something has changed with GMail because last week it was exception free!
But ... the good news, I found a property to make the exception go away
props.put("mail.smtp.quitwait", "false");
This property means :
If set to false, the QUIT command is sent and the connection is immediately closed. If set to true (the default), causes the transport to wait for the response to the QUIT command.
ref : http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
So make your properties as :
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.quitwait", "false");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Mine are :
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", SMTP_HOST_NAME);
props.put("mail.smtps.auth", "true");
props.put("mail.smtps.quitwait", "false");
--
http://www.rgagnon.com/howto.html
# 6
thanks a lot..
its working now...
Jimmy
# 7
its working ;)
i`ve the same problems
thanks
props.put("mail.smtp.quitwait", "false");
# 8
Hi all,
I'm new to JavaMail. I've gone through this article and wrote a class as follows
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SSL {
String d_email = "usern", d_password = "password",
d_host = "smtp.gmail.com", d_port = "465",
m_to = "arun.p@greytip.com", m_subject = "Testing",
m_text = "Hey, this is the testing email.";
public SSL() {
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.quitwait", "false");
SecurityManager security = System.getSecurityManager();
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
// session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(m_to));
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}
}
public static void main(String[] args) {
SSL blah = new SSL();
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_email, d_password);
}
}
}
This is giving me an error as follows
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:255)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at SSL.<init>(SSL.java:47)
at SSL.main(SSL.java:54)
I'm using jdk 1.6 and javamail 1.3.2.
Can anyone help me
Thank you
# 9
The JavaMail FAQ can help.
# 10
You should use Javamail 1.4 which supports SSL. I got my sending over gmail completelly working. Since my project open source you can see how to do that.