所以我设置了一个使用javax.mail api在后台发送电子邮件的任务,我在堆栈跟踪中收到错误导致电子邮件无法发送。
Ive使用注释/*............*/
堆栈跟踪。
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ nested exception is:
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ java.net.UnknownHostException: Host is unresolved: smtp.gmail.com
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1389)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at javax.mail.Service.connect(Service.java:310)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at javax.mail.Service.connect(Service.java:169)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at javax.mail.Service.connect(Service.java:118)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at javax.mail.Transport.send0(Transport.java:188)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at javax.mail.Transport.send(Transport.java:118)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.example.adrian.trucktracker.AlarmReceiver$SendMailTask.doInBackground(AlarmReceiver.java:96)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.example.adrian.trucktracker.AlarmReceiver$SendMailTask.doInBackground(AlarmReceiver.java:80)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-22 16:34:47.052 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.lang.Thread.run(Thread.java:864)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ Caused by: java.net.UnknownHostException: Host is unresolved: smtp.gmail.com
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.net.Socket.connect(Socket.java:856)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at java.net.Socket.connect(Socket.java:816)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1359)
08-22 16:34:47.062 20025-20422/com.example.adrian.trucktracker W/System.err﹕ ... 14 more
广播接收器代码:
public class AlarmReceiver extends WakefulBroadcastReceiver {
public AlarmReceiver() {
}
Context cont;
public String DATAOUT() {
String Dataout = "";
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(new
File(cont.getFilesDir() + File.separator + "XXXXXXXXXX.txt")));
String read;
StringBuilder builder = new StringBuilder("");
while ((read = bufferedReader.readLine()) != null) {
builder.append(read);
}
Log.d("Output", builder.toString());
Dataout = builder.toString();
bufferedReader.close();
} catch (Exception e) {
}
return Dataout;
}
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("XXXXXXXXXXXXX@gmail.com", "XXXXXXXXXXXXX1298");
}
});
}
private Message createMessage(String email, String subject, String messageBody, Session session) throws MessagingException, UnsupportedEncodingException {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("XXXXXXXXXXXXXXXXXXX@gmail.com", "Admin"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email, email));
message.setSubject(subject);
message.setText(messageBody);
return message;
}
/*Line 80 (error)*/ private class SendMailTask extends AsyncTask<Message, Void, Void> {
private ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
}
@Override
/*Line 96 (error)*/ protected Void doInBackground(Message... messages) {
try {
Transport.send(messages[0]);
} catch (MessagingException e) {
e.printStackTrace();
}
return null;
}
}
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();
}
}
@Override
public void onReceive(Context context, Intent intent) {
String check = "";
String currentDateTimeString = DateFormat.getDateTimeInstance().format(new Date());
File checker = new File(context.getFilesDir() + File.separator + "LEGION_LOCATION_DATA.txt");
if(checker.exists()) {
sendMail("XXXXXXXXX@XXXXXXXXXXXX.com", "DataTest", DATAOUT());
Log.d("Update", "Update Sent");
check = "Auto Check In Complete At " + currentDateTimeString;
}
else
{
check = "ERROR";
sendMail("XXXXXXXXXXX@XXXXXXXXXXX.com", "DataTest",check);
}
Intent intenti= new Intent(context, Locator.class);
intenti.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
intenti, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.legionnotifyicon)
.setContentTitle("Legion Link")
.setContentText(check);
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}