// File Name SendFileEmail.java

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class yahoo_sendmail
{
   public static void main(String [] args)
   {
      if(args.length != 3) {
         System.err.println("usage: sendmail <src_email> <dest_email> <SMTP_host>");
         System.exit(0);
      }
      
      // Recipient's email ID needs to be mentioned.
      String to = args[0];

      // Sender's email ID needs to be mentioned
      String from = args[1];

      // Assuming you are sending email from localhost
      String host = args[2];

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);
      properties.setProperty("mail.host", host);
      properties.setProperty("mail.transport.protocol", "smtp");
      //properties.setProperty("mail.smtp.starttls.enable", "true");
      //properties.setProperty("mail.smtp.auth", "true");

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Create the message part 

         String file = "test.png";

         // Create your new message part
         BodyPart messageBodyPart = new MimeBodyPart();
         String htmlText = "<H1>Hello</H1>" +
         "<img src=\"cid:memememe\">";
         messageBodyPart.setContent(htmlText, "text/html");

         // Create a related multi-part to combine the parts
         MimeMultipart multipart = new MimeMultipart("related");
         multipart.addBodyPart(messageBodyPart);

         // Create part for the image
         messageBodyPart = new MimeBodyPart();

         // Fetch the image and associate to part
         DataSource fds = new FileDataSource(file);
         messageBodyPart.setDataHandler(new DataHandler(fds));
         messageBodyPart.setHeader("Content-ID","<memememe>");

         // Set text message part
         multipart.addBodyPart(messageBodyPart);

         // Send the complete message parts
         message.setContent(multipart);

         // Get password
         //System.out.print("pasword> ");
         //String pass = new BufferedReader(new InputStreamReader(System.in)).readLine();

         // Send message
         Transport.send(message);
         //Session session = Session.getDefaultInstance(properties);
         //Transport trans = session.getTransport("smtp");
         //trans.connect("smtp.live.com", 25, "matthewcoan@hotmail.com", pass);
         System.out.println("Sent message successfully....");
      }
      catch (MessagingException mex) {
         mex.printStackTrace();
      }
      //catch (IOException ioe) {
      //   ioe.printStackTrace(System.err);
      //}
   }
}

