Java mail iCloud mail connect to IMAP server - jakarta-mail

I am trying to connect with iCloud mail using Java app. I want to read mails from iCloud IMAP mail server using user credentials. But it does not works. Below code snippet worked for Gmail, Yahoo, Outlook, but not working for iCloud:
public void connectToIMAP(final User user, MailSettings settings) {
ICloudSettings iCloud = (ICloudSettings) settings;
properties = System.getProperties();
properties.setProperty("mail.store.protocol", iCloud.imap.protocol);
properties.setProperty("mail.imap.starttls.enable", "true");
properties.setProperty("mail.imap.debug", "true");
try {
session = Session.getInstance(properties);
session.setDebug(true);
store = session.getStore();
store.connect(iCloud.imap.host, iCloud.imap.port, user.getEmail(), user.getPassword());
folder = store.getFolder(MailSettings.INBOX);
folder.open(Folder.READ_ONLY);
} catch (Exception e) {
e.printStackTrace();
}
}
public ICloudSettings() {
imap.host = "imap.mail.me.com";
imap.port = 993;
imap.protocol = "imaps";
}
Debug info is:
DEBUG: setDebug: JavaMail version 1.5.4
DEBUG: getProvider() returning
javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle]
DEBUG IMAPS: mail.imap.fetchsize: 16384
DEBUG IMAPS: mail.imap.ignorebodystructuresize: false
DEBUG IMAPS: mail.imap.statuscachetimeout: 1000
DEBUG IMAPS: mail.imap.appendbuffersize: -1
DEBUG IMAPS: mail.imap.minidletime: 10
DEBUG IMAPS: closeFoldersOnStoreFailure
DEBUG IMAPS: trying to connect to host "imap.mail.me.com", port 993, isSSL true
* OK [CAPABILITY mr11p00im-iscream006 15E43 XAPPLEPUSHSERVICE IMAP4 IMAP4rev1 SASL-IR AUTH=ATOKEN AUTH=PLAIN] iSCREAM ready to rumble (15E43-20056:9352) mr11p00im-iscream006 [24:210:09:33:11:22]
DEBUG IMAPS: AUTH: ATOKEN
DEBUG IMAPS: AUTH: PLAIN
DEBUG IMAPS: protocolConnect login, host=imap.mail.me.com, user=yogesh.h.patil#icloud.com, password=
DEBUG IMAPS: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAPS: AUTHENTICATE PLAIN command result: A0 NO [AUTHENTICATIONFAILED] Authentication failed
javax.mail.AuthenticationFailedException: [AUTHENTICATIONFAILED] Authentication failed
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:723)
at javax.mail.Service.connect(Service.java:364)

Hi I'm also connecting to iCloud via IMAP, but not with java mail. I'm using piece of code from K9 mail client, available at github. I find out that accessing IMAP does not work when you have 2 factor authorization turned on for your icloud account. Second scenario when I get similar message to you (Authentication failed) is when I use email address different then #icloud.com one. For example I set up my apple id with my gmail account, and created #icloud.com alias. I cannot access IMAP with gmail address as username, but there is no problem when I use #icloud.com alias.

If you have enabled 2 Factor authentication, you need to have app-specific password created. Use the app-specific password instead of standard Apple ID password.
Also, if your Apple ID ends with #me.com (say foo#me.com), then IMAP username should be just the username part of the email address (for email => foo#me.com, IMAP username => foo)

Related

Nodemailer with gmail service account

I'm implementing a "no-reply" account for sending automated emails. Do I need the access key/refresh token, and if so where can I generate them for a service account?
At the moment I have this:
noreply = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: smtpConfig.client_email,
serviceClient: smtpConfig.client_id,
privateKey: smtpConfig.private_key,
}
});
Which unfortunately gives me this:
{ Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials h81sm3513839itb.18 - gsmtp
at SMTPConnection._formatError
...
Thanks!
you do not need a refresh token with a service account and nodemailer will generate the access key for you automatically
You cannot only use a service account to send emails for a GSuite account and not a gmail account.
If you have a gmail account you can use 3-legged OAuth2 authentication
Or turn on 2FA, generate an App Password and use that as seen here
If you ARE using a GSuite account you can use the ServiceAccount but you will have to make sure it has G Suite Domain-wide Delegation as described here and then you need to give access to the GSuite Domain as described here

Mail sending without authentication using Java Mail Api

I am following code to send mail without authentication.The java code described as follows.
final String frommail = "a#g.com"
String toEmail = "b#gmail.com";
Properties props = new Properties();
props.put("mail.smtp.auth", PropertiesLoader.getPropertiesValue(MAIL_SMTP_AUTH));
props.put("mail.smtp.host", PropertiesLoader.getPropertiesValue(MAIL_SMTP_HOST));
props.put("mail.smtp.port", PropertiesLoader.getPropertiesValue(MAIL_SMTP_PORT));
//enable authentication
props.put(MAIL_SMTP_ENABLE, PropertiesLoader.getPropertiesValue(MAIL_SMTP_ENABLE));
Session session = Session.getInstance(props);
try {
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress(frommail, EMAIL_FROM_NAME));
// msg.setReplyTo(InternetAddress.parse(frommail, false));
msg.setSubject(subject, "UTF-8");
msg.setContent(body, MAIL_CONTENT_TYPE);
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("pramilkprince#rediffmail.com", false));
Transport.send(msg);
logger.info("EMail Sent Successfully!!");
But when sending mail, it throws following exception
com.sun.mail.smtp.SMTPAddressFailedException: 554 5.7.1
: Relay access denied at
com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:1862) at
com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1118)
at javax.mail.Transport.send0(Transport.java:254) at
javax.mail.Transport.send(Transport.java:124) at
com.gtl.gcc.util.SendEmail.sendEmailFromInfo(SendEmail.java:195) at
com.gtl.gcc.util.SendEmail.createMailBodyAndSendMailForUpdateKYC(SendEmail.java:144)
if any one have any idea about this please help
If you want to send mail without authentication, you need to run your own SMTP server. If it's on the public internet, it will be flooded with spam before you can use it yourself. If it's on your private intranet, you can make it work. Gmail, for example, is definitely not going to let you send mail without authenticating. Note that this has absolutely nothing at all to do with use of the JavaMail API.
As #BillShannon pointed out, sending through most SMTP servers will require authentication due to SPAM issues.
However, the MX Gateways for any domain are required to be open on TCP 25 and for emails without authentication.
While working on Java send email avoiding smtp relay server and send directly to MX server I created a small example to use the target domain's MX server to directly address that.
As pointed out before: If your sending server does not reverse-DNS to the domain you're sending from, you'll most likely end up being blocked or directly classified as spam.
Good Luck

