-2

Email Sending with Hotmail by using java. I got 554.5.2.252 error.

I got Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.252 ... error while sending emails from my hotmail account.

package utilities;

import java.io.IOException;
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 = "sansalnuray@hotmail.com";  
        String fromUserPassword = "***";  
        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@gmail.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");
        newSession = Session.getDefaultInstance(properties,null);
    }
    
}   

My full error mesagge.

Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 554 5.2.252 SendAsDenied; sansalnuray@hotmail.com not allowed to send as Sansal@SansalPC.mshome.net; STOREDRV.Submission.Exception:SendAsDeniedException.MapiExceptionSendAsDenied; Failed to process message due to a permanent exception with message [BeginDiagnosticData]Cannot submit message. 0.35250:1F00BE88, 1.36674:0A000000, 1.61250:00000000, 1.45378:02000000, 1.44866:0D020000,....

Sansal
  • 1
  • 1

1 Answers1

0

It looks like the email address you were trying to use, is not allowed to send an email under the name Sansal@SansalPC.mshome.net, judging by the error message you gave. This means that a "Send As" restriction is in place and prevents you from sending emails from this email address using a different address.

You must ensure that the email address you're trying to send from is one that is authorized to send emails.

  • 1
    Thank you for your comment. I don't have any Sansal@SansalPC.mshome.net email addresses. I also log in to my laptop with my Hotmail credential. – Sansal Dec 25 '22 at 03:06
  • You must ensure that the email address you're trying to send from is one that is authorized to send emails. Do you mean sansalnuray@hotmail.com here? – Sansal Dec 25 '22 at 03:07
  • Do you know any solution for this? – Sansal Dec 27 '22 at 22:21