changeset 1587:fa1a9aceac3e

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 23:22:53 -0700
parents fcca0ddf5a4d
children 0b904d30721f
files src/goodjava/mail/Examples.java src/goodjava/mail/Message.java src/luan/modules/mail/MailCon.java
diffstat 3 files changed, 43 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/mail/Examples.java	Fri Mar 12 20:12:43 2021 -0700
+++ b/src/goodjava/mail/Examples.java	Fri Mar 12 23:22:53 2021 -0700
@@ -42,7 +42,7 @@
 		headers.put("From","smtp@luan.software");
 		headers.put("To","fschmidt@gmail.com");
 		headers.put("Subject","test text");
-		headers.put("Content-Type","text/plain; charset=utf-8");
+//		headers.put("Content-Type","text/plain; charset=utf-8");
 		Message msg = new Message(headers,text);
 		Smtp smtp = newSmtp();
 		smtp.send(msg);
@@ -124,6 +124,6 @@
 	}
 
 	public static void main(String[] args) throws Exception {
-		multi();
+		mailText();
 	}
 }
--- a/src/goodjava/mail/Message.java	Fri Mar 12 20:12:43 2021 -0700
+++ b/src/goodjava/mail/Message.java	Fri Mar 12 23:22:53 2021 -0700
@@ -13,7 +13,17 @@
 	final Object content;
 	private static Pattern line = Pattern.compile("(?m)^.*$");
 
-	public Message(Map<String,String> headers,Object content) {
+	public Message(Map<String,String> headers,String content) {
+		this.headers = headers;
+		this.content = content;
+	}
+
+	public Message(Map<String,String> headers,byte[] content) {
+		this.headers = headers;
+		this.content = content;
+	}
+
+	public Message(Map<String,String> headers,Message[] content) {
 		this.headers = headers;
 		this.content = content;
 	}
@@ -42,11 +52,10 @@
 			}
 			sb.append( name ).append( ": " ).append( value ).append( "\r\n" );
 		}
-		if( contentType==null )
-			throw new MailException("Content-Type not defined");
 		if( content instanceof String ) {
 			String s = (String)content;
-			sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
+			if( contentType!=null )
+				sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
 			boolean isAscii = s.matches("\\p{ASCII}*");
 			if( !isAscii )
 				sb.append( "Content-Transfer-Encoding: base64\r\n" );
@@ -60,11 +69,14 @@
 				addBase64( sb, GoodUtils.getBytes(s,"UTF-8") );
 			}
 		} else if( content instanceof byte[] ) {
-			sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
+			if( contentType!=null )
+				sb.append( "Content-Type: " ).append( contentType ).append( "\r\n" );
 			sb.append( "Content-Transfer-Encoding: base64\r\n" );
 			sb.append( "\r\n" );
 			addBase64( sb, (byte[])content );
 		} else if( content instanceof Message[] ) {
+			if( contentType==null )
+				throw new MailException("Content-Type must be defined for multipart");
 			Message[] messages = (Message[])content;
 			String[] texts = new String[messages.length];
 			StringBuilder allTextSb = new StringBuilder();
@@ -88,7 +100,7 @@
 			}
 			sb.append( "--" ).append( boundary ).append( "--\r\n" );
 		} else
-			throw new MailException("content is unrecognized type: "+content.getClass());
+			throw new RuntimeException();
 		return sb.toString();
 	}
 }
--- a/src/luan/modules/mail/MailCon.java	Fri Mar 12 20:12:43 2021 -0700
+++ b/src/luan/modules/mail/MailCon.java	Fri Mar 12 23:22:53 2021 -0700
@@ -60,20 +60,6 @@
 		Object body = mailParams.remove("body");
 		if( body == null )
 			throw new LuanException( "parameter 'body' is required" );
-		if( body instanceof LuanTable ) {
-			LuanTable tbl = (LuanTable)body;
-			if( !tbl.isList() )
-				throw new LuanException( "body table must be a list" );
-			List list = tbl.asList();
-			Message[] msgs = new Message[list.size()];
-			for( int i=0; i<msgs.length; i++ ) {
-				Object obj = list.get(i);
-				if( !(obj instanceof LuanTable) )
-					throw new LuanException( "body table must be a list of part tables" );
-				msgs[i] = message((LuanTable)obj);
-			}
-			body = msgs;
-		}
 		Map<String,String> headers = new LinkedHashMap<String,String>();
 		boolean hasContentType = false;
 		for( Map.Entry<Object,Object> entry : mailParams.entrySet() ) {
@@ -88,9 +74,29 @@
 			if( name.equalsIgnoreCase("content-type") )
 				hasContentType = true;
 		}
-		if( !hasContentType && body instanceof String )
-			headers.put("Content-Type","text/plain; charset=utf-8");
-		return new Message(headers,body);
+		if( body instanceof String ) {
+			if( !hasContentType )
+				headers.put("Content-Type","text/plain; charset=utf-8");
+			return new Message(headers,(String)body);
+		}
+		if( body instanceof byte[] ) {
+			return new Message(headers,(byte[])body);
+		}
+		if( body instanceof LuanTable ) {
+			LuanTable tbl = (LuanTable)body;
+			if( !tbl.isList() )
+				throw new LuanException( "body table must be a list" );
+			List list = tbl.asList();
+			Message[] msgs = new Message[list.size()];
+			for( int i=0; i<msgs.length; i++ ) {
+				Object obj = list.get(i);
+				if( !(obj instanceof LuanTable) )
+					throw new LuanException( "body table must be a list of part tables" );
+				msgs[i] = message((LuanTable)obj);
+			}
+			return new Message(headers,msgs);
+		}
+		throw new LuanException("body must be a string, binary, or list of part tables");
 	}
 
 	public void send(LuanTable mailTbl) throws LuanException, IOException, MailException {