Error using Active Directory in SonarQube

I've been trying to configure Sonar with Active Directory for a while with no luck so I was really excited to see the new LDAP 1.5 plug-in. Unfortunately it's still not working for me but it's so close! The lookup is successful but then something fails:
DEBUG web[w.s.NegotiateSecurityFilter] logged in user: CORP\My.UserName (S-1-5-21-1305660829-1405082133-723345943-15257)
DEBUG web[w.s.NegotiateSecurityFilter] roles: CORP\My.UserName, CORP\Domain Users, Everyone, BUILTIN\Administrators, BUILTIN\Users, NT AUTHORITY\NETWORK, NT AUTHORITY\Authenticated Users, NT AUTHORITY\This Organization, [etc.]
INFO web[w.s.NegotiateSecurityFilter] successfully logged in user: CORP\My.UserName
DEBUG web[o.s.p.l.w.s.s.SsoAuthenticationFilter] Validating authenticated user
DEBUG web[http] GET /sessions/new?return_to=%2F | time=1527ms
ERROR web[rails] Error from external users provider: exception Java::Com4j::ComException: 80040e37 (Unknown error) : A referral was returned from the server.
DEBUG web[http] GET /ldap/validate | time=1738ms
This was with the Negotiate protocol but I got the same error using the default NTLM protocol as well. Running Sonar 5.2.
The 1.5.1 update fixed this issue for me.
https://jira.sonarsource.com/browse/LDAP-49

how to send emails in jsp using securesocketlayer?

I have been using java mail to send mail from jsp to send emails.
But google is blocking my request and I am getting a message from gmail to my inbox as Signin attempt blocked.
In order to send mails from my account I have to update my security setting access permission to less secureapps but I don't want to do it.
So how to send mails using ssl in java mail?
The sample code I am using is:
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
The "less secure apps" setting has nothing to do with SSL. It's required to allow you to login using just a username and password, instead of using the (more complicated, but presumably more secure) OAuth2 authentication technique.

Read Inbox using Java

I am working in a company as an intern and everyone in the company uses outlook for mails.
I couldn't figure out a way to read emails from the Inbox.
Let me give a view about my project. I am using JavaMail API for mails.
My first task - To send mails from java program using Reminder system, which I have successfully completed, by using the host, protocol, username and no password. There is no need of entering password, because if i try to enter the password it stopped working. Its working only without password.
so basically, I can send email using other employees in the company using there email address as "From" because there is no password need to be entered, possible only from java program.
My second task - To read the subject of the mail and the sender details and do some task...
There is no password to enter, but
Store store = session.getStore("smtp");
//I tried with imap, pop3, but everything gives error "No Such Provided"
store.connect("email#domain.co.uk", "PASSWORD");
store.connect() doesn't allow me to use the method without password.
The software will be using a new email address which is not the same address in users outlook but for testing I am using my email address first, because the software is going to be used by different users/computers.
If i try this way
session.getStore("imaps");
store.connect("host","username","password");
Error:
DEBUG: getProvider() returning
javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
DEBUG: mail.imap.statuscachetimeout: 1000
DEBUG: mail.imap.appendbuffersize: -1
DEBUG: mail.imap.minidletime: 10
DEBUG: trying to connect to host "host", port 993, isSSL true
javax.mail.MessagingException: Connection refused: connect;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:618)
at javax.mail.Service.connect(Service.java:291)
at javax.mail.Service.connect(Service.java:172)
at TestMail.InboxReader.main(InboxReader.java:52)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:529)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:570)
at
com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:284)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:227)
at com.sun.mail.iap.Protocol.<init>(Protocol.java:109)
at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:585)
... 3 more
Java Result: 2
Use Exchange Web Services (EWS) API for seamless integration with Exchange Server. Quoting from MS site
EWS provides access to much of the same data that is made available through Microsoft Office Outlook.
EWS basics - http://msdn.microsoft.com/en-us/library/exchange/dd877045(v=exchg.140).aspx
EWS Java API - http://archive.msdn.microsoft.com/ewsjavaapi
Need more details...
See the JavaMail FAQ and post the debug output when it fails. Also, the exact code you're using might be helpful.
You can test it all using code that comes with JavaMail, so you can determine whether the problem is in your code or something else. See the msgshow.java demo program included in the JavaMail download bundle.

Resources