参考下面的程序!
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3]; // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
Session session =
Session.getInstance(props, null); // Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment"); // create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //fill message
messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); // Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart); // Put parts in message
message.setContent(multipart); // Send the message
Transport.send( message );
}
}
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3]; // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
Session session =
Session.getInstance(props, null); // Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment"); // create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //fill message
messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); // Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart); // Put parts in message
message.setContent(multipart); // Send the message
Transport.send( message );
}
}
解决方案 »
- JavaScript表单认证
- 请高手们帮解决 正则
- 实现一个简易的的短信平台功能,请指教!
- JSP开发用Struts的时候,什么时候用FORM,,什么时候可以不用FORM啊
- 有关发mail的问题?
- 关于JBuilder7的超级简单问题!谢谢大家了。好急的。
- 谁知道用JSP做一份简历的填写,修改,预览以及简历.doc或.xls的导入呢?
- 讨论:Jsp,Servlet,EJB,Datebase的关系!
- 奇怪的现象,请指点!
- 很急的,谁能帮帮我!????
- Weblogic+jdk1.2该如何配置,我配置了weblogic.properties后还是不能启动,请高人指教,小弟不胜感激,一定有分相送。
- 在servlet里面怎样读路径呀,比如:(里面)
* @(#)msgshow.java 1.18 98/10/28
*
* Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
*
*/import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;/*
* Demo app that exercises the Message interfaces.
* Show information about and contents of messages.
*
* @author John Mani
* @author Bill Shannon
*/public class msgshow { static String protocol;
static String host = null;
static String user = null;
static String password = null;
static String mbox = "INBOX";
static String url = null;
static int port = -1;
static boolean verbose = false;
static boolean debug = false; public static void main(String argv[]) {
int msgnum = -1;
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("-v")) {
verbose = true;
} else if (argv[optind].equals("-D")) {
debug = true;
} else if (argv[optind].equals("-f")) {
mbox = argv[++optind];
} else if (argv[optind].equals("-L")) {
url = argv[++optind];
} else if (argv[optind].equals("-p")) {
port = Integer.parseInt(argv[++optind]);
} else if (argv[optind].equals("--")) {
optind++;
break;
} else if (argv[optind].startsWith("-")) {
System.out.println(
"Usage: msgshow [-L url] [-T protocol] [-H host] [-p port] [-U user]");
System.out.println(
"\t[-P password] [-f mailbox] [msgnum] [-v] [-D]");
System.exit(1);
} else {
break;
}
} try {
if (optind < argv.length)
msgnum = Integer.parseInt(argv[optind]); // Get a Properties object
Properties props = System.getProperties(); // Get a Session object
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug); // 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, port, user, password);
else
store.connect();
}
// Open the Folder Folder folder = store.getDefaultFolder();
if (folder == null) {
System.out.println("No default folder");
System.exit(1);
} folder = folder.getFolder(mbox);
if (folder == null) {
System.out.println("Invalid folder");
System.exit(1);
} folder.open(Folder.READ_WRITE);
int totalMessages = folder.getMessageCount(); if (totalMessages == 0) {
System.out.println("Empty folder");
folder.close(false);
store.close();
System.exit(1);
} if (verbose) {
int newMessages = folder.getNewMessageCount();
System.out.println("Total messages = " + totalMessages);
System.out.println("New messages = " + newMessages);
System.out.println("-------------------------------");
} if (msgnum == -1) {
// Attributes & Flags for all messages ..
Message[] msgs = folder.getMessages(); // Use a suitable FetchProfile
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.FLAGS);
fp.add("X-Mailer");
folder.fetch(msgs, fp); for (int i = 0; i < msgs.length; i++) {
System.out.println("--------------------------");
System.out.println("MESSAGE #" + (i + 1) + ":");
dumpEnvelope(msgs[i]);
// dumpPart(msgs[i]);
}
} else {
System.out.println("Getting message number: " + msgnum);
Message m = null;
try {
m = folder.getMessage(msgnum);
dumpPart(m);
} catch (IndexOutOfBoundsException iex) {
System.out.println("Message number out of range");
}
} folder.close(false);
store.close();
} catch (Exception ex) {
System.out.println("Oops, got exception! " + ex.getMessage());
ex.printStackTrace();
}
System.exit(1);
} public static void dumpPart(Part p) throws Exception {
if (p instanceof Message)
dumpEnvelope((Message)p); /** Dump input stream .. InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it. Note that the default buffer size of 510 bytes is
// probably too low ...
if (!(is instanceof BufferedInputStream))
is = new BufferedInputStream(p.getInputStream());
int c;
while ((c = is.read()) != -1)
System.out.write(c); **/ System.out.println("CONTENT-TYPE: " + p.getContentType()); Object o = p.getContent();
if (o instanceof String) {
System.out.println("This is a String");
System.out.println("---------------------------");
System.out.println((String)o);
} else if (o instanceof Multipart) {
System.out.println("This is a Multipart");
System.out.println("---------------------------");
Multipart mp = (Multipart)o;
int count = mp.getCount();
for (int i = 0; i < count; i++)
dumpPart(mp.getBodyPart(i));
} else if (o instanceof Message) {
System.out.println("This is a Nested Message");
System.out.println("---------------------------");
dumpPart((Part)o);
} else if (o instanceof InputStream) {
System.out.println("This is just an input stream");
System.out.println("---------------------------");
InputStream is = (InputStream)o;
int c;
while ((c = is.read()) != -1)
System.out.write(c);
}
} public static void dumpEnvelope(Message m) throws Exception {
System.out.println("This is the message envelope");
System.out.println("---------------------------");
Address[] a;
// FROM
if ((a = m.getFrom()) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("FROM: " + a[j].toString());
} // TO
if ((a = m.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++)
System.out.println("TO: " + a[j].toString());
} // SUBJECT
System.out.println("SUBJECT: " + m.getSubject()); // DATE
Date d = m.getSentDate();
System.out.println("SendDate: " +
(d != null ? d.toString() : "UNKNOWN")); // SIZE
System.out.println("Size: " + m.getSize()); // FLAGS:
Flags flags = m.getFlags();
StringBuffer sb = new StringBuffer();
Flags.Flag[] sf = flags.getSystemFlags(); // get the system flags boolean first = true;
for (int i = 0; i < sf.length; i++) {
String s;
Flags.Flag f = sf[i];
if (f == Flags.Flag.ANSWERED)
s = "\\Answered";
else if (f == Flags.Flag.DELETED)
s = "\\Deleted";
else if (f == Flags.Flag.DRAFT)
s = "\\Draft";
else if (f == Flags.Flag.FLAGGED)
s = "\\Flagged";
else if (f == Flags.Flag.RECENT)
s = "\\Recent";
else if (f == Flags.Flag.SEEN)
s = "\\Seen";
else
continue; // skip it
if (first)
first = false;
else
sb.append(' ');
sb.append(s);
} String[] uf = flags.getUserFlags(); // get the user flag strings
for (int i = 0; i < uf.length; i++) {
if (first)
first = false;
else
sb.append(' ');
sb.append(uf[i]);
}
System.out.println("FLAGS = " + sb.toString()); // X-MAILER
String[] hdrs = m.getHeader("X-Mailer");
if (hdrs != null)
System.out.println("X-Mailer: " + hdrs[0]);
else
System.out.println("X-Mailer NOT available");
}
}