view src/goodjava/mail/Examples.java @ 1795:745f756e719b

make copy_to recursive
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 02 Jan 2024 21:49:54 -0700
parents aed284cae1f0
children 92860f763715
line wrap: on
line source

package goodjava.mail;

import java.io.InputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.URL;
import java.util.Map;
import java.util.LinkedHashMap;


public class Examples {

	private static Smtp newSmtp() throws IOException, MailException {
		Socket socket = new Socket("smtpcorp.com",2525);
		Smtp smtp = new Smtp(socket);
		smtp.authenticate("smtp@luan.software","luanhost");
		return smtp;
	}

	private static void smtp() throws IOException, MailException {
		Smtp smtp = newSmtp();
		smtp.from("smtp@luan.software");
		smtp.to("fschmidt@gmail.com");
		String text = "\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();
	}

	private static void mailText() throws IOException, MailException {
		String text = ""
			+"testm 2\n"
			+"x\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\n"
		;
		Map<String,String> headers = new LinkedHashMap<String,String>();
		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");
		Message msg = new Message(headers,text);
		Smtp smtp = newSmtp();
		smtp.send(msg);
		smtp.close();
	}

	private static byte[] flag() throws IOException {
		URL url = new URL("https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Confederate_Rebel_Flag.svg/440px-Confederate_Rebel_Flag.svg.png");
		InputStream in = url.openConnection().getInputStream();
		byte[] a = new byte[100000];
		int n = 0;
		int i;
		while( (i=in.read(a,n,a.length-n)) != -1 )
			n += i;
		in.close();
		byte[] rtn = new byte[n];
		System.arraycopy(a,0,rtn,0,n);
		return rtn;
	}

	private static void mailFlag() throws IOException, MailException {
		byte[] flag = flag();
		Map<String,String> headers = new LinkedHashMap<String,String>();
		headers.put("From","smtp@luan.software");
		headers.put("To","fschmidt@gmail.com");
		headers.put("Subject","test flag");
		headers.put("Content-Type","image/png; name=\"flag.png\"");
		Message msg = new Message(headers,flag);
		Smtp smtp = newSmtp();
		smtp.send(msg);
		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();
	}
}