diff src/goodjava/mail/Message.java @ 1587:fa1a9aceac3e

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 12 Mar 2021 23:22:53 -0700
parents c0ef8acf069d
children 0b904d30721f
line wrap: on
line diff
--- 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();
 	}
 }