Mercurial Hosting > luan
changeset 1583:1cc6c7fa803d
mail work
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 07 Mar 2021 02:22:09 -0700 |
parents | f28cc30d56cb |
children | d3728e3e5af3 |
files | src/goodjava/mail/Message.java src/goodjava/mail/Smtp.java src/goodjava/util/GoodUtils.java |
diffstat | 3 files changed, 130 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
diff -r f28cc30d56cb -r 1cc6c7fa803d src/goodjava/mail/Message.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goodjava/mail/Message.java Sun Mar 07 02:22:09 2021 -0700 @@ -0,0 +1,76 @@ +package goodjava.mail; + +import java.util.Map; +import java.util.LinkedHashMap; +import java.util.regex.Pattern; +import java.util.regex.Matcher; +import goodjava.util.GoodUtils; + +import java.net.Socket; + + +public class Message { + public final Map<String,String> headers = new LinkedHashMap<String,String>(); + public final String content; + public final String contentType; + private static Pattern line = Pattern.compile("(?m)^.*$"); + + public Message(String content) { + this(content,"text/plain"); + } + + public Message(String content,String contentType) { + this.content = content; + this.contentType = contentType; + } + + public String toText() { + StringBuilder sb = new StringBuilder(); + for( Map.Entry<String,String> entry : headers.entrySet() ) { + String name = entry.getKey(); + if( name.equalsIgnoreCase("bcc") ) + continue; + String value = entry.getValue(); + sb.append( name ).append( ": " ).append( value ).append( "\r\n" ); + } + sb.append( "Content-Type: " ).append( contentType ).append( "; charset=\"UTF-8\"\r\n" ); + boolean isAscii = content.matches("\\p{ASCII}*"); + if( !isAscii ) + sb.append( "Content-Transfer-Encoding: base64\r\n" ); + sb.append( "\r\n" ); + if( isAscii ) { + Matcher m = line.matcher(content); + while( m.find() ) { + sb.append(m.group()).append("\r\n"); + } + } else { + String s = GoodUtils.base64Encode(content); + int n = s.length() - 76; + int i; + for( i=0; i<n; i+=76 ) { + sb.append(s.substring(i,i+76)).append("\r\n"); + } + sb.append(s.substring(i)).append("\r\n"); + } + return sb.toString(); + } + + public static void main(String[] args) throws Exception { + String text = "" + +"testm 2\n" + +"x\n" +// +"产品\n" + +"rg; ;lrg dsl rgj errlgerrg neskrjg skrg rdsg drskrg sd;gr s;kgr skrg skrg sdg ds fg;ks gegr erg ;sg sd; g;sdr gsklrg sg s;kkrg s;hg ;slrg ;elrg ;reg r;g ;r g;er g;ler g;e g; g;r g rg; srkd fjl kj kklsjrg lsk gskdf;rs gkrj glj grekjs lksjgkjn kjslg rklrg ;rsd; kj drsg akrglk kalrgklrsdnrgkgj;r ;s ns b;n;sn ;njslk r;n\r\n" + ; + Message msg = new Message(text); + msg.headers.put("From","smtp@luan.software"); + msg.headers.put("To","fschmidt@gmail.com"); + msg.headers.put("Subject","test"); + Socket socket = new Socket("smtpcorp.com",2525); + Smtp smtp = new Smtp(socket); + smtp.authenticate("smtp@luan.software","luanhost"); + smtp.send(msg); + smtp.close(); + System.out.println("done msg"); + } +}
diff -r f28cc30d56cb -r 1cc6c7fa803d src/goodjava/mail/Smtp.java --- a/src/goodjava/mail/Smtp.java Sat Mar 06 21:13:34 2021 -0700 +++ b/src/goodjava/mail/Smtp.java Sun Mar 07 02:22:09 2021 -0700 @@ -6,7 +6,8 @@ import java.io.OutputStreamWriter; import java.io.IOException; import java.net.Socket; -import java.util.Base64; +import java.util.Map; +import goodjava.util.GoodUtils; public class Smtp { @@ -29,6 +30,35 @@ throw new SmtpException(ehlo); } + public String authenticate(String username,String password) throws IOException, SmtpException { + String s = "\0" + username + "\0" + password; + s = GoodUtils.base64Encode(s); + write( "AUTH PLAIN " + s + "\r\n" ); + String r = read(); + if( !r.startsWith("235") ) + throw new SmtpException(r); + return r; + } + + public void send(Message msg) throws IOException, SmtpException { + for( Map.Entry<String,String> entry : msg.headers.entrySet() ) { + String name = entry.getKey(); + String value = entry.getValue(); + if( name.equalsIgnoreCase("from") ) { + from(value); + } + if( name.equalsIgnoreCase("to") + || name.equalsIgnoreCase("cc") + || name.equalsIgnoreCase("bcc") + ) { + for( String s : value.split(",") ) { + to(s); + } + } + } + data( msg.toText() ); + } + public void close() throws IOException, SmtpException { write( "QUIT\r\n" ); String s = read(); @@ -37,16 +67,6 @@ socket.close(); } - public String authenticate(String username,String password) throws IOException, SmtpException { - String s = "\0" + username + "\0" + password; - s = Base64.getEncoder().encodeToString(s.getBytes()); - write( "AUTH PLAIN " + s + "\r\n" ); - String r = read(); - if( !r.startsWith("235") ) - throw new SmtpException(r); - return r; - } - public String from(String address) throws IOException, SmtpException { write( "MAIL FROM: " + address + "\r\n" ); String r = read(); @@ -93,11 +113,12 @@ Smtp smtp = new Smtp(socket); smtp.authenticate("smtp@luan.software","luanhost"); smtp.from("smtp@luan.software"); - smtp.to("fschmidt@gmail.com"); + smtp.to(" fschmidt@gmail.com"); String text = "\r\n" - +"test2\r\n" + +"test3\r\n" +".q\r\n" +"x\r\n" + +"rg; ;lrg dsl rgj errlgerrg neskrjg skrg rdsg drskrg sd;gr s;kgr skrg skrg sdg ds fg;ks gegr erg ;sg sd; g;sdr gsklrg sg s;kkrg s;hg ;slrg ;elrg ;reg r;g ;r g;er g;ler g;e g; g;r g rg; srkd fjl kj kklsjrg lsk gskdf;rs gkrj glj grekjs lksjgkjn kjslg rklrg ;rsd; kj drsg akrglk kalrgklrsdnrgkgj;r ;s ns b;n;sn ;njslk r;n\r\n" ; smtp.data(text); smtp.close();
diff -r f28cc30d56cb -r 1cc6c7fa803d src/goodjava/util/GoodUtils.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/goodjava/util/GoodUtils.java Sun Mar 07 02:22:09 2021 -0700 @@ -0,0 +1,20 @@ +package goodjava.util; + +import java.io.UnsupportedEncodingException; +import java.util.Base64; + + +public final class GoodUtils { + + public static byte[] getBytes(String s,String charsetName) { + try { + return s.getBytes(charsetName); + } catch(UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + } + + public static String base64Encode(String s) { + return Base64.getEncoder().encodeToString(getBytes(s,"UTF-8")); + } +}