gpt4 book ai didi

javax.mail.AuthenticationFailedException : 535 authentication failed (#5. 7.1)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:17 27 4
gpt4 key购买 nike

我正在使用 javamail api 制作一个自动发送电子邮件的 java 项目。当我使用 smtp.gmail.com 主机发送此邮件时,它有效。但是当我使用我自己的主机服务器 mail.sitename.com 时...它显示异常...我的用户名和密码是正确的。请任何人帮我解决这个问题......异常(exception)是:-

    javax.mail.AuthenticationFailedException: 535 authentication failed (#5.7.1)

at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:826)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:761)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:685)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at com.zenga.servlet.MailNotification.sendMail(MailNotification.java:130)
at com.zenga.servlet.MailNotification.doPost(MailNotification.java:45)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:928)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:539)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:300)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

代码:---

a) 我正在使用 DAO 模式来获取数据以达到(电子邮件 ID)的目的b) 一个 jsp 文件开始并限制获取与我要发送电子邮件的那些连接的无连接..c) Servlet 文件如下...

package com.zenga.servlet;

import java.io.*;
import java.security.GeneralSecurityException;
import java.util.*;
import com.sun.mail.util.MailSSLSocketFactory;
import com.zenga.dao.DaoFactory;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.mail.*;
import javax.mail.internet.*;

/**
* Servlet implementation class send
*/
@WebServlet("/send")
public class MailNotification extends HttpServlet {
private static final long serialVersionUID = 1L;

@SuppressWarnings("unchecked")
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub

HashMap<String, String>list = new HashMap<String, String>();
Set<?> set = null;
Iterator<?> itr = null;
int start = 0;
int limit = 0;
Map.Entry<String, String> me = null;
try {
DaoFactory dao = DaoFactory.getDao();
start = Integer.parseInt(request.getParameter("start"));
limit = Integer.parseInt(request.getParameter("end"));
list = dao.insertLimit(start,limit);
set = list.entrySet();
itr = set.iterator();

while(itr.hasNext()) {
me = (Map.Entry<String, String>)itr.next();
sendMail(me.getKey(),me.getValue(),request,response);
}
} catch (Exception e) {

}

}

private void sendMail(String toUsername, String to,HttpServletRequest request,HttpServletResponse response) throws IOException, GeneralSecurityException {

final String from = "xyz@sitename.com";
final String subject = "a subject";
final String password = "password";
final String host = "mail.sitename.com";
String msg = getMessage(toUsername);

//Set the host SMTP address

/*MailSSLSocketFactory socketFactory= new MailSSLSocketFactory();
socketFactory.setTrustAllHosts(true);

Properties properties=System.getProperties();
properties.put("mail.smtp.host", host);
properties.put("mail.smtp.user", from);
properties.put("mail.smtp.password", password);
//properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.ssl.socketFactory", socketFactory);
properties.put("mail.smtp.EnableSSL.enable","true");


// properties.put("mail.smtp.ssl.enable","false");
properties.put("mail.smtp.ssl.trust",host);
properties.put("mail.protocol.ssl.trust", host);
properties.put("mail.smtp.socketFactory.port", "587");
properties.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
*/

Properties properties=System.getProperties();
properties.put("mail.smtp.host",host);
properties.put("mail.smtp.user",from);
properties.put("mail.smtp.password",password);
properties.put("mail.smtp.port","587");
properties.put("mail.smtp.auth","true");
// properties.put("mail.smtp.starttls.enable","true");
properties.put("mail.smtp.ssl.enable","false");
properties.put("mail.smtp.starttls.enable","false");

Authenticator auth = new Authenticator(){
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
};
Session session=Session.getDefaultInstance(properties,auth);
response.setContentType("text/html");
try{
// create a message

MimeMessage message=new MimeMessage(session);

// set the from and to address
// InternetAddress addressFrom = new InternetAddress(from);
message.setFrom(new InternetAddress(from));
//InternetAddress addressTo = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));

// Setting the Subject and Content Type
message.setSubject(subject);

// create and fill the first message part
MimeBodyPart msgBodypart = new MimeBodyPart();

msgBodypart.setText(msg,"ascii","hmtl");


// create the Multipart and add its parts to it
Multipart multipart=new MimeMultipart();
multipart.addBodyPart(msgBodypart);
// add the Multipart to the message
message.setContent(multipart);
// set the Date: header
message.setSentDate(new Date());

Transport.send(message);
success++;


}
catch(MessagingException me)
{
failed++;
me.printStackTrace();
}
}



/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
}

我也尝试了评论中的所有内容,但没有任何效果。我阅读了许多 stackoverflow 示例,但没有人能正确解决它。

最佳答案

找到解决方案....有时 javax.mail.AuthenticationFailedException: 535 authentication failed (#5.7.1) 发生在主机未使用代码对用户进行身份验证时……因为我在一个程序运行时调用主机将邮件发送给大约 500 个用户。所以 sendMail() 调用了 500 次并导致服务器挂起。因此它阻止了我对编码站点的身份验证,但可以使用浏览器访问。 当我创建一个新帐户时,我找到了解决方案,然后在编码时我创建了一个线程,通过该线程将以 10 秒的间隔调用 sendMail(),然后一次性发送给 100 个用户。主机来自网络解决方案电子邮件登录。 希望我能为某人提供解决此类问题的方法。 :)

关于javax.mail.AuthenticationFailedException : 535 authentication failed (#5. 7.1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17970343/

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