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,....