package com.hotmail.walksing.module.mail;import java.io.*;
import java.util.*;import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.activation.DataHandler;
/**
* Used for sending email via the SMTP protocol. This class is a simple
* wrapper for the javax.mail API.
**/
public class SMTPMail
{
private static final String INBOX = "INBOX", POP_MAIL="pop3", SMTP_MAIL="smtp"; private int smtpPort = 25,
timeout = 5*1000;
private String smtpHost = "localhost",
fromName = null,
fromAddress = null,
subject = null,
user = null,
password = null;
private boolean debug = false, verify = true;
private Hashtable to = new Hashtable(),
cc = new Hashtable(),
bcc = new Hashtable(),
replyTo= new Hashtable();
private StringBuffer text= new StringBuffer();
private Vector anyLine = new Vector(2);
public static String error = ""; public SMTPMail()
{
} public SMTPMail(String host)
{
this(host, -1, null, null);
} public SMTPMail(String host, int port)
{
this(host, port, null, null);
} public SMTPMail(String host, int port, String user, String password)
{
smtpHost = host;
smtpPort = port;
this.user = user;
password = password;
} public void setUser (String value)
{
user = value;
} public void setPassword(String value)
{
password = value;
}
public void setVerify (boolean value)
{
verify = value;
}
public void setDebug (boolean value)
{
debug = value;
}
public void setTimeOut (int value)
{
timeout = value;
}
public void setFrom (String name, String address)
{
fromName = name;
fromAddress = address;
}
public void addTo (String name, String address)
{
to.put(name, address);
}
public void addCc (String name, String address)
{
cc.put(name, address);
}
public void addBcc (String name, String address)
{
bcc.put(name, address);
}
public void addReplyTo (String name, String address)
{
replyTo.put(name, address);
}
public void addAny (String completeLine)
{
anyLine.addElement(completeLine);
}
public void setSubject(String subject)
{
this.subject = subject;
}
public void addText (String text)
{
this.text.append(text);
}
private boolean sendAsHTML=false;
public void setSendAsHTML(boolean value)
{
sendAsHTML=value;
}
public void addFile (String filename) throws FileNotFoundException, IOException
{
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = null;
try
{
while ((line = br.readLine()) != null)
addText(line + "\n");
}
finally
{
try { br.close(); } catch (Exception e) {}
}
}
public void sendMail() throws Exception
{
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
Message newMessage = new MimeMessage(session);
// create a message
newMessage.setFrom(new InternetAddress(fromAddress, fromName));
InternetAddress iaa[] = getAddresses(replyTo);
if (iaa != null)
newMessage.setReplyTo(iaa); setRecipients(newMessage, Message.RecipientType.TO, to);
setRecipients(newMessage, Message.RecipientType.CC, cc);
setRecipients(newMessage, Message.RecipientType.BCC, bcc);
newMessage.setSubject(subject);
newMessage.setSentDate(new java.util.Date()); // Set message contents
if (!sendAsHTML)
{
newMessage.setText((String)text.toString());
}
else
{
newMessage.setHeader("Mime-Version", "1.0");
newMessage.setHeader("Content-Transfer-Encoding", "quoted-printable");
newMessage.setHeader("X-Mailer", "sendhtml");
newMessage.setHeader("Content-Type", "text/html"); StringBuffer sb = new StringBuffer();
sb.append(text.toString());
newMessage.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html")));
} newMessage.saveChanges(); // Send newMessage
Transport transport = session.getTransport(SMTP_MAIL);
if (user != null && password != null)
transport.connect(smtpHost, user, password);
else
transport.connect();
transport.send(newMessage);
}[continue...]
import java.util.*;import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.activation.DataHandler;
/**
* Used for sending email via the SMTP protocol. This class is a simple
* wrapper for the javax.mail API.
**/
public class SMTPMail
{
private static final String INBOX = "INBOX", POP_MAIL="pop3", SMTP_MAIL="smtp"; private int smtpPort = 25,
timeout = 5*1000;
private String smtpHost = "localhost",
fromName = null,
fromAddress = null,
subject = null,
user = null,
password = null;
private boolean debug = false, verify = true;
private Hashtable to = new Hashtable(),
cc = new Hashtable(),
bcc = new Hashtable(),
replyTo= new Hashtable();
private StringBuffer text= new StringBuffer();
private Vector anyLine = new Vector(2);
public static String error = ""; public SMTPMail()
{
} public SMTPMail(String host)
{
this(host, -1, null, null);
} public SMTPMail(String host, int port)
{
this(host, port, null, null);
} public SMTPMail(String host, int port, String user, String password)
{
smtpHost = host;
smtpPort = port;
this.user = user;
password = password;
} public void setUser (String value)
{
user = value;
} public void setPassword(String value)
{
password = value;
}
public void setVerify (boolean value)
{
verify = value;
}
public void setDebug (boolean value)
{
debug = value;
}
public void setTimeOut (int value)
{
timeout = value;
}
public void setFrom (String name, String address)
{
fromName = name;
fromAddress = address;
}
public void addTo (String name, String address)
{
to.put(name, address);
}
public void addCc (String name, String address)
{
cc.put(name, address);
}
public void addBcc (String name, String address)
{
bcc.put(name, address);
}
public void addReplyTo (String name, String address)
{
replyTo.put(name, address);
}
public void addAny (String completeLine)
{
anyLine.addElement(completeLine);
}
public void setSubject(String subject)
{
this.subject = subject;
}
public void addText (String text)
{
this.text.append(text);
}
private boolean sendAsHTML=false;
public void setSendAsHTML(boolean value)
{
sendAsHTML=value;
}
public void addFile (String filename) throws FileNotFoundException, IOException
{
BufferedReader br = new BufferedReader(new FileReader(filename));
String line = null;
try
{
while ((line = br.readLine()) != null)
addText(line + "\n");
}
finally
{
try { br.close(); } catch (Exception e) {}
}
}
public void sendMail() throws Exception
{
// create some properties and get the default Session
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
Message newMessage = new MimeMessage(session);
// create a message
newMessage.setFrom(new InternetAddress(fromAddress, fromName));
InternetAddress iaa[] = getAddresses(replyTo);
if (iaa != null)
newMessage.setReplyTo(iaa); setRecipients(newMessage, Message.RecipientType.TO, to);
setRecipients(newMessage, Message.RecipientType.CC, cc);
setRecipients(newMessage, Message.RecipientType.BCC, bcc);
newMessage.setSubject(subject);
newMessage.setSentDate(new java.util.Date()); // Set message contents
if (!sendAsHTML)
{
newMessage.setText((String)text.toString());
}
else
{
newMessage.setHeader("Mime-Version", "1.0");
newMessage.setHeader("Content-Transfer-Encoding", "quoted-printable");
newMessage.setHeader("X-Mailer", "sendhtml");
newMessage.setHeader("Content-Type", "text/html"); StringBuffer sb = new StringBuffer();
sb.append(text.toString());
newMessage.setDataHandler(new DataHandler(new ByteArrayDataSource(sb.toString(), "text/html")));
} newMessage.saveChanges(); // Send newMessage
Transport transport = session.getTransport(SMTP_MAIL);
if (user != null && password != null)
transport.connect(smtpHost, user, password);
else
transport.connect();
transport.send(newMessage);
}[continue...]
解决方案 »
- 数据库连接出现问题
- 菜鸟救命,怎么解决???????????????
- TreeSet的higher和lower方法报NoSuchMethodError错!
- jxls通过模板生成excel,数字为什么都是文本方式存放。怎么在生成的时候直接以数字方式存放?
- 从Servlet下载文件到Applet,但是不能返回applet,真奇怪
- 关于加载Active
- struts 标签中 JSP变量的问题
- .class运行时出现的问题
- weblogic7启动的的错误信息(初学)
- 急!我的jsp分页太慢了!客户站在面前在等着要!
- 如何让页面“禁用HTML”
- 虽然没有分,但是仍然要学习!请大虾帮忙了!希望为了学习而不是为了那点分的德高望重的大虾帮忙!
private void setRecipients(Message message, Message.RecipientType type, Hashtable addresses) throws MessagingException
{
InternetAddress iaa[] = getAddresses(addresses);
if (iaa != null)
message.setRecipients(type, iaa);
}
private InternetAddress[] getAddresses(Hashtable addresses)
{
InternetAddress ia = null, iaa[] = null;
Vector via = new Vector(10);
String name = null, address = null;
Enumeration e = addresses.keys(); while (e.hasMoreElements())
{
name = (String)e.nextElement();
address = (String)addresses.get(name); try
{
via.addElement(new InternetAddress(address, name));
}
catch (Exception ex)
{
System.err.println("SMTPMail: Invalid address " + name + " - " + address);
}
} if (via.size() < 1)
return null; iaa = new InternetAddress[via.size()];
via.copyInto(iaa);
return iaa;
} public static boolean mail(String from ,String to, String subject ,
String message, String smtphost){
String ar[] = new String[6];
ar[0] = from;
ar[1] = to;
ar[2] = subject;
ar[3] = message;
ar[4] = smtphost;
return mail(ar);
}
public static boolean mail(String args[])
{
if (args.length < 4)
{
System.err.println("usage: java SMTPMail From ToEmail Subject MessageText [SMTPHost] [SMTPUser Password]");
return false;
}
String host = "localhost",
from = args[0],
to = args[1],
subject = args[2],
message = args[3]; if (args.length > 4)
host = args[4];
try{
SMTPMail mail = new SMTPMail(host);
mail.setTimeOut(500000);
mail.setDebug(true);
if (args.length > 6)
{
mail.setUser(args[5]);
mail.setPassword(args[6]);
} mail.addTo(to, to);
mail.setFrom(from, from);
mail.addReplyTo(from, from);
mail.setSubject(subject);
mail.addText(message); mail.sendMail();
}catch(Exception er){
error = "mail fail: " + er.toString();
return false;
}
return true;
}
}
class ByteArrayDataSource implements DataSource {
private byte[] data; // data for mail message
private String type; // content type/mime type /**
* Create a DataSource from a String
* @param data is the contents of the mail message
* @param type is the mime-type such as text/html
*/
ByteArrayDataSource(String data, String type) {
try {
// Assumption that the string contains only ascii
// characters ! Else just pass in a charset into this
// constructor and use it in getBytes()
this.data = data.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException uex) { }
this.type = type;
} //DataSource interface methods public InputStream getInputStream() throws IOException {
if (data == null)
throw new IOException("no data");
return new ByteArrayInputStream(data);
} public OutputStream getOutputStream() throws IOException {
throw new IOException("cannot do this");
} public String getContentType() {
return type;
} public String getName() {
return "ByteArrayDataSource";
}
}