Skip to content

Instantly share code, notes, and snippets.

@prabhatkashyap
Created October 25, 2017 14:42
Show Gist options
  • Select an option

  • Save prabhatkashyap/ec3a7ec4719f9a7baada81ebc3502ffd to your computer and use it in GitHub Desktop.

Select an option

Save prabhatkashyap/ec3a7ec4719f9a7baada81ebc3502ffd to your computer and use it in GitHub Desktop.
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
final String username = ""
final String password = ""
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", username, password);
//Create a Folder object and open the folder
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_ONLY);
String messageId = ""
SearchTerm searchTerm = new MessageIDTerm(messageId);
List messages = folder.search(searchTerm);
Message message = messages?.first()
// Get all the information from the message
String from = InternetAddress.toString(message.getFrom());
if (from != null) {
System.out.println("From: " + from);
}
String replyTo = InternetAddress.toString(message.getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}
String to = emailMessage?.senderEmail
if (to != null) {
System.out.println("To: " + to);
}
String subject = message.getSubject();
if (subject != null) {
System.out.println("Subject: " + subject);
}
Date sent = message.getSentDate();
if (sent != null) {
System.out.println("Sent: " + sent);
}
System.out.println(message.getContent());
// compose the message to forward
Message message2 = new MimeMessage(session);
message2 = (MimeMessage) message.reply(false);
message2.setSubject("RE: " + message.getSubject());
String defaultFrom = grailsApplication.config.grails.mail.default.from
message2.setFrom(new InternetAddress(defaultFrom));
message2.setReplyTo(message.getReplyTo());
message2.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// message2.addRecipient(Message.RecipientType.TO);
// Create your new message part
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(replyText);
// Create a multi-part to combine the parts
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Create and fill part for the forwarded content
// messageBodyPart = new MimeBodyPart();
// messageBodyPart.setDataHandler(message.getDataHandler());
// Add part to multi part
// multipart.addBodyPart(messageBodyPart);
// Associate multi-part with message
message2.setContent(multipart);
// Send message
Transport.send(message2);
System.out.println("message replied successfully ....");
} catch (Exception e) {
throw new RuntimeException(e);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment