import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mailer {
/** javamail session对象. */
protected Session session;
/** 发信地址 */
protected String from;
/** 信的标题*/
protected String subject;
/** 存放字符串形式的收信地址 */
protected ArrayList toList = new ArrayList();
/** 抄送地址*/
protected ArrayList ccList = new ArrayList();
/** 暗送地址 */
protected ArrayList bccList = new ArrayList();
/** 信的文本内容*/
protected String body;
/** SMTP服务器地址*/
protected String mailHost;
/**是否在控制台打印信息*/
protected boolean verbose=true;
/** Get from */
public String getFrom() {
return from;
}
public void setFrom(String fm) {
from = fm;
}
public String getSubject() {
return subject;
}
public void setSubject(String subj) {
subject = subj;
}
public ArrayList getToList() {
return toList;
}
public void setToList(ArrayList to) {
toList = to;
}
public void setToList(String s) {
toList = tokenize(s);
}
public void addTo(String to) {
toList.add(to);
}
public ArrayList getCcList() {
return ccList;
}
public void setCcList(ArrayList cc) {
ccList = cc;
}
public void setCcList(String s) {
ccList = tokenize(s);
}
public void addCc(String cc) {
ccList.add(cc);
}
public ArrayList getBccList() {
return bccList;
}
public void setBccList(ArrayList bcc) {
bccList = bcc;
}
public void setBccList(String s) {
bccList = tokenize(s);
}
public void addBcc(String bcc) {
bccList.add(bcc);
}
public String getBody() {
return body;
}
public void setBody(String text) {
body = text;
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean v) {
verbose = v;
}
public boolean isComplete() {
if (from == null || from.length()==0) {
System.err.println("doSend: no FROM");
return false;
}
if (subject == null || subject.length()==0) {
System.err.println("doSend: no SUBJECT");
return false;
}
if (toList.size()==0) {
System.err.println("doSend: no recipients");
return false;
}
if (body == null || body.length()==0) {
System.err.println("doSend: no body");
return false;
}
if (mailHost == null || mailHost.length()==0) {
System.err.println("doSend: no server host");
return false;
}
return true;
}
public void setServer(String s) {
mailHost = s;
}
/** Send the message.
*/
public synchronized void doSend() throws MessagingException {
if (!isComplete())
throw new IllegalArgumentException("doSend called before message was complete");
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
// Create the Session object
if (session == null) {
session = Session.getDefaultInstance(props, null);
if (verbose)
session.setDebug(true); // Verbose!
}
// create a message
final Message mesg = new MimeMessage(session);
InternetAddress[] addresses;
// TO Address list
addresses = new InternetAddress[toList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)toList.get(i));
mesg.setRecipients(Message.RecipientType.TO, addresses);
// From Address
mesg.setFrom(new InternetAddress(from));
// CC Address list
addresses = new InternetAddress[ccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)ccList.get(i));
mesg.setRecipients(Message.RecipientType.CC, addresses);
// BCC Address list
addresses = new InternetAddress[bccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)bccList.get(i));
mesg.setRecipients(Message.RecipientType.BCC, addresses);
// The Subject
mesg.setSubject(subject);
// Now the message body.
mesg.setText(body);
// Finally, send the message! (use static Transport method)
// Do this in a Thread as it sometimes is too slow for JServ
// new Thread() {
// public void run() {
// try {
Transport transport =session.getTransport("smtp");
transport.connect(mailHost, "zz3zcwb", "123456");
transport.sendMessage(mesg, mesg.getAllRecipients());
// } catch (MessagingException e) {
// throw new IllegalArgumentException(
// "Transport.send() threw: " + e.toString());
// }
// }
// }.start();
}
public static void send(String mailhost,String recipient, String sender, String subject, String message)
throws MessagingException {
Mailer m = new Mailer();
m.setServer(mailhost);
m.addTo(recipient);
m.setFrom(sender);
m.setSubject(subject);
m.setBody(message);
m.doSend();
}
protected ArrayList tokenize(String s) {
ArrayList al = new ArrayList();
StringTokenizer tf = new StringTokenizer(s, ",");
// For each word found in the line
while (tf.hasMoreTokens()) {
// trim blanks, and add to list.
al.add(tf.nextToken().trim());
}
return al;
}
public static void main(String args[]){
Mailer mailer=new Mailer();
mailer.setServer("smtp.163.com");
mailer.setFrom("
[email protected]");
mailer.addTo("
[email protected]");
mailer.addTo("
[email protected]");
mailer.setSubject("我发的信,");
mailer.setBody("I am OK,很好!请不要发大量拉圾邮件");
try{
mailer.doSend();
}catch (MessagingException e) {}
}
}
运行结果:
C:\java>javac Mailer.java
C:\java>java Mailer
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25
220 Coremail SMTP(Anti Spam) System (163com[20030606])
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO teacher
250-192.168.1.212
250-PIPELINING
250-AUTH LOGIN PLAIN NTLM
250-AUTH=LOGIN PLAIN NTLM
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN NTLM"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
enozemN3Yg==
334 UGFzc3dvcmQ6
MTIzNDU2
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<
[email protected]>
250 Ok
RCPT TO:<
[email protected]>
250 Ok
RCPT TO:<
[email protected]>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP:
[email protected]
DEBUG SMTP:
[email protected]
DATA
354 End data with <CR><LF>.<CR><LF>
Message-ID: <813251.1098515605463.JavaMail.cwb@teacher>
From:
[email protected]
To:
[email protected],
[email protected]
Subject: =?GBK?B?ztK3orXE0MUs?=
Mime-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
SSBhbSBPS6Osuty6w6Ohx+uyu9Kqt6K088G/wK27+NPKvP4=
.
250 Ok: queued as KcCN948EekFKVOYA.1
C:\java>