How to send an email using Gmail API and Java - email

I have a Web Service deployed on Openshift. Currently what I'm developing is a kind of sending an automated email at a certain point, using my Gmail account.
So I have been documenting myself for two or three days and I've concluded that I've two options:
1) Using JavaMail library.
2) Using Gmail API.
For the first option What I've used is the following classes:
public class EmailSenderService {
private final Properties properties = new Properties();
private String password = "*******";
private Session session;
private void init() {
//ssl
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.port", "465");
session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("eu***#gmail.com",password);
}
});
}
public void sendEmail(){
init();
//ssl
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("eu***#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("c***6#gmail.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport t = session.getTransport("smtp");
t.connect("smtp.gmail.com", "eu***#gmail.com", password);
t.sendMessage(message, message.getAllRecipients());
System.out.println("Done");
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
And calling them using this:
EmailSenderService ess = new EmailSenderService();
ess.sendEmail();
2) The second option I'm using is the following:
public class EmailSenderGmailApi {
/*
* Atributos
*/
// Check https://developers.google.com/gmail/api/auth/scopes for all available scopes
private static final String SCOPE = "https://www.googleapis.com/auth/gmail.compose";
private static final String APP_NAME = "eu***l";
// Email address of the user, or "me" can be used to represent the currently authorized user.
private static final String USER = "eu***#gmail.com";
// Path to the client_secret.json file downloaded from the Developer Console
private static final String CLIENT_SECRET_PATH = "../app-root/data/eu***.json";
private static GoogleClientSecrets clientSecrets;
/*
* Metodos
*/
public static Gmail init() throws IOException{
System.out.println("***Working Directory = " + System.getProperty("user.dir"));
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
clientSecrets = GoogleClientSecrets.load(jsonFactory, new FileReader(CLIENT_SECRET_PATH));
// Allow user to authorize via url.
GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
httpTransport, jsonFactory, clientSecrets, Arrays.asList(SCOPE))
.setAccessType("online")
.setApprovalPrompt("auto").build();
String code = "***";
// Generate Credential using retrieved code.
GoogleTokenResponse response = flow.newTokenRequest(code)
.setRedirectUri(GoogleOAuthConstants.OOB_REDIRECT_URI).execute();
GoogleCredential credential = new GoogleCredential()
.setFromTokenResponse(response);
// Create a new authorized Gmail API client
return new Gmail.Builder(httpTransport, jsonFactory, credential)
.setApplicationName(APP_NAME).build();
}
/**
* Create a MimeMessage using the parameters provided.
*
* #param to Email address of the receiver.
* #param from Email address of the sender, the mailbox account.
* #param subject Subject of the email.
* #param bodyText Body text of the email.
* #return MimeMessage to be used to send email.
* #throws MessagingException
*/
public static MimeMessage createEmail(String to, String from, String subject,
String bodyText) throws MessagingException {
System.out.println("***Empezando a enviar email...");
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage email = new MimeMessage(session);
InternetAddress tAddress = new InternetAddress(to);
InternetAddress fAddress = new InternetAddress(from);
email.setFrom(new InternetAddress(from));
email.addRecipient(javax.mail.Message.RecipientType.TO,
new InternetAddress(to));
email.setSubject(subject);
email.setText(bodyText);
return email;
}
/**
* Create a Message from an email
*
* #param email Email to be set to raw of message
* #return Message containing base64 encoded email.
* #throws IOException
* #throws MessagingException
*/
public static Message createMessageWithEmail(MimeMessage email)
throws MessagingException, IOException {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
email.writeTo(bytes);
String encodedEmail = Base64.encodeBase64URLSafeString(bytes.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
/**
* Send an email from the user's mailbox to its recipient.
*
* #param service Authorized Gmail API instance.
* #param userId User's email address. The special value "me"
* can be used to indicate the authenticated user.
* #param email Email to be sent.
* #throws MessagingException
* #throws IOException
*/
public static void sendMessage(Gmail service, String userId, MimeMessage email)
throws MessagingException, IOException {
Message message = createMessageWithEmail(email);
message = service.users().messages().send(userId, message).execute();
System.out.println("Message id: " + message.getId());
System.out.println(message.toPrettyString());
}
}
The first option, when calling to it, the message which is shown in Openshift COnsole is the following:
javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at main.java.model.EmailSenderService.sendEmail(EmailSenderService.java:86)
at main.java.model.AccessManager.renewPassStepOne(AccessManager.java:234)
at main.java.webService.UsuarioService.renewPassStepOne(UsuarioService.java:192)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:302)
...
I've been trying to fix it by myself, looking at google, stackoverflow... But every change I introduce, the same message I get.
In option 2, I don't know how to use it. I'm trying something like that:
MimeMessage msg = EmailSenderGmailApi.createEmail("ca***#gmail.com", "eu***#gmail.com", "test", "holaaaaa");
EmailSenderGmailApi.sendMessage(
EmailSenderGmailApi.init(),
"cap***#gmail.com",
msg);
Anyway, to be honest I have investigated a lot of Java Mail, I hope someone can give me a hand solving any error I would have.
In relation to Gmail Api, on official documentation I haven't been able to figure out how to send an email. Neither there isn't so much documentation over the internet.
Could somebody lend me a hand?

The Gmail API public docs have a guide on sending email and it even has java sample code:
https://developers.google.com/gmail/api/guides/sending
Your code above doesn't seem that far off though. I'd first make sure you got Oauth2 working right by doing something simple like labels.list() and if that works then move on to something more complicated like sending an email. (You have the right idea constructing, turning into a string, base64url encoding and then sending it though.) What is the exact problem you're getting whist trying to send it with the Gmail API? Got some error output or missing something in your code?

If you are using Java Mail API in your openshift application,
Then adding any new libraries in the application, you have to add its maven-configuration in the pom.xml file. Or in other words, you have to add dependency in the pom.xml file.
this is the dependency for mail-1.4.7
Just add this code in the pom.xml file
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
Similarly, for any other integrations, not forget to add dependencies.
You can search in google :
"maven dependency for ________"

Start by fixing these common mistakes in your JavaMail code.
See this JavaMail FAQ entry for sending email through Gmail.
See this JavaMail FAQ entry for how to debug problems with JavaMail.
See this JavaMail wiki page about using OAuth2.

Related

How can I get "Amazon.Extensions.CognitoAuthentication.CognitoUserSession.IDToken" From AWSCredentials?

I want get "Amazon.Extensions.CognitoAuthentication.CognitoUserSession.IDToken" From AWSCredentials.
I have AWSCredentials From Oauth Google Login.
public AWSCredentials GetAWSCredentials_Google(string token)
{
CognitoAWSCredentials credentials = new CognitoAWSCredentials(FED_POOL_ID, regionTable[REGION]);
credentials.AddLogin("accounts.google.com", token);
return credentials;
}
And, I use EC2 Instance and my ubuntu server is in there. Also, I was originally using a method of accessing the server by receiving a membership from Cognito User Pool, so I was using the following code.
private IEnumerator sendPostUser()
{
string uri = rootUrl + "/user";
string json = "{ ... }";
byte[] jsonData = System.Text.Encoding.UTF8.GetBytes(json);
using (UnityWebRequest request = UnityWebRequest.Post(uri, json))
{
if (request.uploadHandler != null)
request.uploadHandler.Dispose();
request.disposeUploadHandlerOnDispose = true;
request.disposeDownloadHandlerOnDispose = true;
request.uploadHandler = new UploadHandlerRaw(jsonData);
/* Header */
request.SetRequestHeader("Content-Type", "application/json");
request.SetRequestHeader("token", cloud_acess.GetComponent<ControlCloud>().cognitoUser.SessionTokens.IdToken);
/* Send Message */
yield return request.SendWebRequest();
...
}
By the way, there was a problem with this code "request.SetRequestHeader("token", cloud_acess.GetComponent().cognitoUser.SessionTokens.IdToken);".
This cognitouser means Amazon.Extensions.CognitoAuthentication.CognitoUser.
My Project get CognitoUser using user's ID and PW, and get AWSCredentials using this Cognitouser. But Google Login doesn't this process and just get credentials.
So, I can't get "cognitoUser.SessionTokens.IdToken". It makes me cannot to request anything from ec2 server.
How Can i get this? What should I do if the premise of this problem itself is wrong?
I tried to put all the tokens I received when I logged in to Google and the tokens I received as credentials in the header.But I failed.

Jenkins: Seting up mail notifications [duplicate]

I am beginner in java and I want to send an email in java, for that I am using this code in Java. But my code is throwing an exception, and I need a heads-up why…
This is stack trace of exception:
javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt=AKgnsbsNX
534-5.7.14 No6jJbDc4l7fZ_WLdBD0sNHIIp_nLvplRMm0bYFBnZBF_XOyVvNSdd1FenDZJPwBTFQyRH
534-5.7.14 lriPK3myMm-dXkW3zK0-6XpO7BzI8hfRcByG1k7YiVzXlddTvs7QhjtgCWNcrzMBuPhoof
534-5.7.14 GjME2TgYzXJVHz5MV98nRnr_kq-kP7RmgOtX3IQHLwM5E8QGBC9-2THVQr_Ch_U0-1nZsc
534-5.7.14 yoPuNEw> Please log in via your web browser and then try again.
534-5.7.14 Learn more at
534 5.7.14 https://support.google.com/mail/bin/answer.py?answer=78754 wr6sm26888533wjc.24 - gsmtp
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:364)
at javax.mail.Service.connect(Service.java:245)
at SendEmail.sendFromGMail(SendEmail.java:50)
at SendEmail.main(SendEmail.java:18)
sent
This is my code
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendEmail {
private static String USER_NAME = "me";
private static String PASSWORD = "xyz";
private static String RECIPIENT = "abc#seecs.edu.pk";
public static void main(String[] args) {
String from = USER_NAME;
String pass = PASSWORD;
String[] to = { RECIPIENT };
String subject = "Java send mail example";
String body = "Welcome to JavaMail!";
sendFromGMail(from, pass, to, subject, body);
System.out.println("sent");
}
private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
Properties props = System.getProperties();
String host = "smtp.gmail.com";
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.user", from);
props.put("mail.smtp.password", pass);
props.put("mail.smtp.port", "587");
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(from));
InternetAddress[] toAddress = new InternetAddress[to.length];
for( int i = 0; i < to.length; i++ ) {
toAddress[i] = new InternetAddress(to[i]);
}
for( int i = 0; i < toAddress.length; i++) {
message.addRecipient(Message.RecipientType.TO, toAddress[i]);
}
message.setSubject(subject);
message.setText(body);
Transport transport = session.getTransport("smtp");
transport.connect(host, from, pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
catch (AddressException ae) {
ae.printStackTrace();
}
catch (MessagingException me) {
me.printStackTrace();
}
}
}
I had the same problem! Google prevents access for less secure apps.
This is how I solved it:
Log in from your browser to that email account.
Go to https://www.google.com/settings/security/lesssecureapps
You will see Turn off or Turn on. Click on Turn on, then try your code again. It should work now.
Make sure Less secure apps is TURNED ON
https://www.google.com/settings/security/lesssecureapps
Allow each app to send email
Go to https://accounts.google.com/b/0/DisplayUnlockCaptcha
and click on Continue.
This time: you can use your app to send email and all operations are allowed.
More links:
https://support.google.com/accounts/answer/6010255
https://productforums.google.com/forum/#!topic/gmail/9KCgzXY4G_c
allow less secure apps from your google account settings
https://www.google.com/settings/security/lesssecureapps
open your gmail> goto settings > IMAP/POP settings > enable IMAP
goto this url and turn this on (completing all these steps will surely work)
https://www.google.com/accounts/DisplayUnlockCaptcha
official guide for help
https://support.google.com/mail/answer/7126229?hl=en-GB&visit_id=637246238859828954-474780158&rd=2
I am also facing same problem.
Google does't give you direct open port access.
If you are using your your google account for mail sending please Turn on the setting by clicking on this link.
google go here : https://www.google.com/settings/security/lesssecureapps
Best of luck hope it will work for you as well.
Its not just about enabling the "less secure apps" from google admin page.
The above fix did not solve the issue for me. I contacted google and they asked me to add couple of TXT records to my DNS list. You will need a DKIM key that you will need to generate from the G Suite admin settings for gmail.

