通过以下代码发送电子邮件时出现以下错误
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;
}
}
答案 0 :(得分:0)
刚刚提出:
p.setProperty("mail.smtps.host", host);
p.put("mail.smtps.port", 587);
p.put("mail.smtps.auth", "true");
而不是:
p.setProperty("mail.smtp.host", host);
p.put("mail.smtp.port", 587);
p.put("mail.smtp.auth", "true");
答案 1 :(得分:0)
我第一次尝试时遇到同样的问题...这个代码可以在我的本地网络和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();
}
}