gpt4 book ai didi

java - 如何解决 javax.mail.AuthenticationFailedException

转载 作者:行者123 更新时间:2023-12-01 10:29:07 43 4
gpt4 key购买 nike

我已经看过这个问题的所有答案。但我的邮件没有发送。实际上,当我使谷歌安全性降低时它会发送。但对我来说这不是一个解决方案!因为它使 Gmail 帐户不 protected ,所以使用该应用程序的人也不想确保他们的邮件安全。所以我尝试了所有建议的变体并阅读了 Java Mail FAQ。这是代码。如果有真正的解决方案,请帮助我。但请不要建议更改谷歌安全!我需要不需要应用用户执行任何操作的解决方案。

public class Mail  {

public Mail() {
_host = "smtp.gmail.com"; // default smtp server
_port = "587"; // default smtp port587

_user = "myname@gmail.com"; // username
_pass = "password"; // password
_from = "myname@gmail.com"; // email sent from
_to = {"yourname@gmail.com"};
_subject = "go"; // email subject
_body = "mail"; // email body

_debuggable = true; // debug mode on or off - default off
_auth = true; // smtp authentication - default on

_multipart = new MimeMultipart();

// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added.
MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap();
mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html");
mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml");
mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain");
mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed");
mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822");
CommandMap.setDefaultCommandMap(mc);
}

public Mail(String user, String pass) {
this();

_user = user;
_pass = pass;
}

public boolean send() throws Exception {
Properties props = _setProperties();

if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) {

Session session = Session.getInstance(props, new GMailAuthenticator(_user,_pass));

MimeMessage msg = new MimeMessage(session);

msg.setFrom(new InternetAddress(_from));

InternetAddress[] addressTo = new InternetAddress[_to.length];
for (int i = 0; i < _to.length; i++) {
addressTo[i] = new InternetAddress(_to[i]);
}
msg.setRecipients(MimeMessage.RecipientType.TO, addressTo);
msg.setSubject(_subject);
msg.setSentDate(new Date());

// setup message body
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(_body);
_multipart.addBodyPart(messageBodyPart);

// Put parts in message
msg.setContent(_multipart);

// send email

Transport t = session.getTransport("smtp");
try {
t.connect(_host, _user, _pass);
t.sendMessage(msg, msg.getAllRecipients());

} finally {
t.close();
}


return true;
} else {
return false;
}
}

private Properties _setProperties() {
Properties props = new Properties();

props.put("mail.smtp.host", _host);

if(_debuggable) {
props.put("mail.debug", "true");
}

if(_auth) {
props.put("mail.smtp.auth", "true");
}

props.put("mail.smtp.port", _port);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.debug", "true");

return props;
}


// class for secure access gmail

class GMailAuthenticator extends Authenticator {
String user;
String pw;
public GMailAuthenticator (String username, String password)
{
super();
this.user = username;
this.pw = password;
}

public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, pw);
}
}

以及我在 Activity 中调用的方法 send()

final Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub

new SendClass().execute();

}
});

private class SendClass extends AsyncTask{

@Override
protected Object doInBackground(Object[] objects) {
Mail m = new Mail();

m.send();
return null;
}
}

最佳答案

我假设您正在尝试使用 OAuth2,尽管您的代码中没有任何内容表明这一点。这是避免将您的帐户配置为“安全性较低的应用程序”的唯一方法。使用OAuth2要复杂得多,this JavaMail web page将帮助您开始。

关于java - 如何解决 javax.mail.AuthenticationFailedException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35189105/

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