gpt4 book ai didi

Javax.mail.AuthenticationFailedException

转载 作者:行者123 更新时间:2023-12-01 11:39:05 25 4
gpt4 key购买 nike

我正在尝试使用 Gmail smtp 服务器从我的应用发送电子邮件。为此,我使用 javax 库。

MailSender.java

public class MailSender {

final String emailPort = "587";// gmail's smtp port for tls
final String smtpAuth = "true";
final String starttls = "true";
final String emailHost = "smtp.gmail.com";

String fromEmail;
String fromPassword;
String toEmail;
String emailSubject;
String emailBody;

Properties emailProperties;
Session mailSession;
MimeMessage emailMessage;

public MailSender() {

}

public MailSender(String fromEmail, String fromPassword, String toEmail, String emailSubject, String emailBody) {
this.fromEmail = fromEmail;
this.fromPassword = fromPassword;
this.toEmail = toEmail;
this.emailSubject = emailSubject;
this.emailBody = emailBody;

emailProperties = System.getProperties();
emailProperties.put("mail.smtp.port", emailPort);
emailProperties.put("mail.smtp.auth", smtpAuth);
emailProperties.put("mail.smtp.starttls.enable", starttls);

Log.i("MAIL_SENDER", "Mail server properties set.");
}

public MimeMessage createEmailMessage() throws AddressException, MessagingException, UnsupportedEncodingException {

mailSession = Session.getInstance(emailProperties, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(fromEmail, fromPassword);
}
});
emailMessage = new MimeMessage(mailSession);

emailMessage.setFrom(new InternetAddress(fromEmail, fromEmail));
emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmail));
emailMessage.setSubject(emailSubject);
//emailMessage.setContent(emailBody, "text/html");// for a html email
emailMessage.setText(emailBody);// for a text email

Log.i("MAIL_SENDER", "Email Message created.");

return emailMessage;
}

public void sendMail() throws AddressException, MessagingException {

Transport transport = mailSession.getTransport("smtp");
transport.connect(emailHost, fromEmail, fromPassword);
transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
transport.close();

Log.i("MAIL_SENDER", "Email sent successfully.");
}

}

这是我从 Activity/服务发送电子邮件的方式:

private void sendEmail(String email) throws Exception{
String from = "myemail@gmail.com";
String pass = "mypass";
String to = email;
String subject = getString(R.string.email_subject);
String body = location_link;

sendMailTask task = new sendMailTask();
task.execute(from, pass, to, subject, body);
}

private class sendMailTask extends AsyncTask<Object, Object, Object> {

@Override
protected Object doInBackground(Object... args) {
try {
MailSender mail = new MailSender(args[0].toString(),
args[1].toString(), args[2].toString(), args[3].toString(),
args[4].toString());
mail.createEmailMessage();
mail.sendMail();
}catch (Exception e) {
Log.e("EMAIL", e.getMessage(), e);
}
return null;
}
}

但是每次调用 conect() 方法时,我都会在 LogCat 中收到此错误:

04-17 12:18:05.996: E/EMAIL(4160): javax.mail.AuthenticationFailedException
04-17 12:18:05.996: E/EMAIL(4160): at javax.mail.Service.connect(Service.java:319)
04-17 12:18:05.996: E/EMAIL(4160): at javax.mail.Service.connect(Service.java:169)
04-17 12:18:05.996: E/EMAIL(4160): at com.myapp.mail.MailSender.sendMail(MailSender.java:82)
04-17 12:18:05.996: E/EMAIL(4160): at com.myapp.TrackingService$sendMailTask.doInBackground(TrackingService.java:380)
04-17 12:18:05.996: E/EMAIL(4160): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-17 12:18:05.996: E/EMAIL(4160): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-17 12:18:05.996: E/EMAIL(4160): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-17 12:18:05.996: E/EMAIL(4160): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-17 12:18:05.996: E/EMAIL(4160): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-17 12:18:05.996: E/EMAIL(4160): at java.lang.Thread.run(Thread.java:841)

与认证有关。事实上,我的帐户中收到了一封来自 google 的电子邮件,告知有人试图访问我的帐户...

最佳答案

下面的代码对我来说工作得很好。为此,您需要从 http://code.google.com/p/javamail-android/downloads/list 下载库来自 https://commons.apache.org/proper/commons-email/download_email.cgi 的链接和公共(public)电子邮件关联。删除所有其他 java 邮件库,仅添加这四个库;

import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

private class MailClass extends AsyncTask<String, Void, Void>{

@Override
protected Void doInBackground(String... params) {
try {
Email email = new SimpleEmail();
email.setHostName("smtp.googlemail.com");
email.setSmtpPort(465);
email.setAuthenticator(new DefaultAuthenticator("abc@gmail.com", "abc_password"));
email.setSSLOnConnect(true);
email.setFrom("abc@gmail.com");
email.setSubject("TestMail");
email.setMsg("This is a test mail ... :-)");
email.addTo("dhavalpatel.244@gmail.com");
email.send();
} catch (EmailException e) {
e.printStackTrace();
}

return null;
}

}

关于Javax.mail.AuthenticationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29696791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com