I'm trying to listen my Gmail inbox for incoming mails. Every time new mail arrives, I want to see it's subject and content.
So far, I have this:
import java.io.IOException;
import javax.mail.BodyPart;
import javax.mail.Folder;
import javax.mail.internet.ContentType;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.mail.util.MimeMessageParser;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.mail.transformer.MailToStringTransformer;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.MessagingException;
public class GmailInboundImapIdleAdapterTestApp {
private static Log logger = LogFactory.getLog(GmailInboundImapIdleAdapterTestApp.class);
public static void main (String[] args) throws Exception {
#SuppressWarnings("resource")
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("/META-INF/spring/integration/gmail-imap-idle-config.xml");
DirectChannel inputChannel = ac.getBean("receiveChannel", DirectChannel.class);
inputChannel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message){
MimeMessage mm = (MimeMessage) message.getPayload();
try {
System.out.println("Subject: "+mm.getSubject());
System.out.println("Body: "+readPlainContent(mm));
}
catch (javax.mail.MessagingException e) {
System.out.println("MessagingException: "+e.getMessage());
e.printStackTrace();
}
catch (Exception e) {
System.out.println("Exception: "+e.getMessage());
e.printStackTrace();
}
}
});
}
private static String readHtmlContent(MimeMessage message) throws Exception {
return new MimeMessageParser(message).parse().getHtmlContent();
}
private static String readPlainContent(MimeMessage message) throws Exception {
return new MimeMessageParser(message).parse().getPlainContent();
}
}
It can read the mail subject correctly. But no luck with mail body.javax.mail.FolderClosedException hit me. How to fix this?
As Gary said: simple-content="true" or since recently autoCloseFolder = false: https://docs.spring.io/spring-integration/docs/5.2.0.RELEASE/reference/html/mail.html#mail-inbound
Starting with version 5.2, the autoCloseFolder option is provided on the mail receiver. Setting it to false doesn’t close the folder automatically after a fetch, but instead an IntegrationMessageHeaderAccessor.CLOSEABLE_RESOURCE header (see MessageHeaderAccessor API for more information) is populated into every message to producer from the channel adapter. It is the target application’s responsibility to call the close() on this header whenever it is necessary in the downstream flow:
Related
I am working on an application using Play Framework and apache common email + freemarker.
Using this I went into a problem, whenever i send an email i goot the following error message :
javax.mail.MessagingException: IOException while sending message;
nested exception is: javax.activation.UnsupportedDataTypeException: no
object DCH for MIME type multipart/mixed
Here's the email stack :
package service.email;
import com.google.common.base.Strings;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.ImageHtmlEmail;
import play.Logger;
import utils.ConfigurationUtils;
import utils.enums.EmailTemplates;
import javax.activation.CommandMap;
import javax.activation.MailcapCommandMap;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import static java.util.Objects.nonNull;
import static utils.enums.ConfigurationKey.*;
#Singleton
public class EmailService {
#Inject
private MarkerService markerService;
#Inject
private ConfigurationUtils configurationUtils;
private ImageHtmlEmail email;
private final Configuration freemarkerConfiguration;
private final String templatePrefixPath;
private final String from;
private final String overrideTo;
#Inject
public EmailService(ConfigurationUtils configurationUtils) {
this.configurationUtils = configurationUtils;
freemarkerConfiguration = new Configuration();
freemarkerConfiguration.setDefaultEncoding("UTF-8");
freemarkerConfiguration.setLocale(Locale.FRANCE);
freemarkerConfiguration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
templatePrefixPath = configurationUtils.getString(EMAIL_TEMPLATES_PATH);
from = configurationUtils.getString(EMAIL_FROM);
overrideTo = configurationUtils.getString(EMAIL_OVERRIDE_TO);
}
/**
* #param to
* #param emailTemplate
* #param emailAttachments
* #return CompletionStage<Void>
* #throws EmailException
* #throws IOException
* #throws TemplateException
*/
public CompletionStage<Void> send(String to, EmailTemplates emailTemplate, Map<String, String> datas,
List<EmailAttachment> emailAttachments) {
try {
Logger.info("[EmailService] Building email :\n\tSent to {}\n\tTemplate used : {}\n", to, emailTemplate.getFileName());
ImageHtmlEmail email = new ImageHtmlEmail();
email.setHostName(configurationUtils.getString(EMAIL_HOSTNAME));
email.setSmtpPort(configurationUtils.getInt(EMAIL_SMPT_PORT));
email.setAuthenticator(new DefaultAuthenticator(configurationUtils.getString(EMAIL_USERNAME), configurationUtils.getString(EMAIL_PASSWORD)));
email.setSSLOnConnect(configurationUtils.getBoolean(EMAIL_SSL_ENABLED));
// This is useful in dev mode, you can redirect all emails to a single recipient by supplying the 'to' attribute
email.addTo(Strings.isNullOrEmpty(overrideTo) ? to : overrideTo);
email.setFrom(from);
email.setSubject(emailTemplate.getSubject());
Logger.info("[EmailService] Preparing freemarker binding...");
Template template = freemarkerConfiguration.getTemplate(templatePrefixPath + emailTemplate.getFileName());
Writer stringWriter = new StringWriter();
template.process(datas, stringWriter);
stringWriter.flush();
stringWriter.close();
email.setHtmlMsg(stringWriter.toString());
email.setTextMsg("Your email client does not support HTML messages");
Logger.info("[EmailService] attaching files...");
if (nonNull(emailAttachments)) {
for (EmailAttachment emailAttachment : emailAttachments) {
email.attach(emailAttachment);
}
}
Logger.info("[EmailService] Sending email...");
email.send();
} catch (EmailException | IOException | TemplateException e) {
// TODO : Manage exception by type
e.printStackTrace();
Logger.debug("Error While sending email...\n");
}
return CompletableFuture.completedFuture(null);
}
public CompletionStage<Void> send(String to, EmailTemplates emailTemplate, Map<String, String> datas) {
return send(to, emailTemplate, datas, null);
}
}
The template :
<html>
<head>
<title>Test</title>
</head>
<body>
${URL_RESET_PASSWORD}
</body>
</html>
This problem has been bugging me for a week now... I don't really get why the error appears, all I know that the DCH is null and the DCH Factory is also null.
The solution I finally found :
CompletableFuture.runAsync(() -> {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
try {
email.send();
} catch (final EmailException e) {
throw new RuntimeException(e);
}
}, Executors.newSingleThreadExecutor());
The dch was null because the class loader at the current time was null.
package com.act.webmail.service;
import javax.mail.internet.MimeMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.act.webmail.dto.MessageToSend;
#Service
public class ActMailSenderService {
#Autowired
private JavaMailSender javaMailSender;
#Async
public void sendEmail(MessageToSend messageToSend, MultipartFile... multipartFileList) {
try {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
if (messageToSend.getReplyTo() != null && !messageToSend.getReplyTo().equals("")){
mimeMessage.addHeader("In-Reply-To", messageToSend.getReplyTo());
}
helper.setFrom("jackson.baby#xxxx.in");
helper.setTo(messageToSend.getTo());
helper.setSubject(messageToSend.getSubject());
helper.setText(messageToSend.getBody(), true);
for (MultipartFile multipartFile : multipartFileList) {
byte[] multipartFileByteArray= multipartFile.getBytes();
helper.addAttachment(multipartFile.getOriginalFilename(), new ByteArrayResource(multipartFileByteArray));
}
javaMailSender.send(mimeMessage);
System.out.println(messageToSend.getReplyTo() + " sent successfully!");
} catch (Exception e) {
e.printStackTrace();
}
}
}
I initialised new ByteArrayResource(multipartFile.getBytes()) and used in addAttachment function of org.springframework.mail.javamail.MimeMessageHelper but i'm getting an exception "java.io.FileNotFoundException: C:\Users\Jackson Baby\AppData\Local\Temp\tomcat.8088819519816892725.8080\work\Tomcat\localhost\ROOT\upload_525fd01b_db90_4589_921f_50bf9a1e6e47_00000001.tmp (The system cannot find the file specified)"
is there any way to solve this issue?
I had the same issue where when I would try to the get the bytes or input stream of the file; I would be getting File Not Found Exception.
I finally solved it by removing the #Async Annotation from the Rest Controller Method.
That seemed to be creating the issue...
I am using websockets for the first time on a javafx project, when I start the program the session is set to the local variable session, but after when I call the sendMessage function the session is back to null. Below please find my client class
package myclient;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javax.websocket.ClientEndpoint;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
#ClientEndpoint
public class Client extends Application {
private static final Logger LOGGER = Logger.getLogger(Client.class.getName());
private Session session;
#OnOpen
public void onOpen(Session session){
this.session = session;
System.out.println("Opened Session " + this.session);
}
#OnClose
public void onClose(){
System.out.println("Closed Session " + this.session);
}
#OnMessage
public void onMessage(String msg){
System.out.println("Websocket message received! " + msg);
}
#Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("FXMLClient.fxml"));
Scene scene = new Scene(root);
connectToWebSocket();
stage.setScene(scene);
stage.show();
}
private void connectToWebSocket() {
System.out.println("Client WebSocket initialized>> " + this.session);
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
try {
URI uri = URI.create("ws://localhost:8080/Server/endpoint");
container.connectToServer(this, uri);
}
catch (DeploymentException | IOException ex) {
LOGGER.log(Level.SEVERE, null, ex);
System.exit(-1);
}
}
public void sendMessage(String message) throws IOException{
if(this.session != null){
System.out.println(message + ", " + this.session);
this.session.getBasicRemote().sendText(message);
}
else {
System.out.println("Session is null");
}
}
public static void main(String[] args) {
launch(args);
}
}
Any suggestions?
Thanks in advance
I think I now do know the answer to this.
You are probably using tomcat or some other server for this. When you see "tomcat" in this answer, please insert the name of your actually used server.
When a connection to your websocket is opened, tomcat will create an instance of the websocket (your Client) class by itself. This means, the onOpen-Method will be called and it will look as if it was you who created the instance, who opened the connection, when really you did not. Tomcat did.
This in turn means, that when you call sendMessage on your Client instance, the session will be null, because this object never connected anywhere.
Oh, and you don't have access to the connected instance that was created by tomcat.
One way of fixing this would be to do all the work inside the onOpen-Method, however that is not practical. You may want to put the work in another method and call it from onOpen. That way, the instance created by tomcat will do the necessary work.
In my project I needed to poll on an MQTT-Topic and render the data on a website (university assignment). I did the polling in a separate class, resulting in hard to debug errors whenever trying to send received data with my sendMessage-method.
I hope this answer does clear this up a little, if not for you, maybe at least for future generations having the same university assignment...
I have a web application I am making using a websocket API to handle the websockets, here is the code for that part
package comm2.hello;
import java.io.IOException;
import java.util.ArrayList;
import javax.websocket.OnClose;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.ServerEndpoint;
import org.apache.catalina.session.*;
#ServerEndpoint(value = "/echo")
public class wschat {
private static ArrayList<Session> sessionList = new ArrayList<Session>();
#OnOpen
public void onOpen(Session session) {
try {
sessionList.add(session);
// asynchronous communication
session.getBasicRemote().sendText("hello");
} catch (IOException e) {
}
}
public void send(String text, Session session) throws IOException {
session.getBasicRemote().sendText(text);
}
}
I am trying to have another java class then call into the send method to send messages, using the following code.
package comms;
import java.io.IOException;
import java.util.ArrayList;
import javax.websocket.Session;
import javax.websocket.Session;
import comm2.hello.*;
public class main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
wschat h = new wschat();
String text = "hello";
//session shouldn't be null but not sure what to make it
Session session = null;
h.send(text,session);
}
}
As you can see, I have the session variable in the main.java class set to null which will thus always produce a null pointer error. This is because I am not sure what to make session equal to, does anyone have any idea what to initialize the session variable to in main.java?
I have the following clientendpoint class for a websocket in tomcat 7.0.53. It is based off of this example on a website https://blog.openshift.com/how-to-build-java-websocket-applications-using-the-jsr-356-api/
import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import javax.websocket.ClientEndpoint;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.OnClose;
import javax.websocket.OnError;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.WebSocketContainer;
#ClientEndpoint
public class connect {
private static ArrayList<Session> sessionList = new ArrayList<Session>();
public connect(URI endpointURI) throws DeploymentException, IOException
{
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
container.connectToServer(this, endpointURI);
}
#OnOpen
public void onOpen(Session session) throws IOException
{
sessionList.add(session);
System.out.println(session.getId());
session.getBasicRemote().sendText("hello");
}
public void sendMessage(String message) throws IOException
{
for(Session session : sessionList){
//asynchronous communication
session.getBasicRemote().sendText(message);
}
}
#OnClose
public void onClose(Session session){
sessionList.remove(session);
System.out.println("here");
}
#OnError
public void onError(Throwable t, Session session){
System.out.println("tedt");
}
}
I then have the following code to start the client endpoint
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.websocket.DeploymentException;
public class test {
public static void main(String[] args) throws DeploymentException, IOException, URISyntaxException {
// TODO Auto-generated method stub
connect connect = new connect(new URI("ws://localhost:8080/example/talk"));
connect.sendMessage("now");
}
}
The client does successfully connect to the websocket server, however then it gets disconnected right away when I try to send a message or do anything, I know this since the onError function is being called when I try to send a message from the onOpen function. Why is the websocket getting closed immediately after it is connected to the server?
You are being disconnected because your main thread in your client application is ending. After you send "now", your program simply exits. If you want to do anything else (like wait for a response from the server, for instance), then you'll have to prevent the main thread from exiting. Try something like this at the end of your main method:
System.in.read();
This will cause your process to sit and wait for input from standard input. Simply wait for your test to complete and then press ENTER on the command-line to terminate the client.
You will, of course, want to register a handler for receiving messages back from the server to the client. Right now, you can only send messages from the client to the server.