mirror of
https://github.com/moparisthebest/davmail
synced 2024-12-13 19:22:22 -05:00
Added BCC (blind carbon copy) support
git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@222 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
parent
4f4eb2fb9a
commit
6dc0eb450a
@ -13,6 +13,7 @@ import org.apache.webdav.lib.Property;
|
|||||||
import org.apache.webdav.lib.ResponseEntity;
|
import org.apache.webdav.lib.ResponseEntity;
|
||||||
import org.apache.webdav.lib.WebdavResource;
|
import org.apache.webdav.lib.WebdavResource;
|
||||||
import org.apache.webdav.lib.methods.SearchMethod;
|
import org.apache.webdav.lib.methods.SearchMethod;
|
||||||
|
import org.apache.webdav.lib.methods.PropPatchMethod;
|
||||||
import org.htmlcleaner.HtmlCleaner;
|
import org.htmlcleaner.HtmlCleaner;
|
||||||
import org.htmlcleaner.TagNode;
|
import org.htmlcleaner.TagNode;
|
||||||
|
|
||||||
@ -384,11 +385,12 @@ public class ExchangeSession {
|
|||||||
* Create message in current folder
|
* Create message in current folder
|
||||||
*
|
*
|
||||||
* @param subject message subject line
|
* @param subject message subject line
|
||||||
|
* @param bcc blind carbon copy header
|
||||||
* @param messageBody mail body
|
* @param messageBody mail body
|
||||||
* @throws java.io.IOException when unable to create message
|
* @throws java.io.IOException when unable to create message
|
||||||
*/
|
*/
|
||||||
public void createMessage(String subject, String messageBody) throws IOException {
|
public void createMessage(String subject, String bcc, String messageBody) throws IOException {
|
||||||
createMessage(currentFolderUrl, subject, messageBody);
|
createMessage(currentFolderUrl, subject, bcc, messageBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -397,14 +399,14 @@ public class ExchangeSession {
|
|||||||
*
|
*
|
||||||
* @param folderUrl Exchange folder URL
|
* @param folderUrl Exchange folder URL
|
||||||
* @param subject message subject line
|
* @param subject message subject line
|
||||||
|
* @param bcc blind carbon copy header
|
||||||
* @param messageBody mail body
|
* @param messageBody mail body
|
||||||
* @throws java.io.IOException when unable to create message
|
* @throws java.io.IOException when unable to create message
|
||||||
*/
|
*/
|
||||||
public void createMessage(String folderUrl, String subject, String messageBody) throws IOException {
|
public void createMessage(String folderUrl, String subject, String bcc, String messageBody) throws IOException {
|
||||||
String messageUrl = URIUtil.encodePathQuery(folderUrl + "/" + subject + ".EML");
|
String messageUrl = URIUtil.encodePathQuery(folderUrl + "/" + subject + ".EML");
|
||||||
|
|
||||||
PutMethod putmethod = new PutMethod(messageUrl);
|
PutMethod putmethod = new PutMethod(messageUrl);
|
||||||
// TODO : test, bcc ?
|
|
||||||
putmethod.setRequestHeader("Translate", "f");
|
putmethod.setRequestHeader("Translate", "f");
|
||||||
putmethod.setRequestHeader("Content-Type", "message/rfc822");
|
putmethod.setRequestHeader("Content-Type", "message/rfc822");
|
||||||
InputStream bodyStream = null;
|
InputStream bodyStream = null;
|
||||||
@ -429,6 +431,28 @@ public class ExchangeSession {
|
|||||||
}
|
}
|
||||||
putmethod.releaseConnection();
|
putmethod.releaseConnection();
|
||||||
}
|
}
|
||||||
|
// update message with blind carbon copy
|
||||||
|
if (bcc != null && bcc.length() > 0) {
|
||||||
|
PropPatchMethod patchMethod = new PropPatchMethod(messageUrl);
|
||||||
|
try {
|
||||||
|
patchMethod.addPropertyToSet("bcc", bcc, "b", "urn:schemas:mailheader:");
|
||||||
|
int statusCode = wdr.retrieveSessionInstance().executeMethod(patchMethod);
|
||||||
|
if (statusCode != HttpStatus.SC_MULTI_STATUS) {
|
||||||
|
throw new IOException("Unable to add bcc recipients: " + bcc);
|
||||||
|
}
|
||||||
|
Enumeration responseEntityEnum = patchMethod.getResponses();
|
||||||
|
|
||||||
|
if (responseEntityEnum.hasMoreElements()) {
|
||||||
|
ResponseEntity entity = (ResponseEntity) responseEntityEnum.nextElement();
|
||||||
|
if (entity.getStatusCode() != HttpStatus.SC_OK) {
|
||||||
|
throw new IOException("Unable to add bcc recipients: " + bcc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} finally {
|
||||||
|
patchMethod.releaseConnection();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Message buildMessage(ResponseEntity responseEntity) throws URIException {
|
protected Message buildMessage(ResponseEntity responseEntity) throws URIException {
|
||||||
@ -555,31 +579,64 @@ public class ExchangeSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendMessage(BufferedReader reader) throws IOException {
|
public void sendMessage(List<String> recipients, BufferedReader reader) throws IOException {
|
||||||
String subject = "davmailtemp";
|
String subject = "davmailtemp";
|
||||||
String line = reader.readLine();
|
String line = reader.readLine();
|
||||||
StringBuffer mailBuffer = new StringBuffer();
|
StringBuilder mailBuffer = new StringBuilder();
|
||||||
while (line != null && !".".equals(line)) {
|
StringBuilder recipientBuffer = new StringBuilder();
|
||||||
|
boolean inHeader = true;
|
||||||
|
boolean inRecipientHeader = false;
|
||||||
|
while (!".".equals(line)) {
|
||||||
mailBuffer.append(line);
|
mailBuffer.append(line);
|
||||||
mailBuffer.append("\n");
|
mailBuffer.append("\n");
|
||||||
line = reader.readLine();
|
line = reader.readLine();
|
||||||
|
|
||||||
if (line != null) {
|
if (inHeader && line.length() == 0) {
|
||||||
// patch thunderbird html in reply for correct outlook display
|
inHeader = false;
|
||||||
if (line.startsWith("<head>")) {
|
}
|
||||||
line += "\n <style> blockquote { display: block; margin: 1em 0px; padding-left: 1em; border-left: solid; border-color: blue; border-width: thin;}</style>";
|
|
||||||
} else if (line.startsWith("Subject")) {
|
inRecipientHeader = inRecipientHeader && line.startsWith(" ");
|
||||||
subject = MimeUtility.decodeText(line.substring(8).trim());
|
|
||||||
// '/' is invalid as message URL
|
if ((inHeader && line.length() >= 3) || inRecipientHeader) {
|
||||||
subject = subject.replaceAll("/", "_xF8FF_");
|
String prefix = line.substring(0, 3).toLowerCase();
|
||||||
// '?' is also invalid
|
if ("to:".equals(prefix) || "cc:".equals(prefix) || inRecipientHeader) {
|
||||||
subject = subject.replaceAll("\\?", "");
|
inRecipientHeader = true;
|
||||||
// TODO : test & in subject
|
recipientBuffer.append(line);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// patch thunderbird html in reply for correct outlook display
|
||||||
|
if (line.startsWith("<head>")) {
|
||||||
|
line += "\n <style> blockquote { display: block; margin: 1em 0px; padding-left: 1em; border-left: solid; border-color: blue; border-width: thin;}</style>";
|
||||||
|
} else if (line.startsWith("Subject")) {
|
||||||
|
subject = MimeUtility.decodeText(line.substring(8).trim());
|
||||||
|
// '/' is invalid as message URL
|
||||||
|
subject = subject.replaceAll("/", "_xF8FF_");
|
||||||
|
// '?' is also invalid
|
||||||
|
subject = subject.replaceAll("\\?", "");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// remove visible recipients from list
|
||||||
|
List<String> visibleRecipients = new ArrayList<String>();
|
||||||
|
for (String recipient : recipients) {
|
||||||
|
if (recipientBuffer.indexOf(recipient) >= 0) {
|
||||||
|
visibleRecipients.add(recipient);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
recipients.removeAll(visibleRecipients);
|
||||||
|
|
||||||
|
StringBuffer bccBuffer = new StringBuffer();
|
||||||
|
for (String recipient : recipients) {
|
||||||
|
if (bccBuffer.length() > 0) {
|
||||||
|
bccBuffer.append(',');
|
||||||
|
}
|
||||||
|
bccBuffer.append("<");
|
||||||
|
bccBuffer.append(recipient);
|
||||||
|
bccBuffer.append(">");
|
||||||
}
|
}
|
||||||
|
|
||||||
createMessage(draftsUrl, subject, mailBuffer.toString());
|
createMessage(draftsUrl, subject,
|
||||||
|
bccBuffer.toString()
|
||||||
|
, mailBuffer.toString());
|
||||||
|
|
||||||
// warning : slide library expects *unencoded* urls
|
// warning : slide library expects *unencoded* urls
|
||||||
String tempUrl = draftsUrl + "/" + subject + ".eml";
|
String tempUrl = draftsUrl + "/" + subject + ".eml";
|
||||||
|
@ -4,11 +4,15 @@ import davmail.AbstractConnection;
|
|||||||
import davmail.exchange.ExchangeSessionFactory;
|
import davmail.exchange.ExchangeSessionFactory;
|
||||||
import davmail.tray.DavGatewayTray;
|
import davmail.tray.DavGatewayTray;
|
||||||
|
|
||||||
|
import javax.mail.internet.InternetAddress;
|
||||||
|
import javax.mail.internet.AddressException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.Socket;
|
import java.net.Socket;
|
||||||
import java.net.SocketException;
|
import java.net.SocketException;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.StringTokenizer;
|
import java.util.StringTokenizer;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Dav Gateway smtp connection implementation
|
* Dav Gateway smtp connection implementation
|
||||||
@ -30,6 +34,7 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
public void run() {
|
public void run() {
|
||||||
String line;
|
String line;
|
||||||
StringTokenizer tokens;
|
StringTokenizer tokens;
|
||||||
|
List<String> recipients = new ArrayList<String>();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
ExchangeSessionFactory.checkConfig();
|
ExchangeSessionFactory.checkConfig();
|
||||||
@ -83,6 +88,7 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
} else if ("MAIL".equals(command)) {
|
} else if ("MAIL".equals(command)) {
|
||||||
if (state == AUTHENTICATED) {
|
if (state == AUTHENTICATED) {
|
||||||
state = STARTMAIL;
|
state = STARTMAIL;
|
||||||
|
recipients.clear();
|
||||||
sendClient("250 Sender OK");
|
sendClient("250 Sender OK");
|
||||||
} else {
|
} else {
|
||||||
state = INITIAL;
|
state = INITIAL;
|
||||||
@ -90,8 +96,19 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
}
|
}
|
||||||
} else if ("RCPT".equals(command)) {
|
} else if ("RCPT".equals(command)) {
|
||||||
if (state == STARTMAIL || state == RECIPIENT) {
|
if (state == STARTMAIL || state == RECIPIENT) {
|
||||||
state = RECIPIENT;
|
if (line.startsWith("RCPT TO:")) {
|
||||||
sendClient("250 Recipient OK");
|
state = RECIPIENT;
|
||||||
|
try {
|
||||||
|
InternetAddress internetAddress = new InternetAddress(line.substring("RCPT TO:".length()));
|
||||||
|
recipients.add(internetAddress.getAddress());
|
||||||
|
} catch (AddressException e) {
|
||||||
|
throw new IOException("Invalid recipient: "+line);
|
||||||
|
}
|
||||||
|
sendClient("250 Recipient OK");
|
||||||
|
} else {
|
||||||
|
sendClient("500 Unrecognized command");
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
state = AUTHENTICATED;
|
state = AUTHENTICATED;
|
||||||
sendClient("503 Bad sequence of commands");
|
sendClient("503 Bad sequence of commands");
|
||||||
@ -102,7 +119,7 @@ public class SmtpConnection extends AbstractConnection {
|
|||||||
sendClient("354 Start mail input; end with <CRLF>.<CRLF>");
|
sendClient("354 Start mail input; end with <CRLF>.<CRLF>");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
session.sendMessage(in);
|
session.sendMessage(recipients, in);
|
||||||
state = AUTHENTICATED;
|
state = AUTHENTICATED;
|
||||||
sendClient("250 Queued mail for delivery");
|
sendClient("250 Queued mail for delivery");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user