본문 바로가기

카테고리 없음

[Gmail API] Draft Mail

Draft Gmail in Android

임시 보관함으로 메일 보내기

 public static void sendDraftMail(Context context, Account account, MailDetailObject mailDetailObject, 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 SendDraftMailTask(account, service, mailDetailObject, listener).execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static class SendDraftMailTask extends AsyncTask<Void, Void, Message> {

        private Account account;
        private Gmail service;
        private MailDetailObject mailDetailObject;
        private GmailNetworkListener listener;

        public SendDraftMailTask(Account account, Gmail service, MailDetailObject mailDetailObject, GmailNetworkListener listener) {
            this.account = account;
            this.service = service;
            this.mailDetailObject = mailDetailObject;
            this.listener = listener;
        }

        @Override
        protected Message doInBackground(Void... voids) {
            if (listener == null) return null;

            try {
                Message draftMessage = createMessage(mailDetailObject);

                Draft draft = new Draft();
                draft.setId(draftMessage.getId());

                return service.users().drafts().send(account.name, draft).execute();
            } catch (IOException e) {
                e.printStackTrace();
                listener.onFailure(e.toString());
            } catch (MessagingException 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 static Message createMessage(MailDetailObject mailDetailObject) throws MessagingException, IOException {
            if (mailDetailObject.TO.size() == 0 || mailDetailObject.FROM == null) {
                System.out.println("Fields cannot be empty");
                return null;
            }

            Properties props = new Properties();
            Session session = Session.getDefaultInstance(props, null);

            MimeMessage message = new MimeMessage(session);

            message.setSubject(mailDetailObject.SUBJECT);

            if (!mailDetailObject.FROM.EMAIL.equals("")) {
                message.setFrom(new InternetAddress(mailDetailObject.FROM.EMAIL));
            }

            for (AccountObject item : mailDetailObject.TO) {
                if (!item.EMAIL.equals("")) {
                    message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(item.EMAIL));
                }
            }

            if (mailDetailObject.CC.size() > 0) {
                for (AccountObject item : mailDetailObject.CC) {
                    if (!item.EMAIL.equals("")) {
                        message.addRecipient(javax.mail.Message.RecipientType.CC, new InternetAddress(item.EMAIL));
                    }
                }
            }

            if (mailDetailObject.BCC.size() > 0) {
                for (AccountObject item : mailDetailObject.BCC) {
                    if (!item.EMAIL.equals("")) {
                        message.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(item.EMAIL));
                    }
                }
            }

            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) {
                        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) {
                        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 static 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;
        }
    }