When I press on Send Button in log show Mail Sent But Actually mail not sent. Please help me my code below. Why Mail not sent?
GMailSender.java>
mail send from background...
you can use javaMail to send mail from background
first of all you need to add these three .jar file...
Download these jar files
(1) Activation.jar: http://www.java2s.com/Code/Jar/a/Downloadactivationjar.htm
(2) Additionnal.jar: http://code.google.com/p/javamail-android/downloads/detail?name=additionnal.jar&can=2&q=
(3) mail.jar: http://www.java2s.com/Code/Jar/m/Downloadmailjar.htm
And then use these below code:
MainActivity.java
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity{
EditText email_to,subjectEdit,messageEdit;
Button mailButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
mailButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sendMail();
}
});
}
private void initialize() {
email_to=(EditText)findViewById(R.id.editText_to);
subjectEdit=(EditText)findViewById(R.id.editText_Subject);
messageEdit=(EditText)findViewById(R.id.editText_Message);
mailButton=(Button)findViewById(R.id.btn_SendMail);
}
protected void sendMail() {
final String username = "Your Gmail Id ";
final String password = "Your Gmail password";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email_to.getText().toString()));
message.setSubject("Sent from MobileApp" + subjectEdit.getText().toString());
message.setText("Message : ,"
+ "\n\n"+ messageEdit.getText().toString());
new SendMailTask().execute(message);
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
private class SendMailTask extends AsyncTask {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this,null, "Sending mail", true, false);
}
@Override
protected String doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
return "Success";
}
catch(SendFailedException ee)
{
if(progressDialog.isShowing())
progressDialog.dismiss();
return "error1";
}catch (MessagingException e) {
if(progressDialog.isShowing())
progressDialog.dismiss();
return "error2";
}
}
@Override
protected void onPostExecute(String result) {
if(result.equals("Success"))
{
super.onPostExecute(result);
progressDialog.dismiss();
Toast.makeText(MainActivity.this, "Mail Sent Successfully", Toast.LENGTH_LONG).show();
subjectEdit.setText("");
messageEdit.setText("");
}
else
if(result.equals("error1"))
Toast.makeText(MainActivity.this, "Email Failure", Toast.LENGTH_LONG).show();
else
if(result.equals("error2"))
Toast.makeText(MainActivity.this, "Email Sent problem2", Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml
AndroidManifest.xml
Add this permission only...