gpt4 book ai didi

java - 当我发送邮件然后发现错误 javax.mail.AuthenticationFailedException : 535 Incorrect authentication data

转载 作者:行者123 更新时间:2023-12-02 04:48:00 24 4
gpt4 key购买 nike

通过以下代码发送电子邮件时出现以下错误
javax.mail.AuthenticationFailedException: 535 Incorrect authentication data
我的代码中可能有什么问题。

public class SendMail {

public static boolean sendHTMLMail(final String from, final String password, String senderName, String sub, String msg, String[] to) {

String host = "mail.xxxx.org";
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart bodypart = new MimeBodyPart();
Properties p = new Properties();
p.setProperty("mail.smtp.host", host);
p.put("mail.smtp.port", 587);
p.put("mail.smtp.auth", "true");

try {
Session session = Session.getInstance(p, new javax.mail.Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
});
Transport transport = session.getTransport("smtp");
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("" + senderName + "<" + from + ">"));
InternetAddress[] toAddress = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
toAddress[i] = new InternetAddress(to[i]);
}

for (InternetAddress toAddres : toAddress) {
mimeMessage.addRecipient(Message.RecipientType.TO, toAddres);
}
bodypart.setContent(msg, "text/html; charset=\"utf-8\"");
multipart.addBodyPart(bodypart);
mimeMessage.setSubject(sub);
mimeMessage.setContent(multipart);
transport.connect(host, from, password);
mimeMessage.saveChanges();
Transport.send(mimeMessage);
transport.close();
return true;
} catch (MessagingException me) {
me.printStackTrace();
}
return false;
}
}

最佳答案

我在第一次尝试时遇到了同样的问题......这个代码可以在我的本地网络和 Gmail 中发送......你试试

package SendMail;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
* @author akash073@gmail.com
*
*/

public class CrunchifyJavaMailExample {

//static Properties mailServerProperties;
// static Session getMailSession;
// static MimeMessage generateMailMessage;

public static void main(String args[]) throws AddressException, MessagingException {
generateAndSendEmail();
System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
}

public static void generateAndSendEmail() throws AddressException, MessagingException {

String smtpHost="put Your Host";
String smtpUser="UserName in full @somthing.com";
String smtpPassword="your password";
int smtpPort=25;//Port may vary.Check yours smtp port
// Step1
System.out.println("\n 1st ===> setup Mail Server Properties..");
Properties mailServerProperties = System.getProperties();
//mailServerProperties.put("mail.smtp.ssl.trust", smtpHost);
// mailServerProperties.put("mail.smtp.starttls.enable", true); // added this line
mailServerProperties.put("mail.smtp.host", smtpHost);
mailServerProperties.put("mail.smtp.user", smtpUser);
mailServerProperties.put("mail.smtp.password", smtpPassword);
mailServerProperties.put("mail.smtp.port", smtpPort);

mailServerProperties.put("mail.smtp.starttls.enable", "true");
System.out.println("Mail Server Properties have been setup successfully..");

// Step2
System.out.println("\n\n 2nd ===> get Mail Session..");
Session getMailSession = Session.getDefaultInstance(mailServerProperties, null);
MimeMessage generateMailMessage = new MimeMessage(getMailSession);
generateMailMessage.setFrom (new InternetAddress (smtpUser));
generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("akash073@waltonbd.com"));
generateMailMessage.addRecipient(Message.RecipientType.CC, new InternetAddress("akash073@gmail.com"));
generateMailMessage.setSubject("Greetings from Crunchify..");
String emailBody = "2.Test email by Crunchify.com JavaMail API example. " + "<br><br> Regards, <br>Crunchify Admin";
generateMailMessage.setContent(emailBody, "text/html");
System.out.println("Mail Session has been created successfully..");

// Step3
System.out.println("\n\n 3rd ===> Get Session and Send mail");
Transport transport = getMailSession.getTransport("smtp");

// Enter your correct gmail UserID and Password
// if you have 2FA enabled then provide App Specific Password
transport.connect(smtpHost,smtpPort, smtpUser, smtpPassword);
transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
transport.close();
}
}

关于java - 当我发送邮件然后发现错误 javax.mail.AuthenticationFailedException : 535 Incorrect authentication data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31332536/

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