535 5.7.3 Authentication unsuccessful Error

Sansal Nuray 1 Reputation point
2022-12-29T21:42:16.583+00:00

Hi,

My code is shown below. I always get 535 5.7.3 Authentication unsuccessful error. Anyone can help with this issue?

My Code:

package utilities;

import java.io.IOException;
import java.net.Authenticator;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SecureEmail
{

//SETUP MAIL SERVER PROPERTIES  
//DRAFT AN EMAIL  
//SEND EMAIL  
	  
Session newSession = null;  
MimeMessage mimeMessage = null;  
public static void main(String args[]) throws AddressException, MessagingException, IOException  
{  
	SecureEmail mail = new SecureEmail();  
	mail.setupServerProperties();  
	mail.draftEmail();  
	mail.sendEmail();  
}  

private void sendEmail() throws MessagingException {  
	String fromUser = "knuray@msystemsintl.com";  
	String fromUserPassword = "zpZ8Q~OfXd0Lub**************vVlnXqiYblu";    
	String emailHost = "smtp.office365.com";  
	Transport transport = newSession.getTransport("smtp");  
	transport.connect(emailHost, fromUser, fromUserPassword);  
	transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());  
	transport.close();  
	System.out.println("Email successfully sent!!!");  
}  

private MimeMessage draftEmail() throws AddressException, MessagingException, IOException {  
	String[] emailReceipients = {"sansalnuray@hotmail.com"};    
	String emailSubject = "Test Mail";  
	String emailBody = "Test Body of my email";  
	mimeMessage = new MimeMessage(newSession);  
	  
	for (int i =0 ;i<emailReceipients.length;i++)  
	{  
		mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(emailReceipients[i]));  
	}  
	mimeMessage.setSubject(emailSubject);  
     

      
	 MimeBodyPart bodyPart = new MimeBodyPart();  
	 bodyPart.setContent(emailBody,"html/text");  
	 MimeMultipart multiPart = new MimeMultipart();  
	 multiPart.addBodyPart(bodyPart);  
	 mimeMessage.setContent(multiPart);  
	 return mimeMessage;  
}  
  
  
private void setupServerProperties() {  
	Properties properties = System.getProperties();  
	properties.put("mail.smtp.port", "587");  
	properties.put("mail.smtp.auth", "true");  
	properties.put("mail.smtp.starttls.enable", "true");  
	properties.put("mail.smtp.starttls.required", "true");  
	properties.put("mail.smtp.auth.mechanisms", "XOAUTH2");  
	properties.put("mail.debug.auth", "true");  
	properties.put("mail.debug", "true");  
	Session session = Session.getInstance(properties);  
	newSession = Session.getDefaultInstance(properties,null);  
  

}  
   

}

Exchange Server Development
Exchange Server Development
Exchange Server: A family of Microsoft client/server messaging and collaboration software.Development: The process of researching, productizing, and refining new or existing technologies.
508 questions
{count} votes