
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.internet.*;
import javax.mail.*;

import java.util.*;

/**
 * Classe permettant d'envoyer un mail.
 */


//javaee.jar and mail.jar
// https://java.net/projects/javamail/pages/Home#Download_JavaMail_1.5.2_Release

/*
public class TestMail {
  private final static String MAILER_VERSION = "Java";
  public static boolean envoyerMailSMTP(String serveur, boolean debug) {
    boolean result = false;
    try {
      Properties prop = System.getProperties();
      prop.put("mail.smtp.host", serveur);
      Session session = Session.getDefaultInstance(prop,null);
      Message message = new MimeMessage(session);
      message.setFrom(new InternetAddress("moi@chez-moi.fr"));
      InternetAddress[] internetAddresses = new InternetAddress[1];
      internetAddresses[0] = new InternetAddress("moi@chez-moifr");
      message.setRecipients(Message.RecipientType.TO,internetAddresses);
      message.setSubject("Test");
      message.setText("test mail");
      message.setHeader("X-Mailer", MAILER_VERSION);
      message.setSentDate(new Date());
      session.setDebug(debug);
      Transport.send(message);
      result = true;
    } catch (AddressException e) {
      e.printStackTrace();
    } catch (MessagingException e) {
      e.printStackTrace();
    }
    return result;
  }
   
  public static void main(String[] args) {
    TestMail.envoyerMailSMTP("10.10.50.8",true);
  }
}*/


public class TestMail
{
   public static void main(String [] args)
   {    
        /*
	   	final String username = "delpypatrice@gmail.com";
		final String password = "Un92pac007";

		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");

		Session session = Session.getInstance(props,  new javax.mail.Authenticator() 
		{
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		});

		try {

					Message message = new MimeMessage(session);
					message.setFrom(new InternetAddress("from-email@gmail.com"));
					message.setRecipients(Message.RecipientType.TO,	InternetAddress.parse("patrice.delpy@neuf.fr"));
					message.setSubject("Testing Subject");
					message.setText("Dear Mail Crawler,"+ "\n\n No spam to my email, please!");
					Transport.send(message);

					System.out.println("Done");

		} 
		
		catch (MessagingException e) 
		{
			throw new RuntimeException(e);
		}
		*/
		
		
		
		
		//mail obj = new mail();
		//obj.envoyerMail();
		
		pipo obj2 = new pipo();
		obj2.send();;

	   //send_image obj3 = new send_image();
	   //obj3.envoi_image();


	   //SendAttachmentInEmail id = new SendAttachmentInEmail();
	   //id.attach();
   }
}


class pipo
{
	
	public void send()
	{
	
		   	final String username = "delpypatrice@gmail.com";
			final String password = "Un92pac007";
		
			Properties props = new Properties();
			props.put("mail.smtp.auth","true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.port", "587");
		
			Session session = Session.getInstance(props,  new javax.mail.Authenticator() 
			{
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username, password);
				}
			});
		
			try {
		
						Message message = new MimeMessage(session);
						message.setFrom(new InternetAddress("from-email@gmail.com"));
						message.setRecipients(Message.RecipientType.TO,	InternetAddress.parse("patrice.delpy@neuf.fr"));
						message.setSubject("Debug en cours ");
						message.setText("Patrice faut des tests,"+ "\n\n semble fonctionnel");
						Transport.send(message);
		
						System.out.println("Done");
		
			} 
			
			catch (MessagingException e) 
			{
				throw new RuntimeException(e);
			}
	}

}


/*************************************************************************************/
class mail
{

	 public void envoyerMail()
	 {
	
			 // Recipient
			 String to = "patrice.delpy@neuf.fr";
	
			 // Sender
			 String from = "delpypatrice@gmail.com";
			 final String username = "delpypatrice@gmail.com";
			 final String password = "Un92pac007";
	
			 String host = "smtp.gmail.com";
	
			 Properties props = new Properties();
			 props.put("mail.smtp.auth", "true");
			 props.put("mail.smtp.starttls.enable", "true");
			 props.put("mail.smtp.host", host);
			 props.put("mail.smtp.port", "587");
	
			 // Get the Session object.
			 Session session = Session.getInstance(props, new javax.mail.Authenticator() 
			 {
				 protected PasswordAuthentication getPasswordAuthentication()
				 {
					 return new PasswordAuthentication(username, password);
				 }
			 });
	
			 try 
			 {
			   // Create a default MimeMessage object.
			   Message message = new MimeMessage(session);
			
			   // Set From: header field of the header.
			   message.setFrom(new InternetAddress(from));
			
			   // Set To: header field of the header.
			   message.setRecipients(Message.RecipientType.TO,
			   InternetAddress.parse(to));
			
			   // Set Subject: header field
			   message.setSubject("Testing Subject");
			
			   // Now set the actual message
			   message.setText("Hello, this is sample for to check send " + "email using JavaMailAPI ");
			
			   // Send message
			   Transport.send(message);
			
			   System.out.println("Sent message successfully....");
			
			} 
			 
			 catch (MessagingException e) 
			 {
			      throw new RuntimeException(e);
			 }
	 }	
		
}

