All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.mchange.net.SmtpUtils Maven / Gradle / Ivy
package com.mchange.net;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.net.InetAddress;
import java.util.Enumeration;
import java.util.Properties;
import com.mchange.io.OutputStreamUtils;
import com.mchange.io.ReaderUtils;
import com.mchange.net.SocketUtils;
public final class SmtpUtils
{
private final static String ENC = "8859_1";
private final static String CRLF = "\r\n";
private final static String CHARSET = "charset";
private final static int CHARSET_LEN = CHARSET.length();
public final static int DEFAULT_SMTP_PORT = 25;
public static void sendMail(InetAddress smtpHost, int smtpPort,
String mailFrom, String[] rcptTo,
Properties headers,
byte[] bodyBytes)
throws IOException, SmtpException
{
Socket s = null;
DataOutputStream dout = null;
BufferedReader br = null;
try
{
s = new Socket(smtpHost, smtpPort);
dout = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
br = new BufferedReader(new InputStreamReader(s.getInputStream(), ENC));
ensureResponse(br, 200, 300);
dout.writeBytes("HELO " + s.getLocalAddress().getHostName() + CRLF);
//System.out.print("HELO " + s.getLocalAddress().getHostName() + CRLF);
dout.flush();
ensureResponse(br, 200, 300);
dout.writeBytes("MAIL FROM: " + mailFrom + CRLF);
//System.out.print("MAIL FROM: " + mailFrom + CRLF);
dout.flush();
ensureResponse(br, 200, 300);
for (int i = rcptTo.length; --i >= 0;)
{
dout.writeBytes("RCPT TO: " + rcptTo[i] + CRLF);
//System.out.print("RCPT TO: " + rcptTo[i] + CRLF);
dout.flush();
ensureResponse(br, 200, 300);
}
dout.writeBytes("DATA" + CRLF);
//System.out.print("DATA" + CRLF);
dout.flush();
ensureResponse(br, 300, 400);
for (Enumeration e = headers.keys(); e.hasMoreElements();)
{
String key = (String) e.nextElement();
String value = headers.getProperty(key);
dout.writeBytes(key + ": " + value + CRLF);
}
dout.writeBytes(CRLF);
dout.write(bodyBytes);
dout.writeBytes(CRLF + '.' + CRLF);
dout.flush();
ensureResponse(br, 200, 300);
dout.writeBytes("QUIT" + CRLF);
dout.flush();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
throw new InternalError(ENC + " not supported???");
}
finally
{
OutputStreamUtils.attemptClose(dout);
ReaderUtils.attemptClose(br);
SocketUtils.attemptClose(s);
}
}
private static String encodingFromContentType(String contentType)
{
int cs_index = contentType.indexOf(CHARSET);
if (cs_index >= 0)
{
String cs = contentType.substring(cs_index + CHARSET_LEN);
cs = cs.trim();
//is this a good way to handle some bizarre situation
//where charset shows up other than as an attribute?
if (cs.charAt(0) != '=') return encodingFromContentType(cs);
cs = cs.substring(1).trim();
int semidex = cs.indexOf(';');
if (semidex >= 0)
cs = cs.substring(0, semidex);
return cs;
}
else return null;
}
private static byte[] bytesFromBodyString(String body, String enc) throws UnsupportedEncodingException
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos, enc));
pw.print(body);
pw.flush();
return baos.toByteArray();
}
private static void ensureResponse(BufferedReader br, int begin_inclusive, int end_exclusive)
throws IOException, SmtpException
{
String smtpRespStr;
int smtp_resp_num;
smtpRespStr = br.readLine();
//System.out.println(smtpRespStr);
try
{
smtp_resp_num = Integer.parseInt(smtpRespStr.substring(0,3));
while (smtpRespStr.charAt(3) == '-') smtpRespStr = br.readLine();
if (smtp_resp_num < begin_inclusive || smtp_resp_num >= end_exclusive)
throw new SmtpException(smtp_resp_num, smtpRespStr);
}
catch (NumberFormatException e)
{throw new SmtpException("Bad SMTP response while mailing document!");}
}
public static void main(String[] argv)
{
try
{
InetAddress smtpHost = InetAddress.getByName("mailhub.mchange.com");
int smtpPort = 25;
String mailFrom = "[email protected] ";
String[] rcptTo = {"[email protected] ", "[email protected] "};
Properties props = new Properties();
props.put("From", "[email protected] ");
props.put("To", "[email protected] ");
props.put("Subject", "Test test test AGAIN...");
byte[] bodyBytes = "This is a test AGAIN! Imagine that!".getBytes(ENC);
sendMail(smtpHost, smtpPort, mailFrom, rcptTo, props, bodyBytes);
}
catch (Exception e)
{e.printStackTrace();}
}
private SmtpUtils()
{}
}