Showing posts with label core java : email validation example in java.. Show all posts
Showing posts with label core java : email validation example in java.. Show all posts

March 16, 2014

Java Program that sent Email with attachment


Send an Email with attachment in java window application (gmail smtp : Simple Mail Transfer Protocol)


You can also sent reminder email

import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;

public class SendMailSSL {
    public static void main(String[] args) {
        String msg = " Mail sent successfully (java mail service application)";
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class",
                "javax.net.ssl.SSLSocketFactory");
        props.put("mail.debug", "true");  // Debug the program
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("senderMailId@gmail.com","senderPassword");
                }
            });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("cfedprice@gmail.com"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("receiverMailId@yahoo.in"));
            message.setSubject(" Details about Syrian Revolution (Civil War) ");
            // create the message part
            MimeBodyPart messageBodyPart = new MimeBodyPart();
            //message body
            messageBodyPart.setText("Hi,  This application send email with attachment to Receiver mail id ");
            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(messageBodyPart);
            //attachment
            messageBodyPart = new MimeBodyPart();
            DataSource source = new FileDataSource("D:\\FaceBook\\SyrianRevolution.txt");
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName("SyrianRevolution.txt");
            multipart.addBodyPart(messageBodyPart);
            message.setContent(multipart);
            //message.setFileName("hackThroughEmail.java");

            Transport.send(message);

            //System.out.println("Email sent successfully using this java application");
        } catch (MessagingException e) {
            msg = "Error while sending";
            throw new RuntimeException(e);
        }
        JOptionPane.showMessageDialog(null, msg);
    }
}

This example in java shows how to mail using java mail service to sent a mail. User need to create a gmail account). Automatic mail sending application using google spreadsheet (google script)

Image for success mail sent
Java SMTP Mail Sent Application


if you are getting an error : authentication failed, or gmail blocked login attempt, then you should enable the access for less secure apps in your account

 Some devices and apps use insecure sign-in technology to access your data.
Choosing Disable prevents these less secure devices and apps from accessing your Google Account.
Choosing Enable increases your chances of unauthorized account access but allows you to continue using these less secure devices and apps. Learn more

Access for less secure apps should be enabled


You can download the working jar from here download link
filename : javabelazy10203.rar
password for rar file extraction is : 12345ABCDE


Output

Email Template




http://javabelazy.blogspot.in/

October 07, 2013

How to redirect to second page from first page through servlet

Servlet Mapping example in deployment descriptor


This java server page (jsp) example shows how to redirect from a page another page in jsp, the request from jsp page will pass through a servlet and from the servlet to second . the following java code will decide which page the request should be directed : page.response.sendRedirect("secondPage.jsp");

web.xml file



<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>Artificial intelligence</display-name>
  <welcome-file-list>
    <welcome-file>newPage.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description>An example for servlet mapping in jsp servlet</description>
    <display-name>Beyonce superbowl</display-name>
    <servlet-name>Beyonce</servlet-name>
    <servlet-class>com.NeuralNetwork.servlet.EvasiOn</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Beyonce</servlet-name>
    <url-pattern>/NeuralNetwork.jsp</url-pattern>
  </servlet-mapping>
</web-app>



November 03, 2012

Exceptions in java

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mailService' defined in ServletContext resource [/WEB-INF/landview-mail.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'mailSender' of bean class [com.mapview.landview.service.impl.MailServiceImpl]: Bean property 'mailSender' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?



solution : you have forget to create getter and setter method in java class for id specified in xml as bean.



http://javabelazy.blogspot.in/

Facebook comments