/*************************************************************************************/
class send_image
{
	
	public void envoi_image()
	{
		    
		    // Recipient
			 String to = "patrice.delpy@neuf.fr";
	
			 // Sender
			 String from = "delpypatrice@gmail.com";
			 final String username = "delpypatrice@gmail.com";
			 final String password = "Un92pac007";
	
			 String host = "smtp.gmail.com";
		    
		
			 Properties props = new Properties();
			 props.put("mail.smtp.auth", "true");
			 props.put("mail.smtp.starttls.enable", "true");
			 props.put("mail.smtp.host", host);
			 props.put("mail.smtp.port", "25");
		
			 Session session = Session.getInstance(props,
		       new javax.mail.Authenticator() {
		          protected PasswordAuthentication getPasswordAuthentication() {
		             return new PasswordAuthentication(username, password);
		          }
		       });

		    try {
		
		       // Create a default MimeMessage object.
		       Message message = new MimeMessage(session);
		
		       // Set From: header field of the header.
		       message.setFrom(new InternetAddress(from));
		
		       // Set To: header field of the header.
		       message.setRecipients(Message.RecipientType.TO,
		          InternetAddress.parse(to));
		
		       // Set Subject: header field
		       message.setSubject("Image Testing ");
		
		       // This mail has 2 part, the BODY and the embedded image
		       MimeMultipart multipart = new MimeMultipart("related");
		
		       // first part (the html)
		       BodyPart messageBodyPart = new MimeBodyPart();
		       String htmlText = "<H1>Hello</H1><img src=\"cid:image\">";
		       messageBodyPart.setContent(htmlText, "text/html");
		       // add it
		       multipart.addBodyPart(messageBodyPart);
		
		       // second part (the image)
		       messageBodyPart = new MimeBodyPart();
		       DataSource fds = new FileDataSource("d:/recu.jpg");
		
		       messageBodyPart.setDataHandler(new DataHandler(fds));
		       messageBodyPart.setHeader("Content-ID", "<image>");
		
		       // add image to the multipart
		       multipart.addBodyPart(messageBodyPart);
		
		       // put everything together
		       message.setContent(multipart);
		       // Send message
		       Transport.send(message);
		
		       System.out.println("Sent message successfully....");
		
		    } catch (MessagingException e) {
		       throw new RuntimeException(e);
		    }
    
	}
}

/*************************************************************************************/


class SendAttachmentInEmail 
{
	   public void attach() 
	   {
		   
		// Recipient
			 String to = "patrice.delpy@neuf.fr";
	
			 // Sender
			 String from = "delpypatrice@gmail.com";
			 final String username = "delpypatrice@gmail.com";
			 final String password = "Un92pac007";
	
			 String host = "smtp.gmail.com";
		   
	     

	     
	      Properties props = new Properties();
	      props.put("mail.smtp.auth", "true");
	      props.put("mail.smtp.starttls.enable", "true");
	      props.put("mail.smtp.host", host);
	      props.put("mail.smtp.port", "25");

	      // Get the Session object.
	      Session session = Session.getInstance(props,
	         new javax.mail.Authenticator() {
	            protected PasswordAuthentication getPasswordAuthentication() {
	               return new PasswordAuthentication(username, password);
	            }
	         });

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

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

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

	         // Set Subject: header field
	         message.setSubject("Testing Subject");

	         // Create the message part
	         BodyPart messageBodyPart = new MimeBodyPart();

	         // Now set the actual message
	         messageBodyPart.setText("This is message body");

	         // Create a multipar message
	         Multipart multipart = new MimeMultipart();

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

	         // Part two is attachment
	         messageBodyPart = new MimeBodyPart();
	         String filename = "d:\\store.txt";
	         DataSource source = new FileDataSource(filename);
	         messageBodyPart.setDataHandler(new DataHandler(source));
	         messageBodyPart.setFileName(filename);
	         multipart.addBodyPart(messageBodyPart);

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

	         // Send message
	         Transport.send(message);

	         System.out.println("Sent message successfully....");
	  
	      } catch (MessagingException e) {
	         throw new RuntimeException(e);
	      }
	   }
	}
