/*
* @(#)sendhtml.java 1.3 98/07/06
*
* Copyright (c) 1996-1998 by Sun Microsystems, Inc.
* All Rights Reserved.
*/import java.io.*;
import java.util.Properties;
import java.util.Date;import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;/**
* Demo app that shows how to construct and send a single part html
* message.
*
* @author John Mani
* @author Bill Shannon
* @author Max Spivak
*/public class sendhtml {
String subject;
String to;
String cc;
String bcc; public static void main(String[] argv) {
new sendhtml(argv);
} public sendhtml(String[] argv) { String url = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind; for (optind = 0; optind < argv.length; optind++) {
if (argv[optind].equals("-T")) {
protocol = argv[++optind];
} else if (argv[optind].equals("-H")) {
host = argv[++optind];
} else if (argv[optind].equals("-U")) {
user = argv[++optind];
} else if (argv[optind].equals("-P")) {
password = argv[++optind];
} else if (argv[optind].equals("-f")) {
record = argv[++optind];
} else if (argv[optind].equals("-s")) {
subject = argv[++optind];
} else if (argv[optind].equals("-c")) {
cc = argv[++optind];
} else if (argv[optind].equals("-b")) {
bcc = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-d")) {
debug = true;
} else if (argv[optind].equals("--")) {
optind++;
break;
} else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
System.out.println(
"\t[-s subject] [-c cc-addresses] [-b bcc-addresses]");
System.out.println(
"\t[-f record-mailbox] [-d] [address]");
System.exit(1);
} else {
break;
}
} try {
if (optind < argv.length) {
// XXX - concatenate all remaining arguments
to = argv[optind];
System.out.println("To: " + to);
} else {
System.out.print("To: ");
System.out.flush();
to = in.readLine();
}
if (subject == null) {
System.out.print("Subject: ");
System.out.flush();
subject = in.readLine();
} else {
System.out.println("Subject: " + subject);
} // Get a Session object
Session session = Session.getDefaultInstance(System.getProperties(), null);
if (debug)
session.setDebug(true); // construct the message
Message msg = new MimeMessage(session);
msg.setFrom(); msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false)); msg.setSubject(subject); collect(in, msg); msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date()); // send the thing off
Transport.send(msg); System.out.println("\nMail was sent successfully."); // Keep a copy, if requested. if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null)
store = session.getStore(protocol);
else
store = session.getStore(); // Connect
if (host != null || user != null || password != null)
store.connect(host, user, password);
else
store.connect();
} // Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
System.err.println("Can't get record folder.");
System.exit(1);
}
if (!folder.exists())
folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1];
msgs[0] = msg;
folder.appendMessages(msgs); System.out.println("Mail was recorded successfully.");
} } catch (Exception e) {
e.printStackTrace();
}
} public void collect(BufferedReader in, Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
sb.append("<HTML>\n");
sb.append("<HEAD>\n");
sb.append("<TITLE>\n");
sb.append(subject + "\n");
sb.append("</TITLE>\n");
sb.append("</HEAD>\n"); sb.append("<BODY>\n");
sb.append("<H1>" + subject + "</H1>" + "\n"); while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
} sb.append("</BODY>\n");
sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
new ByteArrayDataSource(sb.toString(), "text/html")));
}
}/* This class implements a typed DataSource from :
* an InputStream
* a byte array
* a String
*/
class ByteArrayDataSource implements DataSource {
private byte[] data; // data
private String type; // content-type /* Create a datasource from an input stream */
ByteArrayDataSource(InputStream is, String type) {
this.type = type;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int ch; while ((ch = is.read()) != -1)
// XXX : must be made more efficient by
// doing buffered reads, rather than one byte reads
os.write(ch);
data = os.toByteArray(); } catch (IOException ioex) { }
} /* Create a datasource from a byte array */
ByteArrayDataSource(byte[] data, String type) {
this.data = data;
this.type = type;
} /* Create a datasource from a String */
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;
} 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 "dummy";
}
}
* @(#)sendhtml.java 1.3 98/07/06
*
* Copyright (c) 1996-1998 by Sun Microsystems, Inc.
* All Rights Reserved.
*/import java.io.*;
import java.util.Properties;
import java.util.Date;import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;/**
* Demo app that shows how to construct and send a single part html
* message.
*
* @author John Mani
* @author Bill Shannon
* @author Max Spivak
*/public class sendhtml {
String subject;
String to;
String cc;
String bcc; public static void main(String[] argv) {
new sendhtml(argv);
} public sendhtml(String[] argv) { String url = null;
String mailer = "msgsend";
String protocol = null, host = null, user = null, password = null;
String record = null; // name of folder in which to record mail
boolean debug = false;
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
int optind; for (optind = 0; optind < argv.length; optind++) {
if (argv[optind].equals("-T")) {
protocol = argv[++optind];
} else if (argv[optind].equals("-H")) {
host = argv[++optind];
} else if (argv[optind].equals("-U")) {
user = argv[++optind];
} else if (argv[optind].equals("-P")) {
password = argv[++optind];
} else if (argv[optind].equals("-f")) {
record = argv[++optind];
} else if (argv[optind].equals("-s")) {
subject = argv[++optind];
} else if (argv[optind].equals("-c")) {
cc = argv[++optind];
} else if (argv[optind].equals("-b")) {
bcc = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-d")) {
debug = true;
} else if (argv[optind].equals("--")) {
optind++;
break;
} else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: msgsend [[-L store-url] | [-T prot] [-H host] [-U user] [-P passwd]]");
System.out.println(
"\t[-s subject] [-c cc-addresses] [-b bcc-addresses]");
System.out.println(
"\t[-f record-mailbox] [-d] [address]");
System.exit(1);
} else {
break;
}
} try {
if (optind < argv.length) {
// XXX - concatenate all remaining arguments
to = argv[optind];
System.out.println("To: " + to);
} else {
System.out.print("To: ");
System.out.flush();
to = in.readLine();
}
if (subject == null) {
System.out.print("Subject: ");
System.out.flush();
subject = in.readLine();
} else {
System.out.println("Subject: " + subject);
} // Get a Session object
Session session = Session.getDefaultInstance(System.getProperties(), null);
if (debug)
session.setDebug(true); // construct the message
Message msg = new MimeMessage(session);
msg.setFrom(); msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(Message.RecipientType.BCC,
InternetAddress.parse(bcc, false)); msg.setSubject(subject); collect(in, msg); msg.setHeader("X-Mailer", mailer);
msg.setSentDate(new Date()); // send the thing off
Transport.send(msg); System.out.println("\nMail was sent successfully."); // Keep a copy, if requested. if (record != null) {
// Get a Store object
Store store = null;
if (url != null) {
URLName urln = new URLName(url);
store = session.getStore(urln);
store.connect();
} else {
if (protocol != null)
store = session.getStore(protocol);
else
store = session.getStore(); // Connect
if (host != null || user != null || password != null)
store.connect(host, user, password);
else
store.connect();
} // Get record Folder. Create if it does not exist.
Folder folder = store.getFolder(record);
if (folder == null) {
System.err.println("Can't get record folder.");
System.exit(1);
}
if (!folder.exists())
folder.create(Folder.HOLDS_MESSAGES); Message[] msgs = new Message[1];
msgs[0] = msg;
folder.appendMessages(msgs); System.out.println("Mail was recorded successfully.");
} } catch (Exception e) {
e.printStackTrace();
}
} public void collect(BufferedReader in, Message msg)
throws MessagingException, IOException {
String line;
StringBuffer sb = new StringBuffer();
sb.append("<HTML>\n");
sb.append("<HEAD>\n");
sb.append("<TITLE>\n");
sb.append(subject + "\n");
sb.append("</TITLE>\n");
sb.append("</HEAD>\n"); sb.append("<BODY>\n");
sb.append("<H1>" + subject + "</H1>" + "\n"); while ((line = in.readLine()) != null) {
sb.append(line);
sb.append("\n");
} sb.append("</BODY>\n");
sb.append("</HTML>\n"); msg.setDataHandler(new DataHandler(
new ByteArrayDataSource(sb.toString(), "text/html")));
}
}/* This class implements a typed DataSource from :
* an InputStream
* a byte array
* a String
*/
class ByteArrayDataSource implements DataSource {
private byte[] data; // data
private String type; // content-type /* Create a datasource from an input stream */
ByteArrayDataSource(InputStream is, String type) {
this.type = type;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int ch; while ((ch = is.read()) != -1)
// XXX : must be made more efficient by
// doing buffered reads, rather than one byte reads
os.write(ch);
data = os.toByteArray(); } catch (IOException ioex) { }
} /* Create a datasource from a byte array */
ByteArrayDataSource(byte[] data, String type) {
this.data = data;
this.type = type;
} /* Create a datasource from a String */
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;
} 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 "dummy";
}
}
解决方案 »
- JList强迫更新怎么实现?不用点击某个按钮区出发事件的。
- java写了个即时通讯系统,想加入语音(类似于微信对讲也行),视频功能,求指导。
- 求助!不出窗口!!怎么回事?急等!!
- 求解一个递归的算法
- java数据结构的经典问题。高分求解,看看哪里错了?
- 我现在有个项目需要通过在客户端使用放置证书链的方式,用https方式连接服务器,有人能指点一下吗?最好能给出源码,谢谢
- 如何通过AOP得到切点方法的返回值,中间变量?
- 海康SDK二次开发问题
- 请问java 中如何依次的播放声音文件?
- 几个关于InputStream的小问题
- ????再次请教::想用JAVAMAIL发送一个带有附件的多媒体邮件(即:邮件正文是HTML,并随信有附件),怎样形成邮件的信体部分??
- 在IE中使用JavaPlugin浏览Java1.2的Applet?求助,急急急...
你最好找一个这样的组件。
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds=new FileDataSource(filename);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(filename);
// create the Multipart and its parts to it
mp.addBodyPart(mbp2);