📄 mailer.java
字号:
// Mailer.java - A class for e-mailing.
//
// Copyright (C) 2000-2002 Smart Software Consulting
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//
// Smart Software Consulting
// 1688 Silverwood Court
// Danville, CA 94526-3079
// USA
//
// http://www.smartsc.com
//
package com.smartsc.util;
import java.io.BufferedOutputStream;
import java.io.InputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.Hashtable;
public class Mailer
{
public static void send( Hashtable headers, File f)
throws IOException
{
send( headers, new FileInputStream( f));
}
public static void send( Hashtable headers, InputStream is)
throws IOException
{
OutputStream os =
new BufferedOutputStream(
send( headers), BUFFER_SIZE);
// Send input stream
byte[] buf = new byte[512];
for( int bytesRead = is.read( buf);
bytesRead != -1;
bytesRead = is.read( buf) )
{
os.write( buf, 0, bytesRead);
}
// Close streams
is.close();
os.close();
}
public static OutputStream send( Hashtable headers)
throws java.io.IOException
{
URL url;
try
{
url = new URL( "mailto", null, (String)headers.get( HEADER_TO));
}
catch( NullPointerException npe)
{
throw new MalformedURLException( "No recipient specified.");
}
URLConnection urlConn = url.openConnection();
urlConn.setDoOutput( true);
urlConn.connect();
OutputStream os = urlConn.getOutputStream();
// Send headers
Enumeration enum = headers.keys();
while( enum.hasMoreElements())
{
String header = (String)enum.nextElement();
if( !HEADER_TO.equals( header ))
{
byte[] headerBytes = header.getBytes();
byte[] valueBytes = ((String)headers.get( header)).getBytes();
os.write( headerBytes);
os.write( HEADER_SEPARATOR);
os.write( valueBytes);
os.write( CRLF);
}
}
os.write( CRLF);
return os;
}
public static final int BUFFER_SIZE = 512;
public static final byte[] HEADER_SEPARATOR = { (byte)':', (byte)' ' };
public static final byte[] CRLF = { (byte)'\r', (byte)'\n' };
public static final String HEADER_FROM = "From";
public static final String HEADER_TO = "To";
public static final String HEADER_CC = "Cc";
public static final String HEADER_BCC = "Bcc";
public static final String HEADER_SUBJECT = "Subject";
public static final String HEADER_DATE = "Date";
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -