Java tutorial
/* * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.angstoverseer.service.mail; import com.angstoverseer.service.command.CommandService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import java.io.*; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; @Component public class MailServiceImpl implements MailService { @Autowired private CommandService commandService; @Override public void handleIncomingEmail(InputStream inputStream) { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); try { MimeMessage mimeMessage = new MimeMessage(session, inputStream); Object messageContent = mimeMessage.getContent(); String message; if (messageContent instanceof Multipart) { Multipart mp = (Multipart) messageContent; BodyPart bodyPart = mp.getBodyPart(0); InputStream is = bodyPart.getInputStream(); message = convertStreamToString(is); } else { message = (String) messageContent; } Address sender = mimeMessage.getFrom()[0]; final String commandOutput = commandService.process(message, extractEmail(sender.toString())); sendResponseMail(sender, commandOutput, mimeMessage.getSubject()); } catch (Exception e) { throw new RuntimeException(e); } } String extractEmail(String sender) { Pattern pattern = Pattern .compile("[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})"); Matcher matcher = pattern.matcher(sender); if (!matcher.find()) { throw new RuntimeException("No valid e-mail could be found: " + sender); } return matcher.group(0); } private String convertStreamToString(final InputStream is) { if (is != null) { StringBuilder sb = new StringBuilder(); String line; try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); while ((line = reader.readLine()) != null) { sb.append(line).append("\n"); } } catch (Exception e) { throw new RuntimeException(e); } finally { try { is.close(); } catch (Exception e) { throw new RuntimeException(e); } } return sb.toString(); } else { return ""; } } private void sendResponseMail(final Address sender, final String answer, final String subject) throws Exception { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); Message mimeMessage = new MimeMessage(session); mimeMessage.setFrom(new InternetAddress("overseer@thenewoverseer.appspotmail.com", "Overseer")); mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(sender.toString())); mimeMessage.setSubject("Re: " + subject); mimeMessage.setText(answer); Transport.send(mimeMessage); } }