私が書いているREST MVCの春アプリに春の統合を追加しようとしています。私は最新のSpring 4.2.xをコア、統合、およびMVCに使用しています。この考え方は、Dynamic FTPの例とは別のアプリケーションコンテキストを作成することです。なぜなら、私は2つの別々のアカウントから電子メールを送信することができるだけでなく、2つの別々のアカウントから耳を傾けることができ、したがって、各コンテキストのBean作成を支援するための環境変数だけでなく、XMLの代わりにJavaクラスを使用してSpring Integration 4.3 Email送信フローを設定する方法は?
私は初心者の質問をお詫び申し上げますが、私はマニュアルに苦労してだけでなく、XMLせずにどのようにセットアップSMTP電子メールの設定クラスに把握しようとしています。
は私が持っている両方の受信および統合チャネルを送信します。すべての電子メール設定は環境変数から設定されるので、環境を注入しました:@Autowired Environment env;
私が定義することができます。
- A MailSenderの豆 SMTP用
- A MessageChannel(アウトバウンド)
さて、XML構成でおoutbound-を持って豆
私の目標は、次のような設定をすることです:
- メールを送信します。
- IMAPメールを聞き、それらを処理します。
電子メールを送信するには、残りのエンドポイントからサービスを呼び出すことが考えられます。そのサービスは、インテグレーションSMTPアウトバウンドチャネルに電子メールを送信するメッセージです。 MailSendingMessageHandlerを使用すると、統合メッセージが取得され、MailSenderのメールメッセージに変換されます。 MailSendingMessageHandlerを発信チャンネルに接続して、電子メールを送信できるようにする方法はわかりません。また、残りのエンドポイントによって呼び出される@Serviceクラスからメッセージを作成して送信SMTPチャネル経由で送信する方法を知らないので、電子メールを送信できます。 1回の休憩では、私が手に入れたいすべての電子メール受信者を送信します。 MailSendingMessageHandlerによって処理され、変換される各統合メッセージ(電子メールとして)を作成できるように、各電子メールメッセージ本文は適切にフォーマットされています。私はこれを達成するための成功なしに例をオンラインで見つけることを試みました。
あなたは私をリダイレクトすることができ、任意の例?前もって感謝します!
は、これまでのところ私は、コンフィギュレーションのために持っている:
import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.integration.annotation.InboundChannelAdapter;
import org.springframework.integration.config.EnableIntegration;
import org.springframework.integration.annotation.Poller;
import org.springframework.integration.channel.DirectChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.mail.MailReceiver;
import org.springframework.integration.mail.MailReceivingMessageSource;
import org.springframework.integration.mail.MailSendingMessageHandler;
import org.springframework.mail.MailMessage;
import org.springframework.mail.MailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessagingException;
import org.springframework.core.env.Environment;
@Configuration
@EnableIntegration
public class IntegrationEmailConfig {
@Autowired
Environment env;
@Bean
public static PropertySourcesPlaceholderConfigurer pspc() {
return new PropertySourcesPlaceholderConfigurer();
}
@Bean
@InboundChannelAdapter(value = "emailInboundChannel", poller = @Poller(fixedDelay = "5000"))
public MailReceivingMessageSource mailMessageSource(MailReceiver imapMailReceiver) {
return new MailReceivingMessageSource(imapMailReceiver);
}
private Properties additionalMailProperties() {
Properties properties = new Properties();
if (env.containsProperty("mail.smtp.auth")) {
properties.setProperty("mail.smtp.auth",env.getProperty("mail.smtp.auth"));
}
if (env.containsProperty("mail.smtp.starttls.enable")) {
properties.setProperty("mail.smtp.starttls.enable",env.getProperty("mail.smtp.starttls.enable"));
}
return properties;
}
@Bean
public MailSender mailSender() throws Exception {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
if (env.containsProperty("mail.server.host")) {
mailSender.setHost(env.getProperty("mail.server.host"));
} else {
throw new Exception("Missing mail.server.host property");
}
if (env.containsProperty("mail.server.port")) {
mailSender.setPort(Integer.parseInt(env.getProperty("mail.server.port")));
} else {
throw new Exception("Missing mail.server.port property");
}
if (env.containsProperty("mail.server.username")) {
mailSender.setUsername(env.getProperty("mail.server.username"));
} else {
throw new Exception("Missing mail.server.username property");
}
if (env.containsProperty("mail.server.password")) {
mailSender.setPassword(env.getProperty("mail.server.password"));
} else {
throw new Exception("Missing mail.server.password property");
}
mailSender.setJavaMailProperties(additionalMailProperties());
return mailSender;
}
@Bean
public MailSendingMessageHandler mailSendingMessageHandler() throws Exception {
MailSendingMessageHandler mailSendingMessageHandler = new MailSendingMessageHandler(mailSender());
//mailSendingMessageHandler.setChannelResolver(channelResolver);
return mailSendingMessageHandler;
}
/* @Bean
public DirectChannel outboundMail() {
DirectChannel outboundChannel = new DirectChannel();
return outboundChannel;
}
*/
@Bean
public MessageChannel smtpChannel() {
return new DirectChannel();
}
/* @Bean
@Value("${imap.url}")
public MailReceiver imapMailReceiver(String imapUrl) {
// ImapMailReceiver imapMailReceiver = new ImapMailReceiver(imapUrl);
// imapMailReceiver.setShouldMarkMessagesAsRead(true);
// imapMailReceiver.setShouldDeleteMessages(false);
// // other setters here
// return imapMailReceiver;
MailReceiver receiver = mock(MailReceiver.class);
MailMessage message = mock(Message.class);
when(message.toString()).thenReturn("Message from " + imapUrl);
Message[] messages = new Message[] {message};
try {
when(receiver.receive()).thenReturn(messages);
}
catch (MessagingException e) {
e.printStackTrace();
}
return receiver;
}*/
}
ソースコードMailNamespaceHandlerを見て、XML注釈がどこに定義されているかを確認しました。したがって、 "outbound-channel-adapter"、新しいMailOutboundChannelAdapterParser()。それはずっと意味をなさないようになります。私はここでさらなる助けを得る前にこれを理解しようとすると、コードの変更をいくつか追加します。 –