I'm trying to use Gmail API for sending emails using my Google account.
I have checked the main guide here : Api Android Quickstart
and used methods here : Sending Email example
Everything seems fine when authorizing the Api and using it in my activity. I can fetch information from my account successfully.
Unfortunately, I'm getting error while sending e-mail. I need help with this issue.
Here's the code I'm working on:
public class SEND_EMAIL extends AsyncTask<MimeMessage, MimeMessage, MimeMessage>
{
@Override
protected MimeMessage doInBackground(MimeMessage... mime_messages) {
try {
mService.users().messages().send(PREF_ACCOUNT_NAME, createMessageWithEmail(createEmail(PREF_ACCOUNT_NAME, PREF_ACCOUNT_NAME, "Subject", "Hello Gmail.")));
} catch (IOException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
Log.i("AATS", "Trying to send email.");
return null;
}
}
Methods that are being used from example:
public static MimeMessage createEmail(String to, String from, String subject,
String bodyText) throws MessagingException {
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;
}
public static Message createMessageWithEmail(MimeMessage email)
throws MessagingException, IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
email.writeTo(baos);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
Libraries:
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.Base64;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.services.gmail.Gmail;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.gmail.model.ListMessagesResponse;
import com.google.api.services.gmail.model.Message;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
Function call:
private void refreshResults() {
if (credential.getSelectedAccountName() == null) {
chooseAccount();
} else {
if (isDeviceOnline()) {
new ApiAsyncTask(this).execute();
} else {
mStatusText.setText("No network connection available.");
}
}
SEND_ASNYCTASK_EMAIL();
}
public void SEND_ASNYCTASK_EMAIL ()
{
Log.i("AATS", "Sending email.");
new SEND_EMAIL().execute();
}
@Override
protected void onResume() {
super.onResume();
if (isGooglePlayServicesAvailable()) {
refreshResults();
} else {
mStatusText.setText("Google Play Services required: " +
"after installing, close and relaunch this app.");
}
}
---[EDIT 1]---
After applying gmail.compose scope with ND003's suggestion, I got a new warning.
Updated Trace:
07-02 15:28:36.252 20997-21201/com.adak.aats.send_email W/﹕ Unable to open '/system/framework/WfdCommon.jar': No such file or directory
07-02 15:28:36.252 20997-21201/com.adak.aats.send_email W/art﹕ Failed to open zip archive '/system/framework/WfdCommon.jar': I/O Error
---[EDIT 2]--- [WORKING]
I am still getting the same warning but the API works as intended and is able to send mails using my Google account.
Okey so, only thing I did was adding line:
compile files('libs/mail.jar', 'libs/activation.jar', 'libs/additional.jar')
in my build.gradle(Module: app) file in Android Studio. It's definitely not understandable for me since I also have:
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
}
by default in my build.gradle file. So it should already compile all the .jar files in app/libs folder anyway. I wasn't getting any errors either while using javax library. Seems a bit strange for me, but still posted so maybe someone can clarify what's going on.
Thanks.
-----------------------NOTE-------------------------
Gmail API uses javax.mail and javax.activation libraries referred to Sending Email example on top (can't share link due to low reputation).
That's why you need to import these three files in your project, app/libs folder: mail.jar, activation.jar, additional.jar.
Referred answer: this