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/

March 12, 2014

your suggestions and feedbacks

Please provide your feed back and suggestions

March 07, 2014

Online Hardware Register - Consumerfed Regional office kozhikode

Online Hardware Register for IT Section Consumerfed

Created using G suites


Created an online hardware register using G suite apps, Link to this spreadsheet is given below. The accessories transferred details has to be inputted in spreadsheet supplies where the consolidation is automatically carried out in invoice sheet. Try it out !

Transfer of accessories details

Automatic report generating


Calculating reports automatically using queries


Link to spreadsheet




Call us @ 82 81 80 80 25/29








http://javabelazy.blogspot.in/

Facebook comments