Reading Mails from Outlook Javamail - java

Am trying to read the emails from the outlook using javamail
herez the code snippet.
try {
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imap");
props.setProperty("mail.imaps.starttls.enable", "true");
props.setProperty("mail.imaps.host", "outlook.office365.com");
props.setProperty("mail.imaps.port", "143");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imaps");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
} catch (Exception ex){
ex.printStackTrace();
}
Exception
javax.mail.MessagingException: Unrecognized SSL message, plaintext connection?;
nested exception is:
javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:727)
Any pointers are appreciated.

Your code is very confused. Keep it simple:
Properties props = new Properties();
props.setProperty("mail.imap.ssl.enable", "true");
Session mailSession = Session.getInstance(props);
mailSession.setDebug(true);
Store mailStore = mailSession.getStore("imap");
mailStore.connect("outlook.office365.com", "<username>", "<password>");
Use the "imap" protocol, but tell JavaMail to enable SSL. Don't worry about ports, JavaMail knows what to do. More detail in the JavaMail FAQ.

Port 143 (what you're using) is for plain-text IMAP (hence the error message plaintext connection?). IMAPS uses port 993, so try that.

Related

Send email get stuck at transport.connect

This is the code:
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "mail.mydomain.com");
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.user", "myemail#mydomain.com");
properties.put("mail.smtp.password", "mypassword");
Session sendSession = Session.getInstance(properties);
MimeMessage message = new MimeMessage(sendSession);
message.setFrom(new InternetAddress("myemail#mydomain.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(content);
Transport transport = sendSession.getTransport("smtp");
transport.connect("mail.mydomain.com", 465, "mydomain.com#mydomain.com", "mypassword");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
These are the SSL/TLS setting:
Outgoing Server: mail.mydomain.com
SMTP Port: 465
Executing the above code, it get stuck at:
transport.connect("mail.mydomain.com", 465, "mydomain.com#mydomain.com", "mypassword");
and nothing happen.
Why it get stuck at connect and how to fix it?
Try changing the mail.transport.protocol value from smtps to smtp.
You are trying to use port 465 which is for smtp . So you have to change the protocol to smtp . You are trying to start TLs with secure mode but you are using simple smtp port.
Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtps");
properties.put("mail.smtp.starttls.enable", "true");
.....
Try changing it and send mail.
I found the problem.
At Transport transport = sendSession.getTransport("smtp");
Instead of smtp should be smtps
Thanks to everybody who tried to help me.

Java Mail Change port in Transport without Authentication username and password

Instead of port 25 I want to be able to set the port to be whatever I want, along with the host. But I do not want to set the username or password because credentials will vary. My code base was working fine just using the static method Transport.send() to perform the task of sending out emails.
Transport has a connect method with no arguments or a method host/port/auth credentials.
There does not seem to be a way to set just the port and host without using the other two.
Transport seems to default to 25 no matter what I try. I am using a tool called smtp4dev to listen on ports that I am testing.
The important bits of the code are below:
Properties props = new java.util.Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "localhost");//Just using localhost for testing
props.put("mail.smtp.port", 3500);//Whatever port
props.setProperty("mail.smtp.auth", "false");
session = Session.getInstance(props);
msg = new MimeMessage(session);
Transport transport = session.getTransport("smtp");
transport.connect(); //transport.connect("localhost", 3500, "","");
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
transport.close();
I can inspect session and see that it has the port property set in it, but when I inspect transport I only see the defaultPort as being set. I tried using a blank username, and password to no avail. Any ideas? Am I doing something dumb?
This is what fixed it, just use SMTPTransport instead and it works. I also used port as a string instead of an int.(I then had to unblock the port)
import com.sun.mail.smtp.SMTPTransport;
Properties props = new java.util.Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.host", "localhost");//Just using localhost for testing
props.setProperty(SMTP_PORT_PROPERTY, String.valueOf(3500));//Whatever port
props.setProperty("mail.smtp.auth", "false");
session = Session.getInstance(props);
msg = new MimeMessage(session);
SMTPTransport transport = (SMTPTransport) session.getTransport("smtp");
transport.connect(); //transport.connect("localhost", 3500, "","");
transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
transport.close();
The data type of the property value "mail.smtp.port" must be string.
Try changing this:
props.put ("mail.smtp.port", 3500);
to:
props.put ("mail.smtp.port", "3500");

JavaMail cannot send email

i am trying to write an application in java that will send emails, i found a tutorial on youtube and tried to follow it. however it does not work for me still, here is the error i get
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25, response: 421
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1949)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at libraryFineList.ParseRecords.main(ParseRecords.java:90)
i have no idea, what's wrong, anything i found on google did not help,
here is the code
public static void main(String[] args) throws IOException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", 465);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator(){
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("user#gmail.com", "pass");
}
}
);
try{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user#gmail.com"));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress("ycharnetskaya#gmail.com", "Mr. User"));
message.setSubject("Your Example.com account has been activated");
message.setText("Worked");
Transport.send(message);
}catch(Exception e){
e.printStackTrace();
}
The tutorial you followed is full of errors. Start by fixing these common mistakes. Then follow these instructions for connecting to Gmail. If you're still having problems, you'll find lots more help in the JavaMail FAQ. There's also many sample programs available.
It's strange that your error message is complaining about a problem connecting to localhost:25 when your properties file is clearly suggesting that you should use smtp.gmail.com.
I don't suppose there's anything dodgy going on with your hosts file that's redirecting smtp.gmail.com back to 127.0.0.1 or something, is there? What happens if you ping smtp.gmail.com from the command line?
Apart from that, I'd suggest checking you're using the latest version of Java Mail.

