changeset 1585:c0ef8acf069d

multipart mail
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 18:06:15 -0700
parents d3728e3e5af3
children fcca0ddf5a4d
files src/goodjava/mail/Examples.java src/goodjava/mail/Message.java
diffstat 2 files changed, 75 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
diff -r d3728e3e5af3 -r c0ef8acf069d src/goodjava/mail/Examples.java
--- a/src/goodjava/mail/Examples.java	Thu Mar 11 01:22:20 2021 -0700
+++ b/src/goodjava/mail/Examples.java	Fri Mar 12 18:06:15 2021 -0700
@@ -76,7 +76,54 @@
 		smtp.close();
 	}
 
+	private static void multi() throws IOException, MailException {
+		Message msg1;
+		{
+			String text = ""
+				+"testing 1\n"
+				+"more\n"
+			;
+			Map<String,String> headers = new LinkedHashMap<String,String>();
+			headers.put("Content-Type","text/plain; charset=utf-8");
+			msg1 = new Message(headers,text);
+		}
+
+		Message msg2;
+		{
+			String text = ""
+				+"testing 2\n"
+				+"more\n"
+			;
+			Map<String,String> headers = new LinkedHashMap<String,String>();
+			headers.put("Content-Type","text/plain; charset=utf-8");
+			headers.put("Content-Disposition","attachment; filename=\"t2.txt\"");
+			msg2 = new Message(headers,text);
+		}
+
+		Message msg3;
+		{
+			byte[] flag = flag();
+			Map<String,String> headers = new LinkedHashMap<String,String>();
+			headers.put("Content-Type","image/png; name=\"flag.png\"");
+//			headers.put("Content-Type","image/png");
+//			headers.put("Content-Disposition","inline");
+//			headers.put("Content-Disposition","attachment; filename=\"flag.png\"");
+			msg3 = new Message(headers,flag);
+		}
+
+		Map<String,String> headers = new LinkedHashMap<String,String>();
+		headers.put("From","smtp@luan.software");
+		headers.put("To","fschmidt@gmail.com");
+		headers.put("Subject","multi");
+		headers.put("Content-Type","multipart/mixed");
+		Message[] content = new Message[]{msg1,msg2,msg3};
+		Message msg = new Message(headers,content);
+		Smtp smtp = newSmtp();
+		smtp.send(msg);
+		smtp.close();
+	}
+
 	public static void main(String[] args) throws Exception {
-		mailText();
+		multi();
 	}
 }
diff -r d3728e3e5af3 -r c0ef8acf069d src/goodjava/mail/Message.java
--- a/src/goodjava/mail/Message.java	Thu Mar 11 01:22:20 2021 -0700
+++ b/src/goodjava/mail/Message.java	Fri Mar 12 18:06:15 2021 -0700
@@ -2,14 +2,15 @@
 
 import java.util.Map;
 import java.util.Base64;
+import java.util.Random;
 import java.util.regex.Pattern;
 import java.util.regex.Matcher;
 import goodjava.util.GoodUtils;
 
 
 public class Message {
-	public final Map<String,String> headers;
-	public final Object content;
+	final Map<String,String> headers;
+	final Object content;
 	private static Pattern line = Pattern.compile("(?m)^.*$");
 
 	public Message(Map<String,String> headers,Object content) {
@@ -63,8 +64,31 @@
 			sb.append( "Content-Transfer-Encoding: base64\r\n" );
 			sb.append( "\r\n" );
 			addBase64( sb, (byte[])content );
+		} else if( content instanceof Message[] ) {
+			Message[] messages = (Message[])content;
+			String[] texts = new String[messages.length];
+			StringBuilder allTextSb = new StringBuilder();
+			for( int i=0; i<messages.length; i++ ) {
+				String text = messages[i].toText();
+				texts[i] = text;
+				allTextSb.append(text);
+			}
+			String allText = allTextSb.toString();
+			String boundary;
+			do {
+				boundary = Long.toHexString(new Random().nextLong());
+			} while( allText.contains(boundary) );
+			sb.append( "Content-Type: " ).append( contentType )
+				.append( "; boundary=\"" ).append( boundary ).append( "\"\r\n" );
+			sb.append( "\r\n" );
+			for( String text : texts ) {
+				sb.append( "--" ).append( boundary ).append( "\r\n" );
+				sb.append( text );
+				sb.append( "\r\n" );
+			}
+			sb.append( "--" ).append( boundary ).append( "--\r\n" );
 		} else
-			throw new MailException("content is unrecognized type");
+			throw new MailException("content is unrecognized type: "+content.getClass());
 		return sb.toString();
 	}
 }