Read exchange email Oauth2 in java - java

I have discovered that basic authentication to email account no longer works, when using java.mail library. It seems that an OAuth2 based authentication is needed. I need help in defining the actual steps. I have created a sample app in Azure and granted the IMAP, POP and SMTP permissions to it, but I don't know what are the next steps, how to actually tell my email reader (in Java code) that it should use this app and actually connect to the email inbox. The email server is a Microsoft Exchange server.

Related

Android reading emails without enabling less secure app

So i came across this Less secure apps & your Google Account link, And it states that after the 30th of may this year, Enabling 'less secure apps' option will no longer be available.
I was wondering if there will be any alternative? I need to be able to read emails and download attachments through my application without the user having to do so himself, So the Gmail API is no good as far as i could see.
I also couldn't find any other option to access a gmail inbox without enabling this option... Ty for your time.
The whole point of this is that you should not be using someone's login and password to access gmail. You could try using an apps password but it is unlcear if this will work or not in the long run. Google has been very vague about that.
You should be authorizing the user either using Xoauth2 for the smtp server or by using the Gmail api and authorizing the user.
With oauth you can request a refresh token and then you will only need to request the users permission once after that you can use your refresh token to request a new access token.
The java mailer API works on password authentication(username and password). And if you want to use it with Gmail SMTP Server Address as the host you need to allow access less secure apps in your email account but this option is not available since may 30, this password authentication method will not work using gmail.
The option you can have is to use the gmail API to read/write to your gmail account another option is to use oauth2 to authenticate the account and save the tokens to the file so that you can use it to authenticate every time you call the api. For more on authorizing your app with google click here

I need to send emails from an email hosted on a Microsoft Exchange server

I successfully created a java program that can send emails from a gmail account, however, now I need to do the same with an email account using a Microsoft Exchange Server.
The difference is when I was creating the gmail application I had all the information I needed to just plug into the JavaMail API program to make it work. For example, the SMTP server is smtp.gmail.com, the port is 587, the account needs to be set to "Less secure login requirements", etc...
I do not have access to any of this information for the Exchange Server. I am wondering if this is possible to do? I would assume I probably wont be able to use the JavaMail API like I did with gmail seeing as i don't have any of the information that is required for the fields. Is there some other library out there that I can use?
If some of this sounds like I don't really know what I'm talking about it's probably because I don't. This is kind of new territory for me, and I seem to have hit a wall. Basically I just need a program that can send an email from an account hosted on a Microsoft Exchange server. Hopefully this make since. Thanks.
Ms Exchange and GMail are both email providers, they rely on the same protocol : SMTP. So your JavaMail API should also work with Exchange, provided the required features are enabled :
SMTP : should be enabled by default, unless your admin disabled it.
SMTP Authentication, or SMTP relay for your sender IP address : Some authentication mechanisms (AUTH LOGIN) are disabled by default on latest versions of Exchange. Relay also has to be configured by the admin.
The easiest way to find out your exchange server address and port is to contact your Exchange administrator. However, you can find it by yourself by :
If you're not on the exchange local network, you can try "nslookup -q=mx senderdomain.com" to find out the Exchange IP adderss
Else, you can try and follow this procedure
Once you have the Exchange IP address, confirm that SMTP delivery port (25) is opened, it should be. If relay is authorized for your IP, you should be done.
Else, test if SMTP submission port (587) is opened, that should be the one you use to send authenticated emails.
Alternatively, you can try and use Exchange Web Services API, provided it's enabled on the server. There seems to be a Microsoft JAVA libray to use EWS : ews-java-api.

Can't set the email sender in JavaMail - Spring [duplicate]