How to send email in java GWT

I am using this code in my java GWT application
public String greetServer(String input) throws Exception {
try{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.smtp.port", "25");
props.setProperty("mail.host", "smtp.random.com");
props.setProperty("mail.user", "foo#bar.com");
props.setProperty("mail.password", "000000000");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("hello");
message.setContent("helloo sss", "text/plain");
message.addRecipient(Message.RecipientType.TO, new InternetAddress("junaidp#gmail.com"));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
} catch(NoSuchProviderException e){
throw new Exception(e);
}
return input;
}
Error: javax.mail.MessagingException: Could not connect to SMTP host: smtp.random.com, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect
if i use
props.setProperty("mail.host", "smtp.live.com");
and use my hotmail account , it gives this error
javax.mail.MessagingException: can't determine local email address
Any idea what could be the solution
thanks
I just used Simple Java Mail in a GWT project. You might want to try it. It's very simple to configure.
There are lots of example there, including one of sending using gmail's SMTP server using for example TLS:
Email email = new Email.Builder()
.from("Michel Baker", "m.baker#mbakery.com")
.to("mom", "jean.baker#hotmail.com")
.to("dad", "StevenOakly1963#hotmail.com")
.subject("My Bakery is finally open!")
.text("Mom, Dad. We did the opening ceremony of our bakery!!!")
.build();
new Mailer("smtp.gmail.com", 25, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 587, "your user", "your password", TransportStrategy.SMTP_TLS).sendMail(email);
new Mailer("smtp.gmail.com", 465, "your user", "your password", TransportStrategy.SMTP_SSL).sendMail(email);
If you have two-factor login turned on, you need to generate an application specific password from your Google account.
Here's some Gmail settings which work for me:
//these are fed into the Properties object below:
mail.smtp.starttls.enable = true
mail.transport.protocol = smtp
mail.smtp.auth = true
and some Java:
Properties properties = ...
javax.mail.Session session = javax.mail.Session.getInstance(properties, null);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.gmail.com", username, password);
Error: javax.mail.MessagingException: Could not connect
to SMTP host: smtp.random.com port: 25;
nested exception is: java.net.ConnectException: Connection refused: connect
This error means that your SMTP server you provided is invalid. The code you have is correct, but smtp.random.com must not be a valid SMTP server.
I suggest you look into using Google's SMTP server that is free provided you use a valid gmail account.
Refer to this page for more information on using Gmail's STMP server: http://email.about.com/od/accessinggmail/f/Gmail_SMTP_Settings.htm
This tutorial worked for me in the past

Return path doesn't get set using mail.smtp.from

I am having problems setting the return path for all yahoo emails I'm sending. Here's what I'm using:
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.auth", "true");
props.put("mail.host", host);
props.put("mail.port", port);
props.put("mail.user", username);
props.put("mail.password", password);
props.put("mail.smtp.from", bounceAddress);
I'm sending the mimeMessage using transport.sendMessage method. Still, after sending a few, in the Full-header for yahoo mails the return path is the same as the sender email.
Any idea what might be the problem?
Thanks
You're using the "smtps" protocol, but setting the property for the "smtp" protocol. Set "mail.smtps.from" instead.

Categories