Possible Duplicate:
Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)
I am new to Android Coding. My Requirement is I want to send Email using Android Code.
Please guide me regarding this.
Possible Duplicate:
Sending Email in Android using JavaMail API without using the default android app(Builtin Email application)
I am new to Android Coding. My Requirement is I want to send Email using Android Code.
Please guide me regarding this.
Here is solution ::
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("text/plain");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{"recipient@example.com"});
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_TEXT , "body of email");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MyActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
You could write a server side script using .Net, Java or PHP. Make a web request (asynchronously) to domain.com/sendemail.php with the arguments of:
The server side script can then handle this all for you and gives you more flexibility on the layout of the emails without having to perform app updates if something should need to change. this also means that the email can come from the app rather than the individual user keeping them anonymous (which may or may not be useful as you have not said).
To do all this look at the Painless Threading article by Android then look at how to make web requests.
Option B:
Use an Intent like so:
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
String[] recipients = new String[]{"my@email.com", "",};
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Test");
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email's message");
emailIntent.setType("text/plain");
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Reference: http://thedevelopersinfo.wordpress.com/2009/10/22/email-sending-in-android/
You can send email directly from code:
String to = "abcd@gmail.com";
String from = "web@gmail.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", SMPT_HOSTNAME);
Session session = Session.getInstance(properties, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(USERNAME, PASSWORD);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject("This is the Subject Line!");
message.setText("This is actual message");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}