I want to send an email from A to B, with HEADER and CONTENT through gmail.
How to do that by PHP?
I've specified the FROM (from#example.com), but when I receive the email, it's still from my gmail account (abc#gmail.com).
$mail->From = "from#example.com";
$mail->FromName = "Mailer";
$mail->AddAddress("abc12#163.example", "Josh Adams");// name is optional
$mail->AddReplyTo("abc12#qq.example", "Information");
How do I change the FROM part?
The short answer - you can't.
Google rewrites the From and Reply-To headers in messages you send via it's SMTP service to values which relate to your gmail account.
The SMTP feature of gmail isn't intended to be an open or relay service. If it allowed any values for the From header, it would significantly dilute Google's standing with spam services, as there would be no way to verify the credentials of the sender.
You need to consider alternatives. How are you planning to host your script/application/website when it's finished: virtually every hosting solutions (shared/vps/dedicated server) will come pre-configured with an email transfer solution: be it sendmail or postfix on *nix, or IIS on Windows.
If you are intent on using gmail then you could:
Setup a dedicated myapp#gmail.com account
If you own the domain you are supposedly sending from, use the free gmail for domains, and setup a myapp#mydomain.example account.
====
Edit June 2015
It was suggested that GMail does allow sending via different addresses. As far as I can tell, this is for sending via the GMail wep app, and utilises your existing external SMTP server, which is not relevant to the original question.
====
Edit Nov 2013
Seeing as this is still getting a trickle of votes. A quick update.
Google have withdrawn their free GMail for domains. There are plenty of other free services around. One of note is Mandrill - a one-to-one email service intended for transactional emails (e.g. ecommerce orders etc.). It's ran by MailChimp, who pretty much know all there is to know about sending email at volume. They also give you 12k/month free, which is rather nice.
This question and correct answer may be relevant:
When using Gmail for SMTP, can you set a different "from" address?
Gmail requires you to validate From addresses before sending mail as that email address. So you need to add a new sender in your personal gmail account and validate it.
Doing so will allow you to authenticate with youremail#gmail.com and send email from from#example.com
Unlike everyone else, I'll take the plunge and make the assumption that by letters you mean emails...
But I'm not sure what you are getting at when you mention that it should include "Headers and Content". Do you want to forward emails? Do you want the emails from A to appear as though they came from B's gmail account in the headers? Are you building some sort of gmail client?
The easiest way to send an email with PHP is with the mail function. This example comes straight from their documentation:
$to = 'nobody#example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
If you want the headers to appear from A's gmail and not to simply change the from/reply to part, you'd have to use gmail as the SMTP server. I don't know if you can set that at the script level.
The answer above are not quite correct.
You are definitely able to specify any senders as long as you own the other email address.
As the help page explains:
On your computer, open Gmail.
In the top right, click Settings.
Click the Accounts and import or Accounts tab.
In the "Send mail as" section, click Add another email address.
Enter your name and the address you want to send from.
Click Next Step and then Send verification.
For school or work accounts, enter the SMTP server (for example, smtp.gmail.com or smtp.yourschool.edu) and the username and password on that account.
Click Add Account.
Once that email is added successfully,
you can send email on the behalf of the new email address in gmail.
Google will not rewrite your from email in this way while you're sending email via Google SMTP.
You need to go to GMAIL settings and add new alias.
You will be asked SMTP information, which is basically useless, since you are using SMTP to send email, BUT the catch is that if your alias is on Google Suite domain it will be added just with simple email confirmation!
Once you have the alias there, you can change "From" header in your SMTP email.
NOTE: You cannot change the "From" address to whatever#dude.example, that's just how Gmail works and is the reason it's trusted.
If the reason you want to use gmail is because you don't want to set up an MTA (the reason you stated in a comment to this answer), you have 2 options:
If the web server is at your
home/work place; use your ISP's
smtp-server
If the web server is at a dedicated
hosting center, ask them what
smtp-server to use.

How to send app data securely to a recipient/server

I'm currently developing an android app where the user has to fill out and successfully send the data of a few text fields to a recipient/server, to enable a feature.
The big issue is how to do that in a secure way to be protected against e.g. decompiling. My concern is not the security during the transport but rather the security of the transport medium.
What I've thought/read so far:
I could send the data via mail with the Java Mail API.
First of all, I don't want require that the user has to enter his mail credentials and SMTP server.
That would mean that I have to include the credentials to a mail account in the app, though.
To avoid the situation that somebody decompiles the app and takes over my mail account, I thought of encrypting methods, but even if I would save the aes encrypted version of the password, the attacker could decompile the app and could add a syso to output the decrypted password.
The same applies to OAuth authentication because I have to store an authentication token.
In addition to the mail version, I read something about getting the password with a POST request from a web service, which doesn't seem safer at all.
I could search for free smtp server without the need of credentials, but I want something I can rely on instead of waking up each day and looking if the service still works.
Send the data to a web service.
Okay that would require more work for me, but I would accept that, if there would be a solution without saving the credentials in the app or having a web service which accepts data from everybody.
Have I overlooked something? Or is there no safe method without asking the user for his mail credentials or google account etc. ?
OAuth would probably work. The nice thing about OAuth is that if a token is compromised it can be revoked on the server side.
You could create a web service that accepts TCP connections on some port. You could have some authentication mechanism for example Digest authentication that would be carried out before accepting data.
Another option would be to use an API such as Golgi. Golgi requires a developer key, app key and app instance id to connect to the servers and send data. In the event these credentials somehow get compromised you can simply change the app key and push a new version of the app through the Play Store.

How to make reply of an email becomes a HttpRequestServlet to a web application?

I got a requirement which I don't know how to get started.
Requirement:
The application will send emails to clients (using org.springframework.mail.javamail.JavaMailSender) with same "from email address" as support#mydomain.com. Also the email content will have some hidden information such as client_id. If the client reply back to that email, I need to get that as an HttpServletRequest to a Servlet so that I can process it.
EDIT:
This web application does not have google app engine setup, but my requirement is matching similar to this link.
When someone replies to the mail you sent him that mail will go to the POP3 server configured for your domain. From this SMTP server you can read this mail using the Java Mail API. You can have a Spring timer task to keep polling this mailbox after every few seconds.
I would look at the below resources to see how to read mail from the POP3 server -
Using JavaMail API -
http://metoojava.wordpress.com/2010/03/21/java-code-to-receive-mail-using-javamailapi
Using Spring -
http://blog.solidcraft.eu/2011/04/read-emails-from-imap-with-spring.html
Better and popular solution is to have a link in your email and asking user to click it and asking specifically not to reply to this email (from address can be noreply#domain.com)
For getting a servlet call in an email reply, you need to use
http://javamail.kenai.com/nonav/javadocs/com/sun/mail/pop3/package-summary.html
Keep polling for new emails in your reply address(for eg support#domain.com) using POP, and when new email comes trigger a httprequest(HttpClient? ).
On a related note you can add user_id and similar stuff using a + in reply email address
For eg support#domain.com and support+uid_21_catid_32#domain.com will deliver to same address. Everything after + is ignored. But you will get this info in reply email event as to address and decide on who is the user.This could be a security concern for sure if not properly implemented.

Categories

Resources