0

I'm creating an android application which needs to send e-mail to a specific mail id. I tried using intent (ACTION_SEND).Since it requires user interaction i didn't use this method. what i want is to send the e-mail through back end(without the knowledge of user). Can anyone tell me the best suited way? Thanks in advance...

user456
  • 61
  • 1
  • 7
  • You can use this for sending email from background. http://stackoverflow.com/questions/2020088/sending-email-in-android-using-javamail-api-without-using-the-default-built-in-a – AnujMathur_07 Nov 17 '12 at 11:11

4 Answers4

1

There are a few tutorials on the internet about this, here is one -

jcw
  • 5,132
  • 7
  • 45
  • 54
0
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[]{"test@test.com"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Message body");
emailIntent.setType("application/octet-stream");
startActivity(emailIntent);

Also you can use HTTP POST and write specific method on your server side.

Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
0

You can use Java Mail API for the specific email id. You can fix it.See this link!

Community
  • 1
  • 1
Deepanker Chaudhary
  • 1,694
  • 3
  • 15
  • 35
0

Use this class to send email. create an object of this class with username and password as arguments.

public class MailSender {
private String mailhost = "smtp.gmail.com";
private String user;
private String password;
private Session session;
private Multipart _multipart;
public BodyPart messageBodyPart;
DataSource source;
static {
    Security.addProvider(new com.PackageName.JSSEProvider());
}

public MailSender(String user, String password) {
    this.user = user;
    this.password = password;

    Properties props = new Properties();
    props.setProperty("mail.transport.protocol", "smtp");
    props.setProperty("mail.smtp.host", mailhost);
    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");
    _multipart = new MimeMultipart();
    session = Session.getInstance(props, new MailAuthenticator(user,
            password));

}

public synchronized void sendMail(final String subject, final String uuid,
        final String address, final double latitude,
        final double longitude, final String recipients,
        final String filepath, final Context context, final int i, final int j)
        throws Exception {

    Thread thread = new Thread() {
        public void run() {
            Looper.prepare();
            try {
                System.out.println("SENDING       MAIL");
                Message message = new MimeMessage(session);
                messageBodyPart = new MimeBodyPart();
                DatabaseAdapter db = new DatabaseAdapter(context);
                String eId = db.getYourId();
                message.setFrom(new InternetAddress(eId));
                message.setSubject(subject);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String currentDate = sdf.format(new Date());
                SimpleDateFormat sTf = new SimpleDateFormat("hh:mm:ss");
                String currentTime = sTf.format(new Date());
                System.out.println(i);
                messageBodyPart
                        .setText("Someone tried to unlock your device(ID : "+uuid+" ) at: "
                                + currentTime + " on " + currentDate + "\n"
                                + "Device Location : " + address + "\n"
                                + "Map: " + "http://maps.google.com/?q="
                                + latitude + "," + longitude);
                if (recipients.indexOf(',') > 0)
                    message.setRecipients(Message.RecipientType.TO,
                            InternetAddress.parse(recipients));
                else
                    message.setRecipient(Message.RecipientType.TO,
                            new InternetAddress(recipients));

                Transport transport = session.getTransport("smtp");
                System.out.println("CONECTING.....");
                transport.connect(mailhost, user, password);
                message.saveChanges();
                message.setContent(_multipart);
                _multipart.addBodyPart(messageBodyPart);
                message.setContent(_multipart);
                if (source != null) {

                    message.setFileName("image");
                }
                Transport.send(message);
                System.out.println("Mail  sent ...");

                }

                transport.close();


            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    };
    thread.start();

}

public void addAttachment(String filename) throws Exception {
    if (!filename.equals("")) {
        System.out.println("Ataching     file        :)");
        messageBodyPart = new MimeBodyPart();
        source = new FileDataSource(filename);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(filename);
        _multipart.addBodyPart(messageBodyPart);
        System.out.println("FILE    ATTACHED        :)");
    }
}


class MailAuthenticator extends Authenticator {
    String us;
    String pw;

    public MailAuthenticator(String username, String password) {
        super();
        this.us = username;
        this.pw = password;
    }

    public PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(us, pw);
    }
}

}

Also add this in other file.

public final class JSSEProvider extends Provider {
private static final long serialVersionUID = 1L;

public JSSEProvider() {
    super("HarmonyJSSE", 1.0, "Harmony JSSE Provider");
    AccessController
            .doPrivileged(new java.security.PrivilegedAction<Void>() {
                public Void run() {
                    put("SSLContext.TLS",
                            "org.apache.harmony.xnet.provider.jsse.SSLContextImpl");
                    put("Alg.Alias.SSLContext.TLSv1", "TLS");
                    put("KeyManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.KeyManagerFactoryImpl");
                    put("TrustManagerFactory.X509",
                            "org.apache.harmony.xnet.provider.jsse.TrustManagerFactoryImpl");
                    return null;
                }
            });
}

}

AnujMathur_07
  • 2,586
  • 2
  • 18
  • 25