Hi, my code is shown below. I use my personal laptop. I try to send emails from my java code by using Hotmail account, SMTP. I got this error message. I use my laptop as an admin account and I never have Sansal@SansalPC.mshome.net account. Please help me?
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
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
{
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@yahoo.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");
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
newSession = Session.getDefaultInstance(properties,null);
}
}