view src/goodjava/mail/Examples.java @ 1584:d3728e3e5af3

mail work
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 11 Mar 2021 01:22:20 -0700
parents
children c0ef8acf069d
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"
			+"产品\n"  // some random chinese
			+"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();
	}

	public static void main(String[] args) throws Exception {
		mailText();
	}
}