尝试发送电子邮件时出现错误。早期它运行良好,但是我已经有一段时间没有使用它了。我不知道是什么原因造成的。我对该类进行了很多更改,但我什至没有涉及此功能,而且我不确定代码是否有问题,代码没有更改,或者电子邮件功能有所更改。 这是代码:
class PhotoSend extends AsyncTask <Void, Void, Void>{
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... voids) {
final String username = "user@gmail.com";
final String password = "password";
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username,password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("user@gmail.com"));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("anotheruser@gmail.com"));
message.setSubject("Zamówiono części do zlecenia " + taskID);
message.setText("Do następującego zamówienia " + taskID + " zostały zamówione części w liczbie " + partsCount + "\n\n" + "Opis:\n" + partPicturesDescription);
Transport.send(message);
Log.d(TAG, "onInput: background");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Toast.makeText(getContext(), "Wysłano", Toast.LENGTH_SHORT).show();
Log.d(TAG, "onInput: postExecute");
}
}
和错误:
FATAL EXCEPTION: AsyncTask #6
Process: com.example.damianadamski.Service, PID: 6744
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:353)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
at java.util.concurrent.FutureTask.run(FutureTask.java:271)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: java.lang.RuntimeException: javax.mail.AuthenticationFailedException
at com.example.damianadamski.service.Fragments.TaskFragment$5$1PhotoSend.doInBackground(TaskFragment.java:541)
at com.example.damianadamski.service.Fragments.TaskFragment$5$1PhotoSend.doInBackground(TaskFragment.java:498)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
Caused by: javax.mail.AuthenticationFailedException
at javax.mail.Service.connect(Service.java:319)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at com.example.damianadamski.service.Fragments.TaskFragment$5$1PhotoSend.doInBackground(TaskFragment.java:536)
at com.example.damianadamski.service.Fragments.TaskFragment$5$1PhotoSend.doInBackground(TaskFragment.java:498)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
答案 0 :(得分:2)
就我而言,我在项目中所做的就是这样:
public void onClick(View v) {
final GMailSender sender = new GMailSender("username@gmail.com", "password");
new AsyncTask<Void, Void, Void>() {
@Override public Void doInBackground(Void... arg) {
try {
sender.sendMail("This is Subject",
"This is Body",
"user@gmail.com",
"user@yahoo.com");
} catch (Exception e) {
Log.e("SendMail", e.getMessage(), e);
}
}
}.execute();
}
答案 1 :(得分:0)
登录到您的gmail并打开它应该可以使用的安全性 访问https://www.google.com/settings/security/lesssecureapps