ilias basha | 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} |
| Please sign in to flag this as inappropriate. |