I have a textfield on my app, and a button. I only want that when user press the button, my app have to send a email with the text \"Hello\" to the direction on the textfiel
public class MainActivity extends Activity
{
private static final String username = "emailaddress";
private static final String password = "password";
private EditText emailEdit;
private EditText subjectEdit;
private EditText messageEdit;
private Multipart _multipart;
@SuppressLint("SdCardPath")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emailEdit = (EditText) findViewById(R.id.email);
subjectEdit = (EditText) findViewById(R.id.subject);
messageEdit = (EditText) findViewById(R.id.message);
Button sendButton = (Button) findViewById(R.id.send);
sendButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String email = emailEdit.getText().toString();
String subject = subjectEdit.getText().toString();
String message = messageEdit.getText().toString();
sendMail(email, subject, message);
}
});
}
private void sendMail(String email, String subject, String messageBody)
{
Session session = createSessionObject();
try {
Message message = createMessage(email, subject, messageBody, session);
new SendMailTask().execute(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public void addAttachment(String filename) throws Exception {
BodyPart messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
_multipart.addBodyPart(messageBodyPart);
}
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("tutorials@tiemenschut.com", "Tiemen Schut"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
message.setSubject(subject);
message.setText(messageBody);
return message;
}
private Session createSessionObject() {
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
return Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
private class SendMailTask extends AsyncTask {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this, "Please wait", "Sending mail", true, false);
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
progressDialog.dismiss();
}
@Override
protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
}
add three jar files in your libs folder and try this Mail.jar!
activation.jar!
additional.jar!
Write the subject or body text directly and remove edittext and you will send directly email from your app.
And don't forget to give INTERNET PERMISSION In your manifest