1 package uk.ac.ebi.intenz.tools.export;
2
3 import javax.mail.internet.InternetAddress;
4 import javax.mail.internet.MimeMessage;
5 import javax.mail.internet.MimeBodyPart;
6 import javax.mail.internet.MimeMultipart;
7 import javax.activation.DataHandler;
8 import javax.activation.FileDataSource;
9 import java.util.Date;
10 import java.util.Properties;
11 import java.io.File;
12 import javax.mail.BodyPart;
13 import javax.mail.Message;
14 import javax.mail.MessagingException;
15 import javax.mail.Part;
16 import javax.mail.Session;
17 import javax.mail.Transport;
18
19
20
21
22
23
24
25 public class IntEnzMessenger {
26
27 private static final String SMTP_SERVER = "mailserv.ebi.ac.uk";
28 private static final String SENDER = "mdarsow@ebi.ac.uk";
29
30 public static void sendError(String className, String errorMessage, String userName) {
31 Date today = new Date();
32 Properties properties = System.getProperties();
33 properties.put("mail.smtp.host", SMTP_SERVER);
34 Session session = Session.getDefaultInstance(properties, null);
35 Message message = new MimeMessage(session);
36 try {
37 message.setFrom(new InternetAddress(SENDER));
38 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(SENDER, false));
39 message.setSubject("Notification error " + today.toString());
40 StringBuffer text = new StringBuffer();
41 text.append("Location: ");
42 text.append(className);
43 text.append("\nUser name: ");
44 text.append(userName);
45 text.append("\nError message: ");
46 text.append(errorMessage);
47 message.setText(text.toString());
48 message.setSentDate(today);
49 Transport.send(message);
50 } catch (MessagingException e) {
51 e.printStackTrace();
52 }
53 }
54
55 public static void sendNotificationWithAttachment(String from, String to, String subject, String messageText, File singleAttachment) {
56 Date today = new Date();
57 Properties properties = System.getProperties();
58 properties.put("mail.smtp.host", SMTP_SERVER);
59 Session session = Session.getDefaultInstance(properties, null);
60 Message message = new MimeMessage(session);
61 try {
62 message.setFrom(new InternetAddress(from));
63 message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to, false));
64 message.setSubject(subject);
65 MimeMultipart mp = new MimeMultipart();
66 MimeBodyPart mbp = new MimeBodyPart();
67 mbp.setDisposition(Part.INLINE);
68 mbp.setContent(messageText, "text/plain");
69 mp.addBodyPart(mbp);
70
71 BodyPart file_part = new MimeBodyPart();
72 FileDataSource fds = new FileDataSource(singleAttachment);
73 DataHandler dh = new DataHandler(fds);
74 file_part.setFileName(singleAttachment.getName());
75 file_part.setDisposition(Part.ATTACHMENT);
76 file_part.setDescription("Attached file: " + singleAttachment.getName());
77 file_part.setDataHandler(dh);
78 mp.addBodyPart(file_part);
79
80 message.setContent(mp);
81 message.setSentDate(today);
82 Transport.send(message);
83 } catch (MessagingException e) {
84 e.printStackTrace();
85 }
86 }
87 }