Good morning everybody,
I'm developping an ERP for my company with the GWT Framework and I would get
the number of unread emails using the Java Mail API.
I can do this but, the problem is I stores the SHA-512 hashed password on the
database and I would not pass the clear password to the Java Mail API, but just the hashed password to avoiding to transmit the clear password on the network.
I use this code to get the number of unread mail:
private static int getNumberOfUnreadMails() {
int numberOfUnreadMails = 0;
Properties properties = new Properties();
properties.put("mail.imap.host", "myserver.com");
properties.put("mail.imap.user", "developper#myserver.com");
properties.put("mail.imap.socketFactory", 143);
properties.put("mail.imap.socketFactory.class", "java.net.ssl.SSLSocketFactory");
properties.put("mail.imap.port", 143);
Session session = Session.getDefaultInstance(properties, new Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("developper#myserver.com", "mypassword");
}
});
Store store;
try {
store = session.getStore("imap");
store.connect();
Folder folder = store.getFolder("Inbox");
numberOfUnreadMails = folder.getUnreadMessageCount();
} catch (Exception e) {
e.printStackTrace();
}
return numberOfUnreadMails;
}
I can also use another hashing algorithm.
If you know a solution for my problem, thaks you in advance.
P.S.: Sorry for my poor English, I’m French.
Your IMAP-server will need the unhashed password to be able to authenticate. You probably are already using SSL (as you set mail.imap.socketFactory.class), so your password is never sent in the clear.
BTW: the correct way to use IMAP with SSL with javamail is to use the imaps protocol (and use the mail.imaps.*, not using the imap protocol and specifying an SSL socket factory as the socket factory. Also usually the IMAP with SSL port is 993, not 143 .
Related
I get this error when I try to send a mail using JavaMail API. I am sure that the username and password are 100% correct. The Gmail account which I'm connecting is an older account, because they say it takes time for it to work with new accounts.
DEBUG SMTP RCVD: 535-5.7.1 Username and Password not accepted. Learn more at
535 5.7.1 http://mail.google.com/support/bin/answer.py?answer=14257 x35sm3011668
wfh.6
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.AuthenticationFailedException
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at Main.(Main.java:41)
at Main.main(Main.java:51)
and this is my code:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class Main
{
String d_email = "abc#gmail.com",
d_password = "pass",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "abc#gmail.com",
m_subject = "Testing",
m_text = "testing email.";
public Main()
{
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.fallback", "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)
{
Main blah = new Main();
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}
I had same issue :
I refer this link, I have followed below steps it worked for me.
By default Gmail account is highly secured. When we use gmail smtp from non gmail tool, email is blocked. To test in our local environment, make your gmail account less secure as
Login to Gmail.
Access the URL as https://www.google.com/settings/security/lesssecureapps
Select "Turn on"
The given code snippet works fine on my Gmail account, so this problem lies somewhere else. Did you follow the link given in the error message? It contains the following hints:
Make sure that you've entered your full email address (e.g. username#gmail.com)
Re-enter your password to ensure that it's correct. Keep in mind that passwords are case-sensitive.
Make sure your mail client isn't set to check for new mail too often. If your mail client checks for new messages more than once every 10 minutes, your client might repeatedly request your username and password.
Especially the last point is important. Google is very strict in this. If you're trying to connect Gmail for example more than 10 times in a minute programmatically, then you may already get blocked. Have a bit of patience, after some time it will get unblocked.
If you'd like more freedom in sending mails, I recommend to look for a dedicated mail host or to setup your own mail server, such as Apache James or Microsoft Exchange. I've already answered this in detail in one of your previous questions.
I encountered the exact same problem, for me the reason is I have turned on 2-step verification on my gmail account.
After generating a new application-specific password and use that in my java application, this "535 5.7.1" issue is gone.
You can generate a new application-specific password following this official google guide.
I faced the same problem although my username and password were correct, but after some research, I was able to send email through applications by enabling the Less secure app access option on my Gmail account. You can find this feature under security option in the left menu.
Related links:
I can't sign in to my email client
Enable access to less secure applications
Now, Google not supported Less Secure Apps so we are getting this issue.
You follow bellow steps to resolve your Authentication issue during send mail using SMTP.
1 - Enable 2-step Verification in your mail
2 - Once you enabled above, you will able to see App Passwords option in same Security tab
3 - Now generate App Password for Other(Custom)
4 - Use this generated password in place of your gmail password in your code.
Enjoy :)
I have the same error message and this is how I have solved the issue,
Create an app password: here is how we can generate an app password,
1. Visit your App passwords page. You may be asked to sign in to your Google Account.
2. At the bottom, click Select app and choose the app you’re using.
Click Select device and choose the device you’re using.
3. Select Generate.
4. Follow the instructions to enter the App password (the 16 character code in the yellow bar) on your device.
5. Select Done.
I worked for a Spring boot app and I get the app password say, sadsadaffferere for the email address, xyz#gmail.com. So, I need to configure the app properties like the following,
# the email settings
# ------------------
spring.mail.host=smtp.gmail.com
spring.mail.username=xyz#gmail.com
spring.mail.password=sadsadaffferere
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.socketFactory.port=465
spring.mail.properties.mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory
spring.mail.properties.mail.smtp.socketFactory.fallback=false
support.email=xyz#gmail.com
Everything works fine afterwards
You need to turn on the 'less secure apps allowed' in gmail settings.
If the same credential was working earlier and it stopped working then the primary reason for this problem is password mismatch/changed on gmail client and not updated in Jenkins or other CI server. If that is not the case then check for reasons mentioned by #BalusC
i turned off antivirus and enabled less secure app in gmail, but now google has disabled this option, for that to work you need to do two steps verification and genetare 16 digit password and paste in application.properties replace this 16 digit password.
I've been working on a java application to use my Gmail connection to send an email but am hitting a wall.
I've found out that I need to use OAuth which the current implementation of JavaMail uses, but I haven't really found a clear cut guide to how to do this. I'm an amateur programmer and well, kind of a dummy when it comes to comprehension (I'm working really hard for some C's in school, lol) but I'm trying to learn here.
Most of what I've put together has been culled from other things I've found, trying to get something working, which it kind of is. But the problem that I'm hitting is one of two things.
Either I can't get authorized, then I get an email from Google saying an app was trying to access my email without complying with modern security standards, or that I need to log in via a web browser.
From what I've found, a few years back Google changed their security which is why we need to use OAuth now, which, luckily JavaMail supports, but I can't get it working. And a lot of the help I've seen here on Stack Overflow or other places is nearly 5 years old.
Right now, I'm trying to make a java program in eclipse to just send an email, and I'm using github.com/google/gmail-oauth2-tools
To try and make the OAuth token, but there's an error
Error Line
The method in OAuth2SaslClientFactory
public SaslClient createSaslClient(String[] mechanisms,
String authorizationId,
String protocol,
String serverName,
Map<String, ?> props,
CallbackHandler callbackHandler) {
boolean matchedMechanism = false;
for (int i = 0; i < mechanisms.length; ++i) {
if ("XOAUTH2".equalsIgnoreCase(mechanisms[i])) {
matchedMechanism = true;
break;
}
}
if (!matchedMechanism) {
logger.info("Failed to match any mechanisms");
return null;
}
return new OAuth2SaslClient((String)props.get(OAUTH_TOKEN_PROP), callbackHandler);
}
The code from OAuth2SASLClient
public OAuth2SaslClient(String oauthToken, CallbackHandler callbackHandler) {
this.oauthToken = oauthToken;
this.callbackHandler = callbackHandler; }
That return line is saying the constructor can't handle that, but the constructor in OAuth2SaSLClient.java is actually set up to have a (String, Callback) parameter.
If somebody has something quick and easy that works, I'd love to see it,even though it appears that this needs to use a specific way of creating an OAuth token.
I can post my code if it helps, but I need to clean it up first, it's sort of a first draft / messy collage of things I've tried to get working.
Or a javamail / Oauth tutorial for dummies.
The end goal here is to tie this to a program that can send out a quick notification when something happens. Which I can't seem to do.
Mixing OAuth and javamail won't work, you need to choose your API: either the Gmail v4 API (with OAuth) or the javamail API. You said you are just trying to send emails from your own account, so I'd suggest you use the javamail IMAP API. When going through the javamail API you don't need Oauth at all.
For example:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props, new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(GOOGLE_USERNAME, password);
}
});
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(FROM_EMAIL, FROM_NAME));
// rest of the email settings
If you needed to send emails on behalf of other people, or need to do more advanced mail operations such as create a Gmail draft, then the Gmail API would be the proper approach.
Note: if you are going to use the Gmail API, be sure to use the newer Gmail API: https://developers.google.com/gmail/api/quickstart/java
The old Google Data API is deprecated
Hi I want to write a java program where I will provide my email id and password. and I want to read all new unread messages that arrived to that email id. I donot know how to write program for that.
The below program works fine for gmail. but it does not work for yahoomail because for yahoo pop3 is not configured. I want a generic code which will work for all email id.
import java.io.*;
import java.util.*;
import javax.mail.*;
public class ReadMail {
public static void main(String args[]) throws Exception {
// String host = "pop.gmail.com";
// String user = "xyz";
// String password = "12345";
// Get system properties
Properties properties = System.getProperties();
// Get the default Session object.
Session session = Session.getDefaultInstance(properties, null);
// Get a Store object that implements the specified protocol.
Store store = session.getStore("pop3s");
//Connect to the current host using the specified username and password.
store.connect(host, user, password);
//Create a Folder object corresponding to the given name.
Folder folder = store.getFolder("inbox");
// Open the Folder.
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
// Display message.
for (int i = 0; i < message.length; i++) {
System.out.println("------------ Message " + (i + 1) + " ------------");
System.out.println("SentDate : " + message[i].getSentDate());
System.out.println("From : " + message[i].getFrom()[0]);
System.out.println("Subject : " + message[i].getSubject());
System.out.print("Message : ");
InputStream stream = message[i].getInputStream();
while (stream.available() != 0) {
System.out.print((char) stream.read());
}
System.out.println();
}
folder.close(true);
store.close();
}
}
You need to know more than just login-pass. Things like mail server address, mail server type, port for connections, etc.
You should probably check out Java Mail API, or Commons Email.
UPD:
You create a Session using Session.getDefaultInstance() method (which takes connection Properties object and authenticator), get a Store from this Session using Session.getStore() method, get a Folder from that store using Store.getFolder("FOLDER_NAME") method, open that Folder, using Folder.open(Folder.READ) method, and get all messages, using something like Message[] messages = inboxFolder.getMessages();
Is that what you were looking for?
UPD2:
There is simply no way to write a generic program, which will work with any mail provider, using just server path, userID and password. Because different mail servers are configured differently. They talk differen protocols (imap/pop3/pop3 ssl) on different ports. There's always some guy, who has configured his mail server to talk imap over ssl on 31337 port only, all the other ports and protocols are banned. And this guy breaks your program. So, you'll have to specify all this properties in your properties object. Look here for properties, you'll have to specify.
UPD3:
On second thought, you actually have one option. Just try connecting to the server using different protocols. If that does not help, start iterating through ports. The one that fits is your configuration. If that's really what you want.
You need the javax.mail package, and the documentation of it. Read the documentation. Then you know.
There are two ways to do it:
1) Google provides API's to access mail you could use that library which provides more control over your mails. See here: http://code.google.com/apis/gmail/. In the same way try for other email providers.
2) Simple mail client(you could find it easily googling), but you need to look at headers to identify which mails are read/unread etc.
You need a registry where you can get the properties for a given mail service.
For instance, instead of specifying a pop3 host, you could specify the name of a .properties file that would contain the host, the port, the protocol, etc...
If your .properties file contains the protocol, for instance mail.store.protocol=pop3, you could use session.getStore() (with no argument), and the same code could be used for pop3, imap, pop3s, imaps.
I'm using the javax.mail system, and having problems with "Invalid Address" exceptions. Here's the basics of the code:
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", m_sending_host);
// Get session
Session session = Session.getDefaultInstance(props, new Authenticator(){
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(m_sending_user, m_sending_pass);
}
});
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(m_sending_from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(vcea.get(i).emailaddr));
message.setSubject( replaceEnvVars(subject) );
message.setText(replaceEnvVars(body));
// Send message
try {
Transport.send(message);
} catch (Exception e){
Log.Error("Error sending e-mail to addr (%s): %s",
vcea.get(i).emailaddr, e.getLocalizedMessage() );
}
The issue is that the above code does work, sometimes. But for some e-mail addresses that I know to be valid (because I can send to them via a standard e-mail client), the above code will throw an "Invalid Address" exception when trying to send.
Any clues or hints would be greatly appreciated.
--Update: problem with authentication.
Ok, here's what I've discovered was going on. When receiving e-mail, the code above correctly sets up authentication and the Authenticator.getPasswordAuthentication() callback is actually invoked.
Not so when sending e-mail. You have to do a bit more. Add this:
// Setup mail server
props.put("mail.smtp.host", m_sending_host);
props.put("mail.smtp.auth", "true");
which will force the javax.mail API to do the login authentication. And then use an actual Transport instance instead of the static .send() method:
Transport t = session.getTransport(m_sending_protocol);
t.connect(m_sending_user, m_sending_pass);
...
// Send message
try {
t.sendMessage(message, message.getAllRecipients());
} catch (Exception e){
Without forcing the authentication, the mail server saw me as an unauthorized relay, and just shut me down. The difference between the addresses that "worked" and the addresses that didn't was that the ones that "worked" were all local to the mail server. Therefore, it simply accepted them. But for any non-local "relay" addresses, it would reject the message because my authentication information hadn't been presented by the javax.mail API when I thought it would have.
Thanks for the clues to prompt me to look at the mail server side of things as well.
--Update: problem with authentication.
Ok, here's what I've discovered was going on. When receiving e-mail, the code above correctly sets up authentication and the Authenticator.getPasswordAuthentication() callback is actually invoked.
Not so when sending e-mail. You have to do a bit more. Add this:
// Setup mail server
props.put("mail.smtp.host", m_sending_host);
props.put("mail.smtp.auth", "true");
which will force the javax.mail API to do the login authentication. And then use an actual Transport instance instead of the static .send() method:
Transport t = session.getTransport(m_sending_protocol);
t.connect(m_sending_user, m_sending_pass);
...
// Send message
try {
t.sendMessage(message, message.getAllRecipients());
} catch (Exception e){
Without forcing the authentication, the mail server saw me as an unauthorized relay, and just shut me down. The difference between the addresses that "worked" and the addresses that didn't was that the ones that "worked" were all local to the mail server. Therefore, it simply accepted them. But for any non-local "relay" addresses, it would reject the message because my authentication information hadn't been presented by the javax.mail API when I thought it would have.
Thanks for the clues to prompt me to look at the mail server side of things as well.
I would change the call to InternetAddress to use the "strict" interpretation and see if you get further errors on the addresses you are having trouble with.
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(vcea.get(i).emailaddr, true ));
// ^^^^ turns on strict interpretation
Javadoc for InternetAddress constructor
If this fails, it will throw an AddressException which has a method called getPos() which returns the position of the failure (Javadoc)
A good hint for those using the ssl encryption in the smtp configuration, you should enable it by specifying the property mail.smtp.ssl.enable, as shown below:
props.put("mail.smtp.ssl.enable", "true");
Otherwise it can lead to similar problems as described above.
Try this:
String to="stackoverflow#so.com";
String cc="one#mail.com,two#mail.com"; //The separator ',' works good
message.setRecipients(Message.RecipientType.TO,new InternetAddress[] {
new InternetAddress(to) }); // This is only one mail
InternetAddress[] addr = parseAddressList(cc); //Here add all the rest of the mails
message.setRecipients(Message.RecipientType.CC,addr);
Sorry for my english. It's not good.
This seems to me like a problem that happened at my work.
If the code you are showing is being concurrent, then using directly System.getProperties
could be a problem, because the host value you put on them can be overwritten by next request, while still keeping the user and password from the overwritten host.
In our case, we solved that using a clone of the System.getProperties() hashtable.
Hope that helps (this problem was really hard to track).
I want to access messages in Gmail from a Java application using JavaMail and IMAP. Why am I getting a SocketTimeoutException ?
Here is my code:
Properties props = System.getProperties();
props.setProperty("mail.imap.host", "imap.gmail.com");
props.setProperty("mail.imap.port", "993");
props.setProperty("mail.imap.connectiontimeout", "5000");
props.setProperty("mail.imap.timeout", "5000");
try {
Session session = Session.getDefaultInstance(props, new MyAuthenticator());
URLName urlName = new URLName("imap://MYUSERNAME#gmail.com:MYPASSWORD#imap.gmail.com");
Store store = session.getStore(urlName);
if (!store.isConnected()) {
store.connect();
}
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
I have set the timeout values so that it wouldn't take "forever" to timeout. Also, MyAuthenticator also has the username and password, which seems redundant with the URL. Is there another way to specify the protocol? (I didn't see it in the JavaDoc for IMAP.)
Using imaps was a great suggestion. Neither of the answers provided just worked for me, so I googled some more and found something that worked. Here's how my code looks now.
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", "<username>#gmail.com", "<password>");
...
} catch (NoSuchProviderException e) {
e.printStackTrace();
System.exit(1);
} catch (MessagingException e) {
e.printStackTrace();
System.exit(2);
}
This is nice because it takes the redundant Authenticator out of the picture. I'm glad this worked because the SSLNOTES.txt make my head spin.
You need to use the following properties for imaps:
props.setProperty("mail.imaps.host", "imap.gmail.com");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.imaps.connectiontimeout", "5000");
props.setProperty("mail.imaps.timeout", "5000");
Notices it's "imaps", not "imap", since the protocol you're using is imaps (IMAP + SSL)
In JavaMail, you can use imaps as the URL scheme to use IMAP over SSL. (See SSLNOTES.txt in your JavaMail distribution for more details.) For example, imaps://username%40gmail.com#imap.gmail.com/INBOX.
Similarly, use smtps to send emails via Gmail. e.g., smtps://username%40gmail.com#smtp.gmail.com/. Again, read SSLNOTES.txt for more details. Hope it helps!
You have to connect to GMail using SSL only. Setting the following properties will force that for you.
props.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.imap.socketFactory.fallback", "false");
Here is what worked for my team and I, given a classic account nickname#gmail.com and a business account employee#business.com :
final Properties properties = new Properties();
properties.put("mail.imap.ssl.enable", "true");
imapSession = Session.getInstance(properties, null);
imapSession.setDebug(false);
imapStore = imapSession.getStore("imap");
imapStore.connect("imap.gmail.com", USERNAME, "password");
with USERNAME = "nickname" in the classic case, and USERNAME = "employee#business.com" in the business account case.
In the classic case, don't forget to lower the account security here : https://www.google.com/settings/security/lesssecureapps
In both cases check in GMail Settings => Forwarding POP / IMAP if IMAP is enabled for the account.
Hope it helps!
To go further :
http://www.oracle.com/technetwork/java/javamail/faq/index.html#gmail
https://support.google.com/mail/accounts/answer/78754
If you'd like more sample code on using JavaMail with Gmail (e.g. converting Gmail labels to IMAP folder names, or using IMAP IDLE), do check out my program GmailAssistant on SourceForge.
Check http://g4j.sourceforge.net/. There is a minimal gmail client built using this API.
I used following properties to get the store and It works well.
"mail.imaps.host" : "imap.gmail.com"
"mail.store.protocol" : "imaps"
"mail.imaps.port" : "993"
You need to have JSSE installed to use SSL with Java
URLName server = new URLName("imaps://<gmail-user-name>:<gmail-pass>#imap.gmail.com/INBOX");