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"%> <% if(request.getParameter("submit")!=null) { String name,email,phone,query1; name=request.getParameter("name"); email=request.getParameter("email"); phone=request.getParameter("phone"); query1=request.getParameter("message"); Properties props = new Properties(); props.put("mail.smtp.host", "smtp.office365.com"); props.put("mail.smtp.socketFactory.port", "587"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 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); Transport.send(message); 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
You must be logged in to post a comment.