GCM Cloud Connection Server doesn't respond to SASL on XMPP

Using agsXMPP to connect to Google Cloud Messaging XMPP API for the purpose of sending notification to Android devices.
The connection is established OK, but on SASL start, after sending the PLAIN auth element, the server stops responding, and closes the connection after a further 20 seconds.
Base64 decoding the auth example from the documentation page (http://developer.android.com/google/gcm/ccs.html) shows login values of:
126200347933#projects.gcm.android.com12620034793#projects-ga-.android.comAIzaSyB3rcZNkfnqKdFb9mhzCBiYpORDA2JWWtw
Where as agsXMPP is (correctly I think) encoding the string, to give something like:
[ProjectID]\40gcm.googleapis.com[**API*KEY*PASSWORD**]
Note the \40 in my version instead of the # in the Google example - could this make a difference?
I'm expecting either a success or failure message, no response at all is difficult to debug. Could this at character be responsible for some failure, or does Google's implementation of XMPP just not provide the correct responses.
UPDATED:
I answered below, essentially, yes, Google can't handled the encoded # character because it doesn't support that XMPP extension.
After some more testing, I added a new SaslFactory mechanism in agsXMPP and bound it to use the username without encoding (part of extension http://xmpp.org/extensions/xep-0106.html, which Google doesn't support), and then on SaslStartEvent - specify that I want to use that mechanism instead of the inbuilt plain one. - and now the connection will continue normally.
xmpp = new XmppClientConnection();
xmpp.UseSSL = true;
xmpp.UseStartTLS = false;
xmpp.Server = "gcm.googleapis.com";
xmpp.ConnectServer = "gcm.googleapis.com";
xmpp.Port = 5235;
/* Other connection settings /*
SaslFactory.AddMechanism("MyPLAINMechanism", typeof(MyPlainMechanismClass));
xmpp.OnSaslStart += (sender, args) =>
{
args.Auto = false;
args.Mechanism = "MyPLAINMechanism";
args.ExtentedData = new GcmPlainSaslExtendedData
{
Username = "MY UNENCODED USERNAME"
};
};
Then we define the MyPlainMechanismClass which inherits from the Mechanism in agsXMPP, the source code is the same as the original PlainSaslMechanism except the line where the username is input - you can pass in an unencoded username using the ExtendedData property on args.
public class MyPlainMechanismClass: Mechanism
{
private XmppClientConnection m_XmppClient = null;
public GcmPlainSaslMechanism()
{
}
public override void Init(XmppClientConnection con)
{
m_XmppClient = con;
// <auth mechanism="PLAIN" xmlns="urn:ietf:params:xml:ns:xmpp-sasl">$Message</auth>
m_XmppClient.Send(new agsXMPP.protocol.sasl.Auth(agsXMPP.protocol.sasl.MechanismType.PLAIN, Message()));
}
public override void Parse(Node e)
{
// not needed here in PLAIN mechanism
}
private string Message()
{
// NULL Username NULL Password
StringBuilder sb = new StringBuilder();
//sb.Append( (char) 0 );
//sb.Append(this.m_XmppClient.MyJID.Bare);
sb.Append((char)0);
//sb.Append(this.Username);
sb.Append(((GcmPlainSaslExtendedData) this.ExtentedData).Username);
sb.Append((char)0);
sb.Append(this.Password);
byte[] msg = Encoding.UTF8.GetBytes(sb.ToString());
return Convert.ToBase64String(msg, 0, msg.Length);
}
}
Our custom ExtendedData object which we use to pass in custom arguments, such as an unencoded username in this case.
public class GcmPlainSaslExtendedData : agsXMPP.Sasl.ExtendedData
{
public string Username { get; set; }
}

Java send mail - how to use "Send via"

We are sending mails from our local system.
We got our IPs white listed.
We have a scenario where we have to send email on behalf of somebody.
for ex: our email id is: support#mycompany.com
but we need to send email with a from address: john#abc.com
When we send with different from address, the receiving mail client displays "phishing" error.
One of the solution is to use "via" as dispayed in google link
https://mail.google.com/support/bin/answer.py?hl=en&ctx=mail&answer=185812
We also want the message to be displayed like this in receivers inbox.
Any pointers in this will help us a lot.
thanks in advance.
Note: We are using localhost as the smtp.
Read about email headers. you can add email headers while creating the mail message at runtime.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.jcom.net");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
for further reading check this :
http://www.javacommerce.com/displaypage.jsp?name=javamail.sql&id=18274
http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/package-summary.html
#http://javamail.kenai.com/nonav/javadocs/javax/mail/internet/MimeMessage.html
You can create aliases for the smtp server too.

BlackBerry - Facebook extended permissions

I've just found a great sample of Facebook Connect on Blackberry by Eki Y. Baskoro,
The following is a short HOWTO on using Facebook Connect on Blackberry. I created a simple Facade encapsulating the Facebook REST API as well as added 'rough' MVC approach for screen navigation. I have tested on JDE 4.5 using 8320 simulator. This is still work in progress and all work is GPLed.
It works great for reading stuff.
NB Don't forget to get Facebook App Key and set it in TestBB class.
But now I want to post something on my wall. So I've add new method to FacebookFacade class using Stream.publish API:
/***
* Publishes message to the stream.
* #param message - message that will appear on the facebook stream
* #param targetId - The ID of the user, Page, group, or event where
* you are publishing the content.
*/
public void streamPublish(String message, String targetId)
{
Hashtable arguments = new Hashtable();
arguments.put("method", "stream.publish");
arguments.put("message", message);
arguments.put("target_id", targetId);
try {
JSONObject result = new JSONObject(
int new JSONTokener(sendRequest(arguments)));
int errorCode = result.getInt("error_code");
if (errorCode != 0) System.out.println("Error Code: "+errorCode);
} catch (Exception e) {
System.out.println(e);
}
}
/***
* Publishes message on current user wall.
* #param message - message that will appear on the facebook stream
*/
public void postOnTheWall(String message)
{
String targetId = String.valueOf(getLoggedInUserId());
streamPublish(message, targetId);
}
This will return Error code 200, "The user hasn't authorized the application to perform this action"
First I thought it's related with Facebook -> Application Settings -> Additional Permissions -> Publish recent activity (one line stories) to my wall but even checked, no difference...
Then I've found this post explains that issue related with extended permissions.
This in turn should be fixed by modifying url a little in LoginScreen class :
public LoginScreen(FacebookFacade facebookFacade) {
this.facebookFacade = facebookFacade;
StringBuffer data = new StringBuffer();
data.append("api_key=" + facebookFacade.getApplicationKey());
data.append("&connect_display=popup");
data.append("&v=1.0");
//revomed
//data.append("&next=http://www.facebook.com/connect/login_success.html");
//added
data.append("&next=http://www.facebook.com/connect/prompt_permissions.php?" +
"api_key="+facebookFacade.getApplicationKey()+"&display=popup&v=1.0"+
"&next=http://www.facebook.com/connect/login_success.html?"+
"xxRESULTTOKENxx&fbconnect=true" +
"&ext_perm=read_stream,publish_stream,offline_access");
data.append("&cancel_url=http://www.facebook.com/connect/login_failure.html");
data.append("&fbconnect=true");
data.append("&return_session=true");
(new FetchThread("http://m.facebook.com/login.php?"
+ data.toString())).start();
}
Unfortunately it's not working. Still Error Code 200 in return to stream.publish request...
Do you have any suggestions how to resolve this?
Thank you!
I have posted the updated API on my website (http://www.baskoro.web.id/facebook-connect-blackberry-HOWTO.html) and this should solve this issue. Please let me know otherwise.
Salam. Cheers!
Eki