New User Offer - Use NEW10 Coupon to get extra 10% off Call +91-8283017879

Java Mailing code with Outlook / Office 365

This is the code for sending java mail with an outlook account as Gmail has stopped less secure app support.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page import="java.util.Properties"%>

 String name,email,phone,query1;
 Properties props = new Properties();
 props.put("", "");
 props.put("mail.smtp.socketFactory.port", "587");
 props.put("mail.smtp.auth", "true");
 props.put("mail.smtp.port", "587");
 props.put("mail.smtp.starttls.enable", "true");

 Session session2 = Session.getInstance(props,
 new javax.mail.Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication("username","password");
 try {
 Message message = new MimeMessage(session2);
 message.addHeader("Content-type", "text/html; charset=UTF-8");
 message.setFrom(new InternetAddress(""));
 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(""));
 message.setSubject("Feedback from website");
 message.setText("Name : " + name + "\nEmailid : "+ email + "\nPhone :" + phone + "\nMessage : " + query1);


 out.print("Thanks for contacting us. We have received your message. We will contact you shortly");
 catch(Exception e)
 out.println("Mail does not works due to " + e.getMessage());

We have to download and use java mailing and authentication jar files also as libraries along with this code.

Hope it helps. The best way to learn java is through java projects. Get java projects with source code here

Give your comments below on whether it worked for you or not.

Leave a Reply

We will be happy to hear your thoughts

Leave a reply
Shopping cart