« Back to CVP - All Versions

Send Email in On Call End class

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
I'm implemeting an updated On Call End class for my home grown courtesy callback app in 7.5 so I can do away with hotevent traps which do not work most of the time (cause lots of session timeouts).  However, in my hotevent traps in the call studio GUI I use sendmail in different ways to notify me that something went wrong.  I want to do the same in the On Call End class. 
 
I find lots of java sendmail examples, most using javax however it appears we do not have the javax.mail.* packages installed natively.  I see a ant sendmail jar file from apache in the eclipse directory but haven't found much to use it.
 
Anyone send email in an On Call End class that would share their source code here?  Or point in some direction, I'm not against figuring this out myself and will keep plugging away, just thought I'd come up for a breath and ask before I go read the documentation :-)
 
Thanks, Bill Westby
Medtronic

You needs to have activation.jar and mail.jar in your buildpath to execute the above source code..

Hi Westby,
 
Here is  sample code to send an email and the same you can invoke from the
1CallEndClass
that
1implements EndCallInterface

 

 1package mail;
 2 
 3import javax.activation.DataHandler;
 4import javax.activation.DataSource;
 5import javax.activation.FileDataSource;
 6import javax.mail.*;
 7import javax.mail.internet.*;
 8import javax.mail.Authenticator;
 9import javax.mail.BodyPart;
10import javax.mail.Multipart;
11import javax.mail.PasswordAuthentication;
12 
13import sun.rmi.transport.Transport;
14 
15import java.io.File;
16import java.io.FileInputStream;
17import java.io.FileOutputStream;
18import java.util.Properties;
19 
20 
21public class MailWithAttachementAndAuthentication {
22 
23    private final String SMTP_HOST_NAME = "hostname";
24    private final String SMTP_AUTH_USER = "useName";
25    private final String SMTP_AUTH_PWD  = "passWord";
26 
27    public static void main(String[] args) throws Exception{
28        new MailWithAttachementAndAuthentication().sendMail();
29    }
30 
31    public void sendMail() throws Exception{
32        Properties props = new Properties();
33        props.put("mail.transport.protocol", "smtp");
34        props.put("mail.smtp.host", SMTP_HOST_NAME);
35        props.put("mail.smtp.auth", "true");
36        props.put("mail.smtp.port", "25");
37 
38        Authenticator auth = new SMTPAuthenticator();
39        Session mailSession = Session.getDefaultInstance(props, auth);
40        // uncomment for debugging infos to stdout
41        mailSession.setDebug(true);
42        javax.mail.Transport transport = mailSession.getTransport();
43 
44        MimeMessage message = new MimeMessage(mailSession);
45        //    message.setContent("This is a test", "text/plain");
46        message.setFrom(new InternetAddress("yourMail@organization.com"));
47 
48        /*        message.addRecipient(Message.RecipientType.TO,
49                new InternetAddress("yourMail@organization.com"));*/
50        message.addRecipient(Message.RecipientType.TO,
51                new InternetAddress("yourMail@organization.com"));
52 
53        /*        message.addRecipient(Message.RecipientType.CC,
54                new InternetAddress("yourMail@organization.com"));*/
55 
56        message.setSubject("Sample Subject");
57 
58        // Create the message part 
59        BodyPart messageBodyPart = new MimeBodyPart();
60       
61        // Fill the message
62        messageBodyPart.setText("Hi, This is Machine generated Mail.");
63       
64        Multipart multipart = new MimeMultipart();
65        multipart.addBodyPart(messageBodyPart);
66 
67        // Part two is attachment
68        messageBodyPart = new MimeBodyPart();
69        String filename = "c:\\Sample.pdf";
70        DataSource source = new FileDataSource(filename);
71        messageBodyPart.setDataHandler(new DataHandler(source));
72        messageBodyPart.setFileName(filename);
73        multipart.addBodyPart(messageBodyPart);
74 
75        // Put parts in message
76        message.setContent(multipart);
77 
78        transport.connect();
79        transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));       
80        transport.close();
81 
82        System.out.println("mail sent successfully");
83    }
84 
85    private class SMTPAuthenticator extends javax.mail.Authenticator {
86        public PasswordAuthentication getPasswordAuthentication() {
87            String username = SMTP_AUTH_USER;
88            String password = SMTP_AUTH_PWD;
89            return new PasswordAuthentication(username, password);
90        }
91    }
92}

Thanks Ilias so much, I'll give it a try today!!!
 
Bill Westby.