From d9ecb65c0d8f679533c383dc85407c57cb86d413 Mon Sep 17 00:00:00 2001 From: mguessan Date: Sun, 11 Jul 2010 22:50:20 +0000 Subject: [PATCH] EWS: implement send message (SMTP) git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@1167 3d1905a2-6b24-0410-a738-b14d5a86fcbd --- .../davmail/exchange/ExchangeSession.java | 6 +- .../exchange/ews/EwsExchangeSession.java | 9 +- src/test/davmail/smtp/TestSmtp.java | 116 ++++++++++++++++++ 3 files changed, 127 insertions(+), 4 deletions(-) create mode 100644 src/test/davmail/smtp/TestSmtp.java diff --git a/src/java/davmail/exchange/ExchangeSession.java b/src/java/davmail/exchange/ExchangeSession.java index 3e7dfe86..b3db1862 100644 --- a/src/java/davmail/exchange/ExchangeSession.java +++ b/src/java/davmail/exchange/ExchangeSession.java @@ -913,8 +913,6 @@ public abstract class ExchangeSession { boolean inHeader = true; boolean inRecipientHeader = false; while (!".".equals(line)) { - mailBuffer.append(line).append((char) 13).append((char) 10); - line = reader.readLine(); // Exchange 2007 : skip From: header if ((inHeader && line.length() >= 5)) { String prefix = line.substring(0, 5).toLowerCase(); @@ -931,11 +929,13 @@ public abstract class ExchangeSession { if ((inHeader && line.length() >= 3) || inRecipientHeader) { String prefix = line.substring(0, 3).toLowerCase(); - if ("to:".equals(prefix) || "cc:".equals(prefix) || inRecipientHeader) { + if ("to:".equalsIgnoreCase(prefix) || "cc:".equalsIgnoreCase(prefix) || inRecipientHeader) { inRecipientHeader = true; recipientBuffer.append(line); } } + mailBuffer.append(line).append((char) 13).append((char) 10); + line = reader.readLine(); } // remove visible recipients from list List visibleRecipients = new ArrayList(); diff --git a/src/java/davmail/exchange/ews/EwsExchangeSession.java b/src/java/davmail/exchange/ews/EwsExchangeSession.java index 066eb919..25e8100f 100644 --- a/src/java/davmail/exchange/ews/EwsExchangeSession.java +++ b/src/java/davmail/exchange/ews/EwsExchangeSession.java @@ -217,7 +217,14 @@ public class EwsExchangeSession extends ExchangeSession { @Override public void sendMessage(HashMap properties, String messageBody) throws IOException { - throw new UnsupportedOperationException(); + EWSMethod.Item item = new EWSMethod.Item(); + item.type = "Message"; + item.mimeContent = Base64.encodeBase64(messageBody.getBytes()); + // TODO: handle bcc + Set fieldUpdates = buildProperties(properties); + item.setFieldUpdates(fieldUpdates); + CreateItemMethod createItemMethod = new CreateItemMethod(MessageDisposition.SendAndSaveCopy, getFolderId("Drafts"), item); + executeMethod(createItemMethod); } diff --git a/src/test/davmail/smtp/TestSmtp.java b/src/test/davmail/smtp/TestSmtp.java new file mode 100644 index 00000000..d17621a8 --- /dev/null +++ b/src/test/davmail/smtp/TestSmtp.java @@ -0,0 +1,116 @@ +/* + * DavMail POP/IMAP/SMTP/CalDav/LDAP Exchange Gateway + * Copyright (C) 2010 Mickael Guessant + * + * 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 2 + * 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package davmail.smtp; + +import davmail.AbstractDavMailTestCase; +import davmail.DavGateway; +import davmail.Settings; +import davmail.exchange.ExchangeSessionFactory; +import org.apache.commons.codec.binary.Base64; + +import javax.mail.MessagingException; +import javax.mail.Session; +import javax.mail.internet.MimeMessage; +import java.io.*; +import java.net.Socket; + +/** + * Test smtp send message. + */ +public class TestSmtp extends AbstractDavMailTestCase { + static Socket clientSocket; + static BufferedWriter socketWriter; + static BufferedReader socketReader; + + protected void write(String line) throws IOException { + socketWriter.write(line); + socketWriter.flush(); + } + + protected void writeLine(String line) throws IOException { + socketWriter.write(line); + socketWriter.newLine(); + socketWriter.flush(); + } + + protected String readLine() throws IOException { + return socketReader.readLine(); + } + + protected String readFullAnswer(String prefix) throws IOException { + String line = socketReader.readLine(); + while (!line.startsWith(prefix)) { + line = socketReader.readLine(); + } + return line; + } + + @Override + public void setUp() throws IOException { + super.setUp(); + if (clientSocket == null) { + // start gateway + DavGateway.start(); + clientSocket = new Socket("localhost", Settings.getIntProperty("davmail.smtpPort")); + socketWriter = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream())); + socketReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); + } + if (session == null) { + session = ExchangeSessionFactory.getInstance(Settings.getProperty("davmail.username"), Settings.getProperty("davmail.password")); + } + } + + public void testBanner() throws IOException { + String banner = socketReader.readLine(); + assertNotNull(banner); + } + + public void testLogin() throws IOException { + String credentials = (char) 0+Settings.getProperty("davmail.username")+ (char) 0 +Settings.getProperty("davmail.password"); + writeLine("AUTH PLAIN " + new String(new Base64().encode(credentials.getBytes()))); + assertEquals("235 OK Authenticated", socketReader.readLine()); + } + + + public void testCreateMessage() throws IOException, MessagingException { + + MimeMessage mimeMessage = new MimeMessage((Session) null); + mimeMessage.addHeader("To", Settings.getProperty("davmail.to")); + mimeMessage.setText("Test message"); + mimeMessage.setSubject("Test subject"); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + mimeMessage.writeTo(baos); + byte[] content = baos.toByteArray(); + writeLine("MAIL FROM:"+session.getEmail()); + readLine(); + writeLine("RCPT TO:"+Settings.getProperty("davmail.to")); + readLine(); + writeLine("DATA"); + assertEquals("354 Start mail input; end with .", readLine()); + writeLine(new String(content)); + writeLine("."); + assertEquals("250 Queued mail for delivery", readLine()); + } + + public void testQuit() throws IOException { + writeLine("QUIT"); + assertEquals("221 Closing connection", readLine()); + } + +}