メール クライアント オプションを使用せずにメールを送信したい。私は独自のカスタム メール ビューを持っています。ここからアイデアをダウンロードし ます。そしてそれはOSネットワーク例外を与えています。そのため、asynctask を使用して実行しますが、Logcat でエラーが発生します
EmailSend.java:
public class EmailSend extends Activity{
Mail m ;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_main);
Button addImage = (Button) findViewById(R.id.send_email);
addImage.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
new ListViewContactsLoader().execute();
}
});
}
/** An AsyncTask class to retrieve and load listview with contacts */
private class ListViewContactsLoader extends AsyncTask<Void, Void, Boolean>{
public ListViewContactsLoader() {
}
@Override
protected Boolean doInBackground(Void... params) {
try {
// m.addAttachment("/sdcard/filelocation");
if(m.send()) {
Toast.makeText(EmailSend .this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
return true;
} else {
Toast.makeText(EmailSend .this, "Email was not sent.", Toast.LENGTH_LONG).show();
return false;
}
} catch(Exception e) {
//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show();
Log.e("MailApp", "Could not send email", e);
return false;
}
}
@Override
protected void onPostExecute(Boolean result) {
// Setting the cursor containing contacts to listview
}
public void onPreExecute() {
Mail m = new Mail("user@gmail.com", "");
String[] toArr = {"bla@bla.com", "lala@lala.com"};
m.setTo(toArr);
m.setFrom("wooo@wooo.com");
m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
m.setBody("Email body.");
}
}
}
マニフェスト ファイル:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
LogCat:
Could not send email
java.lang.NullPointerException
at com.example.emailer.EmailSend$ListViewContactsLoader.doInBackground(EmailSend.java:54)
at com.example.emailer.EmailSend$ListViewContactsLoader.doInBackground(EmailSend.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:252)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1081)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:574)
at java.lang.Thread.run(Thread.java:1020)