This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Other JSE/JEE APIs and the fly likes Unable to send a mail through Outlook. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "Unable to send a mail through Outlook." Watch "Unable to send a mail through Outlook." New topic
Author

Unable to send a mail through Outlook.

Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Hi,
I am using the following code to connect to outlook and send a mail...but it gives me a "exception reading response..." error and does not send mail through Outlook.


package Mail;

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 = "abc.com";
//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","25");
props.put("mail.smtp.socketFactory.port","25");
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("userid","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
{
SimpleMail mailutils = new SimpleMail();
mailutils.sendMail("test", "test", "p.kk@sprint.com", "p.kk@sprint.com");

}

}

and i get the following error:

Exception in thread "main" javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
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:412)
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 Mail.SimpleMail.sendMail(SimpleMail.java:53)
at Mail.SimpleMail.main(SimpleMail.java:62)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:830)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1555)
at com.sun.net.ssl.internal.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:103)
at com.sun.net.ssl.internal.ssl.Handshaker.kickstart(Handshaker.java:626)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1272)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1169)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:785)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)
... 9 more

It works perfectly fine when I use the gmail smtp....but it errors out with the above exception when I try to use the Outlook SMTP. Is outlook restricting my program from using it to send mails? or am I using the wrong smtp host....??
Please let me know if I missed something...
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Outlook is an email client; it is not accessible via SMTP. What, precisely, are you trying to do?
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Tim,
I am trying to read some email ids from an excel and shoot a mail to those ids using my outlook account.

Am I using a wrong approach? My java program currently reads values from an excel sheet and I thought the program below will help me take each of the ids and send them a mail. But I want the mail to be sent from my outlook. Hence got stuck...
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
It doesn't matter where the data originates from, Excel or otherwise, since the problem is apparently with the email part.

But again:
Outlook is an email client; it is not accessible via SMTP.

... and you won't be able to use it to send mails using JavaMail. Maybe you're just confused about terminology, and are actually trying to use some email server?
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8

Yes, maybe you are right.
My main concern here is to use my outlook account to send mails when I give it a list of addresses[email ids].
But can we not use webmail or find out the configurations of Microsoft Exchange Server and provide it in the program?? if not accessible via smtp, then which other protocol can I use? Also, where can I find the port and related details for MS exchange server?
I have seen threads that use MS Exchange server and some even say they are using Outlook, so is it not what I'm trying to accomplish as well??
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Hi,

You can get the server details from the Outlook itself. I guess its there in the Tools----> Accounts option, although not sure.
Once you get the server details, i think you can use it along with your credentials to send the email.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Thanks Sumit.
I tried that as well. I picked up the server details following Tools -> Accounts. But I feel the connection is happening fine. But not too sure about the exception I recieve.
Looks like either the port used is different from 25. Or Outlook is blocking external apps(in this case, my java program) from using Outlook.

Can you make out what the above error is??
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
That makes it clearer. You want to send mails via the Exchange server using the account credentials that are also used by your Outlook client. In that case, SMTP is indeed the protocol to be used. All details (including the correct port number) would have had to be set up in Outlook, so you can look them up there. Are you using the correct value for the "mailhost" property?

Pooja psharma wrote:Or Outlook is blocking external apps(in this case, my java program) from using Outlook.

You are not using Outlook at all; it has nothing whatsoever to do with this.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Could be a firewall problem, just a thought.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
Unlikely; that would block Outlook's connections just like JavaMail's connections.
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Thanks Tim!
I am also thinking on the same lines of 'mailhost' property not being right.
So where can I look it up? I tried digging Tools->Email accounts-> clicking on MS exchange link...and all I get is an address which when I use, I get an unknown SMTP host Excpetion.

Also, any idea what this error/exception indicates?

Exception in thread "main" javax.mail.MessagingException: Exception reading response;
nested exception is:
java.net.SocketException: Connection reset
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:412)
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 Mail.SimpleMail.sendMail(SimpleMail.java:53)
at Mail.SimpleMail.main(SimpleMail.java:62)
Caused by: java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:830)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1170)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:785)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)...
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Its look like that your server ip address is not reachable to your application host.

Can you check if you can ping the server from your application machine?
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

To get the server details go to

Tools-->Email Accounts-->View or change existing email accounts

Link
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Sumit,
I tried pinging the IP from my local command prompt..and the request timed out
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2408
That is from the same machine from where you successfully use Outlook to connect to the mail server? That sounds odd.
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Are you running the code in your local machine from where you pinged the server?
Pooja psharma
Greenhorn

Joined: Jan 29, 2008
Posts: 8
Yes, that's right...same machine
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Pooja psharma wrote:Yes, that's right...same machine


not sure...
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

But the outlook is working fine rite? Can you get in touch with the network team in your office?
 
 
subject: Unable to send a mail through Outlook.