-1

I try to send email with Intent. I wrote code witch can send email

send.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent email = new Intent(Intent.ACTION_SEND); 
        email.putExtra(Intent.EXTRA_EMAIL, new String[] { Email }); 
        email.putExtra(Intent.EXTRA_SUBJECT,"subject"); 
        email.putExtra(Intent.EXTRA_TEXT, "text1"); 

        email.setType("message/rfc822"); 

        startActivity(Intent.createChooser(email, "Choose an Email client :"));
    }
});

I can send email, but when I choose email client then I can change subject text and extra text. How I can write code to use would not can change subject and extra texts when user would choose email client?

stealthjong
  • 10,858
  • 13
  • 45
  • 84
user3775061
  • 145
  • 2
  • 11
  • 1
    possible duplicate of [Sending Email in Android using JavaMail API without using the default/built-in app](http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a) – Andrew Schuster Jul 03 '14 at 14:22
  • That's not the same question though, although the solution of using another API to send mail would work. – Stealth Rabbi Jul 03 '14 at 14:26

2 Answers2

1

The only way to avoid the user from changing email text is to issue the mail programmatically, i.e. with no UI. It is usually done in Android by using the JavaMail API.

Send Email via JavaMail

First add lines below to your sender class:

static {
    Security.addProvider(new JSSEProvider()); 
}

JSSEP stands for Java Secure Socket Extension, which is the protocol used for Gmail authentication that we will later on use.

You can grab a working sample of JSSEProvider at: https://android.googlesource.com/platform/libcore/+/jb-mr2-release/luni/src/main/java/org/apache/harmony/xnet/provider/jsse/JSSEProvider.java


Create a sender class :

public class EmailSender extends javax.mail.Authenticator {

 void sendMail(username, password, )  {
     ...
 }

}

We now move to sendMail() method implementation:

Start by setting properties for transmission via Gmail:

Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com"); // <--------- via Gmail
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
        "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");

session = Session.getDefaultInstance(props, this);


And follow by sending the mail:

  MimeMessage message = new MimeMessage(session);
  DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));
  message.setSender(new InternetAddress(sender));
  message.setSubject(subject);
  message.setDataHandler(handler);
  message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));
  Transport.send(message);

ByteArrayDataSource is a standard implementation. You can grab it from http://commons.apache.org/proper/commons-email/apidocs/src-html/org/apache/commons/mail/ByteArrayDataSource.html

You wil also need to add the method below, returning the PasswordAuthentication, to your class:

@Override
protected PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, password);
}

Good luck.

Gilad Haimov
  • 5,767
  • 2
  • 26
  • 30
0

If you really need to lock in the subject and message body, but let the user choose the email recipients (the To list), you'll have to provide them a dialog to enter in email recipient, and then send the email in an automated fashion by using something like Apache Commons EMail

As a user, I'd be pretty wary of having an email sent from my phone where I can't see the subject line and body text. You may want to just use the method you have now wehre they can edit the text, or at least show them what you're sending.

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176