-1

I have a service, and in that Service I have been sending some data through mail. For that I am using the Gmail account. To send the mail. I have searched a lot but not able to get the right answer.

I can not send mail through Intent as I do not want to show it to user. All I want is that to send mail in the service with out notifying the user.

For this I have followed this tutorial but It is not ending the mail. It just shows toast "Mail Sent" but in fact It does not send message I have checked the inbox and sent items as well but there is nothing

I have not get any exception. But still it is not working .

Please help . If you know any working solutions.

Will
  • 810
  • 6
  • 21
Allay Khalil
  • 674
  • 3
  • 11
  • 31
  • Check-out this tutorial:- http://www.jondev.net/articles/Sending_Emails_without_User_Intervention_(no_Intents)_in_Android – Adarsh Yadav Jan 26 '16 at 13:09
  • 2
    You need to show the code that is not working. Also, if I ever catch an app sending an email from my account without clear disclosure, I'll uninstall and report it immediately. – 2Dee Jan 26 '16 at 13:09
  • thanks @2Dee for your valuable information , which I do not know . – Allay Khalil Jan 26 '16 at 13:10
  • 1
    @AllayKhalil ;- Check-out this one too http://stackoverflow.com/a/2033124/1384010 – Adarsh Yadav Jan 26 '16 at 13:11
  • @adrash yadav but when I changed my google setting to secure , then this code is unable to send the mail – Allay Khalil Jan 26 '16 at 13:28

1 Answers1

1

Yes. You can, now, send email programatically. You need to use Gmail API

According to Gmail API,

To create message:

/**
   * 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 {
    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;
  }

To send 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());
  }

Check the link: Sending Email

Chintan Soni
  • 24,761
  • 25
  • 106
  • 174