Advertisement
Guest User

Untitled

a guest
Mar 6th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.20 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package com.ericsson.nosc.smtp;
  6.  
  7.  
  8. import com.ericsson.nosc.configuration.AppConfCC;
  9. import com.ericsson.nosc.outage.entities.SmtpSendingQueue;
  10. import java.io.File;
  11. import java.io.FileOutputStream;
  12. import java.io.IOException;
  13. import java.text.SimpleDateFormat;
  14. import java.util.Properties;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17. import javax.activation.DataHandler;
  18. import javax.activation.DataSource;
  19. import javax.activation.FileDataSource;
  20. import javax.mail.Address;
  21. import javax.mail.Authenticator;
  22. import javax.mail.Message;
  23. import javax.mail.MessagingException;
  24. import javax.mail.Multipart;
  25. import javax.mail.PasswordAuthentication;
  26. import javax.mail.Session;
  27. import javax.mail.internet.InternetAddress;
  28. import javax.mail.internet.MimeBodyPart;
  29. import javax.mail.internet.MimeMessage;
  30. import javax.mail.internet.MimeMultipart;
  31. import javax.persistence.EntityManager;
  32. import javax.persistence.EntityManagerFactory;
  33. import javax.persistence.Persistence;
  34. import org.apache.poi.ss.usermodel.ExcelStyleDateFormatter;
  35. import org.joda.time.DateTime;
  36. import org.joda.time.format.DateTimeFormat;
  37. import org.joda.time.format.DateTimeFormatter;
  38.  
  39.  
  40. /**
  41.  *
  42.  * @author Amr
  43.  */
  44. public class SMTPAgent {
  45.    
  46.     private String Host = AppConfCC.getESD_MAIL_HOST();//"https://mail.etisalat.com/ews/exchange.asmx";
  47.     private String UserName = AppConfCC.getESD_MAIL_USER();//"esd.helpdesk";
  48.     private String From = AppConfCC.getESD_MAIL_FROM();//"ESD-HelpDesk-Ericsson@etisalat.com";//ESD HelpDesk Ericsson <ESD-HelpDesk-Ericsson@etisalat.com>
  49.     private String Password = AppConfCC.getESD_MAIL_PASS();//"Temp4321";
  50.     private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMhhmmss");
  51.    
  52.     public void SendEmail(String SavePath,Address[] distList,String Subject,String MessageToSend,String[] Path,String[] FileName) throws MessagingException, IOException{
  53.        
  54.             Session session=Session.getInstance(new Properties(), new Authenticator()
  55.       {
  56.           @Override
  57.           protected PasswordAuthentication getPasswordAuthentication() {
  58.              
  59.               return new PasswordAuthentication(UserName,Password);
  60.              
  61.           }
  62.       });
  63.        
  64.  
  65. //            Transport lTransport = session.getTransport("ewstransport");
  66. //            lTransport.connect(
  67. //                                  Host,
  68. //                                  UserName,
  69. //                                  Password);
  70.  
  71.            Message message = new MimeMessage(session);
  72.            message.setFrom(new InternetAddress(From));
  73.            message.setRecipients(Message.RecipientType.TO,distList);
  74.            message.setSubject(Subject);  
  75.            
  76.             message.setSubject(Subject);  
  77.             message.setText(MessageToSend);
  78.             MimeMultipart multipart = new MimeMultipart("related");
  79.             MimeBodyPart messageBodyPart = new MimeBodyPart();  
  80.             messageBodyPart.setContent(MessageToSend,"text/html");//
  81.             multipart.addBodyPart(messageBodyPart);
  82.  
  83.             for (int i = 0; i < Path.length; i++)
  84.             {
  85.               if(Path[i].contains("."))  
  86.         addAttachment(multipart, Path[i],FileName[i],i);
  87.            
  88.             }
  89.    
  90.    message.setContent(multipart);
  91.  
  92.            
  93.    
  94.     message.writeTo(new FileOutputStream(new File(SavePath+"/"+Subject+".eml")));
  95.    
  96.    
  97.          
  98. //           lTransport.sendMessage(message, distList);
  99.          
  100.         if(FTPFile.FTPThisFile(SavePath+"/"+Subject+".eml", Subject+".eml"))
  101.         {
  102.             SmtpSendingQueue msg = new SmtpSendingQueue();
  103.             msg.setFtped(true);
  104.             msg.setLocation(AppConfCC.getSMTP_PATH()+"/"+Subject+".eml");
  105.             msg.setId(new Long(sdf.format(DateTime.now().toDate())));
  106.             persist(msg);
  107.            System.out.println("Message Sent !!");
  108.            
  109.         }
  110.      
  111. //           lTransport.close();
  112. //           lTransport = null;
  113.            
  114.            session = null;
  115.        
  116.        
  117.          
  118.          
  119.          
  120.  
  121. }
  122.  
  123.      private static void addAttachment(Multipart multipart, String Path,String Name,int x)
  124. {
  125.         try {
  126.             MimeBodyPart messageBodyPart2 = new MimeBodyPart();
  127.             DataSource source2 = new FileDataSource(Path);
  128.             messageBodyPart2.setDataHandler( new DataHandler(source2));
  129.             messageBodyPart2.setFileName(Name);
  130.             multipart.addBodyPart(messageBodyPart2);
  131.         }
  132.         catch (MessagingException ex) {
  133.             Logger.getLogger(SMTPAgent.class.getName()).log(Level.SEVERE, null, ex);
  134.         }
  135. }
  136.  
  137.     public void persist(Object object) {
  138.         EntityManagerFactory emf = Persistence.createEntityManagerFactory("Mubasher_CC_1027_OutageDB");
  139.         EntityManager em = emf.createEntityManager();
  140.         em.getTransaction().begin();
  141.         try {
  142.             em.persist(object);
  143.             em.getTransaction().commit();
  144.         } catch (Exception e) {
  145.             e.printStackTrace();
  146.             em.getTransaction().rollback();
  147.         } finally {
  148.             em.close();
  149.         }
  150.     }
  151.  
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement