gpt4 book ai didi

java - 我正在努力在远程服务器中发送电子邮件,但它在本地工作,在远程服务器中抛出 'AuthenticationFailedException'

转载 作者:行者123 更新时间:2023-12-01 18:12:14 28 4
gpt4 key购买 nike

这是我的配置文件。同时,我还在接收方电子邮件中启用了“不太安全的应用程序是真实的”。我正在使用 587 smpt.gmail.com 端口,因为我仅使用 HTTP,而不是 HTTPS。在本地服务器中,即使接收者邮件也有两步验证,也很高兴邮件正在发送,但来到远程服务器时,我收到了 AuthenticationFailedException 。我也尝试了很多代码更改,但仍然无法重新解决这个问题。我不知道问题出在哪里。

Config.xml:

<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"/>
<property name="port" value="587"/>
<property name="username" value="abc@gmail.com"/>
<property name="password" value="xxxxxx"/>
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>

<bean id="emailSession" class="javax.mail.Session" factory-method="getInstance">
<constructor-arg index="0">
<props>
<prop key="mail.smtp.user">abc@gmail.com</prop>
<prop key="mail.smtp.host">smtp.gmail.com</prop>
<prop key="mail.smtp.port">587</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.debug">true</prop>
<prop key="mail.transport.protocol">smtp</prop>
</props>
</constructor-arg>
<constructor-arg index="1">
<bean id="emailAuthenticator" class="com.user.service.EmailSMTPAuthenticator">
<property name="username" value="abc@gmail.com"></property>
<property name="password" value="xxxxxx"></property>
</bean>
</constructor-arg>
</bean>
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com"></property>
<property name="session" ref="emailSession"></property>
</bean>

电子邮件服务:

import javax.mail.Message;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;

public void sendEmail(String from,String to,String subject,String message){
try {
MimeMessage mimeMessage=mailSender.createMimeMessage();
mimeMessage.addRecipients(Message.RecipientType.TO, to);
mimeMessage.setSubject(subject);
mimeMessage.setContent(message, "text/html");
mailSender.send(mimeMessage);
}
catch(Exception e) {
e.printStackTrace();
}

}

}

最佳答案

授权码不是密码

<bean id="emailAuthenticator" class="com.user.service.EmailSMTPAuthenticator">
<property name="password" value="{Authorization code}"></property>
</bean>

关于java - 我正在努力在远程服务器中发送电子邮件,但它在本地工作,在远程服务器中抛出 'AuthenticationFailedException',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60447480/

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