Send Gmail in Android.
and forward, reply, all-reply
public static void sendMail(Context context, Account account, MailDetailObject mailDetailObject, String mailId, GmailNetworkListener listener) {
try {
GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2(context, Collections.singleton(MAIL_SCOPE));
credential.setSelectedAccount(account);
Gmail service = new Gmail.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).build();
new SendMailTask(account, service, mailDetailObject, mailId, listener).execute();
} catch (Exception e) {
e.printStackTrace();
}
}
private static class SendMailTask extends AsyncTask<Void, Void, Message> {
private Account account;
private Gmail service;
private MailDetailObject mailDetailObject;
private String mailId;
private List<MailAttachmentObject> ATTACHMENTS;
private GmailNetworkListener listener;
public SendMailTask(Account account, Gmail service, MailDetailObject mailDetailObject, String mailId, GmailNetworkListener listener) {
this.account = account;
this.service = service;
this.mailDetailObject = mailDetailObject;
this.mailId = mailId;
this.ATTACHMENTS = new ArrayList<>();
this.listener = listener;
}
@Override
protected Message doInBackground(Void... voids) {
if (listener == null) return null;
try {
Message message = null;
if (mailId != null) {
message = service.users().messages().get(account.name, mailId).setFormat("raw").execute();
Base64 base64Url = new Base64(true);
byte[] emailBytes = base64Url.decodeBase64(message.getRaw());
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage origiMessage = new MimeMessage(session, new ByteArrayInputStream(emailBytes));
message = createMessage(mailDetailObject, origiMessage);
} else {
message = createMessage(mailDetailObject, null);
}
return service.users().messages().send(account.name, message).execute();
} catch (Exception e) {
e.printStackTrace();
listener.onFailure(e.toString());
}
return null;
}
@Override
protected void onPostExecute(Message result) {
super.onPostExecute(result);
if (listener != null) {
listener.onSuccess(result);
}
}
private Message createMessage(MailDetailObject mailDetailObject, MimeMessage originMessage) throws Exception {
if (mailDetailObject.TO.size() == 0 || mailDetailObject.FROM == null) {
return null;
}
if (originMessage != null) {
getAttachmentItem(originMessage.getContent());
mailDetailObject.ATTACHMENTS.addAll(this.ATTACHMENTS);
}
Properties props = new Properties();
Session session = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setSubject(mailDetailObject.SUBJECT);
message.setFrom(new InternetAddress(mailDetailObject.FROM.EMAIL, mailDetailObject.FROM.NAME));
for (AccountObject item : mailDetailObject.TO) {
message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(item.EMAIL, item.NAME));
}
if (mailDetailObject.CC.size() > 0) {
for (AccountObject item : mailDetailObject.CC) {
message.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(item.EMAIL, item.NAME));
}
}
if (mailDetailObject.BCC.size() > 0) {
for (AccountObject item : mailDetailObject.BCC) {
message.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(item.EMAIL, item.NAME));
}
}
if (mailDetailObject.ATTACHMENTS.size() == 0) {
message.setContent(mailDetailObject.CONTENT.BODY, mailDetailObject.CONTENT.CONTENT_TYPE);
} else {
List<MailAttachmentObject> inlineFiles = new ArrayList<>();
List<MailAttachmentObject> attachFiles = new ArrayList<>();
for (MailAttachmentObject item : mailDetailObject.ATTACHMENTS) {
if (item.IS_INLINE.equals("true")) {
inlineFiles.add(item);
} else {
attachFiles.add(item);
}
}
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(mailDetailObject.CONTENT.BODY, mailDetailObject.CONTENT.CONTENT_TYPE);
MimeMultipart mimeMultipart = null;
if (inlineFiles.size() > 0) {
mimeMultipart = new MimeMultipart();
mimeMultipart.setSubType("related");
mimeMultipart.addBodyPart(mimeBodyPart);
for (MailAttachmentObject item : inlineFiles) {
if (item.CONTENT != null && item.CONTENT.length() > 0) {
MimeBodyPart inlinePart = new PreencodedMimeBodyPart("base64");
inlinePart.setContent(item.CONTENT, item.FILE_TYPE);
inlinePart.setContentID("<" + item.CONTENT_ID + ">");
inlinePart.setDisposition(MimeBodyPart.INLINE);
mimeMultipart.addBodyPart(inlinePart);
} else if (item.CONTENT_ORIGINAL != null && item.CONTENT_ORIGINAL.length() > 0) {
MimeBodyPart inlinePart = new MimeBodyPart();
inlinePart.attachFile(item.CONTENT_ORIGINAL, item.FILE_TYPE, "base64");
inlinePart.setContentID("<" + item.CONTENT_ID + ">");
inlinePart.setDisposition(MimeBodyPart.INLINE);
mimeMultipart.addBodyPart(inlinePart);
}
}
}
if (inlineFiles.size() > 0 && attachFiles.size() > 0) {
mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setContent(mimeMultipart);
}
if (attachFiles.size() > 0) {
mimeMultipart = new MimeMultipart();
mimeMultipart.setSubType("mixed");
mimeMultipart.addBodyPart(mimeBodyPart);
for (MailAttachmentObject item : attachFiles) {
if (item.CONTENT != null && item.CONTENT.length() > 0) {
MimeBodyPart attachFile = new PreencodedMimeBodyPart("base64");
attachFile.setContent(item.CONTENT, item.FILE_TYPE);
attachFile.setFileName(item.FILE_NAME);
attachFile.setDisposition(MimeBodyPart.ATTACHMENT);
mimeMultipart.addBodyPart(attachFile);
} else if (item.CONTENT_ORIGINAL != null && item.CONTENT_ORIGINAL.length() > 0) {
MimeBodyPart attachFile = new MimeBodyPart();
attachFile.attachFile(item.CONTENT_ORIGINAL, item.FILE_TYPE, "base64");
attachFile.setDisposition(MimeBodyPart.ATTACHMENT);
mimeMultipart.addBodyPart(attachFile);
}
}
}
message.setContent(mimeMultipart);
}
return createFromMimeMessage(message);
}
private Message createFromMimeMessage(MimeMessage email) throws IOException, MessagingException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
email.writeTo(baos);
String encodedEmail = Base64.encodeBase64URLSafeString(baos.toByteArray());
Message message = new Message();
message.setRaw(encodedEmail);
return message;
}
private void getAttachmentItem(Object content) throws Exception {
if (content instanceof MimeMultipart) {
MimeMultipart multipart = (MimeMultipart) content;
for (int i = 0; i < multipart.getCount(); i++) {
MimeBodyPart bp = (MimeBodyPart) multipart.getBodyPart(i);
if (bp.getFileName() != null && bp.getFileName().length() > 0) {
this.ATTACHMENTS.add(new MailAttachmentObject(bp));
} else {
getAttachmentItem(bp.getContent());
}
}
}
}
}