Problem with java mail using smtp protocol.
843834Aug 11 2008 — edited Aug 13 2008Hi,
I used the below program:
{noformat}<code>import java.security.Security;
import java.util.Properties;
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 SimpleMail
{
private String mailhost = "smtp.gmail.com";
public synchronized void sendMail(String subject, String body, String sender, String recipients)
throws Exception
{
Security.addProvider(*new* com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.auth", "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");
props.setProperty("mail.smtp.quitwait", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{ *return* *new* PasswordAuthentication("username","password"); }
});
MimeMessage message = new MimeMessage(session);
message.setSender(*new* InternetAddress(sender));
message.setSubject(subject);
message.setContent(body, "text/plain");
if (recipients.indexOf(',') > 0)
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));
else
message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
Transport.send(message);
}
public static void main(String args[]) throws Exception
{
MailUtils mailutils = new MailUtils();
mailutils.sendMail("test", "test", "from@gmail.com", "To@gmail.com");
}
}
The above program works fine when I give the actual user credentials and the recipient's email address.
However, when I try to replace the server host value and the port to my actual company mail server and also by using an
actual account of my company mail server I am getting the erros as follows:
Exception in thread "main" javax.mail.MessagingException: Exception reading resp
onse;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connecti
on?
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java
:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:41
2)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at SimpleMail.sendMail(SimpleMail.java:49)
at SimpleMail.main(SimpleMail.java:57)
Caused by: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext conne
ction?
at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(Unknown
Source)
at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Un
known Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(Unknown Sou
rce)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(Unknown Source)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java
:1589)
... 9 more
If I comment the line containing the value "props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");"
from the above code I am able to send the mail to the recipients but the sender's name in the recipient mail box looks as "(unknown sender)".
Any help on the above issue will be greatly appreciated.
</code>{noformat}