I tried many blogs but none of them could solve my problem. Though stand alone java programs were sending the meeting invites, but on trying to send from JBOSS webservice, they were throwing exceptions and were failing.
Finally the code that worked for me was
// the acceptance or rejection notification goes to this email address
private String rsvpTo="ToEmail"; // eg: xxx@gmail.com
private String fromEmail="RSVPEmail"; // eg: xxxyy@gmail.com
private String toEmail;
// meeting details are extracted from agendaDetails object passed in the constructor
public SendMeetingMail(String toEmail)
{
this.toEmail = toEmail;
}
/**
* The method that takes care of sending the meeting invite
* @throws Exception the caller has to catch the exception to ascertain if there is an error while sending.
*/
public void send() throws Exception {
try {
Session session = Session.getDefaultInstance(props,
new CredentialAuthenticator());
session.setDebug(true);
Transport transport = session.getTransport();
// another approach
MimeMessage message = new MimeMessage(session);
message.setFrom( new InternetAddress(fromEmail, rsvpTo) );
javax.mail.Address address = new InternetAddress(toEmail,toEmail);
message.addRecipient(MimeMessage.RecipientType.TO, address);
message.setSubject(summary);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Sent from Summit Application.");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = "demo.vcs";
messageBodyPart.setFileName(filename);
messageBodyPart.setContent(buffer.toString(), "text/plain");
// Add part two
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// send message
// Connect, Send the Message, and Close the Connection
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
// Then print a success message - this is only for testing
System.out.println("Message sent!");
} catch (MessagingException me) {
me.printStackTrace();
// again throw it to ws
throw me;
} catch (Exception ex) {
ex.printStackTrace();
// again throw it to ws
throw ex;
}
}
private class CredentialAuthenticator extends javax.mail.Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xxx@gmail.com",
"passcode");
}
}
public static void main(String[] args) {
try {
String toEmail="dummyemail@gmail.com";
SendMeetingMail obj = new SendMeetingMail(toEmail);
obj.send();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Bookmarks