]> matita.cs.unibo.it Git - logicplayer.git/blob - Server/server/tesi/src/com/company/EmailSender.java
new initial commit
[logicplayer.git] / Server / server / tesi / src / com / company / EmailSender.java
1 package com.company;
2
3 import java.util.Date;
4 import java.util.Properties;
5 import javax.mail.Message;
6 import javax.mail.MessagingException;
7 import javax.mail.Multipart;
8 import javax.mail.NoSuchProviderException;
9 import javax.mail.Session;
10 import javax.mail.Transport;
11 import javax.mail.internet.AddressException;
12 import javax.mail.internet.InternetAddress;
13 import javax.mail.internet.MimeBodyPart;
14 import javax.mail.internet.MimeMessage;
15 import javax.mail.internet.MimeMultipart;
16
17  
18
19 public class EmailSender {
20   private String user;
21   private String password;
22   private String host;
23   private String mittente;
24   private String destinatario;
25   private String oggetto;
26   private String mess;
27  
28
29   EmailSender(String user, String password, String host, 
30                      String mittente, String destinatario, 
31                      String oggetto, String mess){
32  
33     this.user = user;
34     this.password = password;
35     this.host = host;
36     this.mittente = mittente;
37     this.destinatario = destinatario;
38     this.oggetto = oggetto;
39     this.mess=mess;
40   }
41
42  
43   // Metodo che si occupa dell'invio effettivo della mail
44   public void inviaEmail() {
45     int port = 465; //porta 25 per non usare SSL
46  
47     Properties props = new Properties();
48     props.put("mail.smtp.auth", "true");
49     props.put("mail.smtp.user", mittente);
50     props.put("mail.smtp.host", host);
51     props.put("mail.smtp.port", port);
52  
53     // commentare la riga seguente per non usare SSL 
54     props.put("mail.smtp.starttls.enable","true");
55     props.put("mail.smtp.socketFactory.port", port);
56  
57     // commentare la riga seguente per non usare SSL 
58     props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
59     props.put("mail.smtp.socketFactory.fallback", "false");
60  
61     Session session = Session.getInstance(props, null);
62     session.setDebug(true);
63  
64     // Creazione delle BodyParts del messaggio
65     MimeBodyPart messageBodyPart1 = new MimeBodyPart();
66  
67     try{
68       // COSTRUZIONE DEL MESSAGGIO
69       Multipart multipart = new MimeMultipart();
70       MimeMessage msg = new MimeMessage(session);
71  
72       // header del messaggio
73       msg.setSubject(oggetto);
74       msg.setSentDate(new Date());
75       msg.setFrom(new InternetAddress(mittente));
76  
77       // destinatario
78       msg.addRecipient(Message.RecipientType.TO,
79       new InternetAddress(destinatario));
80           
81       // corpo del messaggio
82       messageBodyPart1.setText(mess);
83       multipart.addBodyPart(messageBodyPart1);
84  
85       // allegato al messaggio
86       /*DataSource source = new FileDataSource(allegato);
87       messageBodyPart2.setDataHandler(new DataHandler(source));
88       messageBodyPart2.setFileName(allegato);
89       multipart.addBodyPart(messageBodyPart2);*/
90  
91       // inserimento delle parti nel messaggio
92       msg.setContent(multipart);
93  
94       Transport transport = session.getTransport("smtps"); //("smtp") per non usare SSL
95       transport.connect(host, user, password);
96       transport.sendMessage(msg, msg.getAllRecipients());
97       transport.close();
98  
99       System.out.println("Invio dell'email Terminato");
100  
101     }catch(AddressException ae) {
102       ae.printStackTrace();
103     }catch(NoSuchProviderException nspe){
104       nspe.printStackTrace();
105     }catch(MessagingException me){
106       me.printStackTrace();
107     }
108     
109   }
110 }