//发送一封html信,信里包含一副图片! 给分!
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;public class sendhtml { public static void main(String[] argv) {
new sendhtml(argv);
} public sendhtml(String[] argv) { String to, subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String mailer = "sendhtml";
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("-M")) {
mailhost = 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("-o")) { // originator
from = 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: sendhtml [[-L
store-url] | [-T prot] [-H host] [-U
user] [-P passwd]]");
System.out.println("\t[-s subject] [-o
from-address] [-c cc-addresses] [-b
bcc-addresses]");
System.out.println("\t[-f record-mailbox]
[-M transport-host] [-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);
} Properties props = System.getProperties();
// could use Session.getTransport() and Transport.connect()
// assume we're using SMTP
if (mailhost != null)
props.put("mail.smtp.host", mailhost); // Get a Session object
Session session = Session.getDefaultInstance(props, null);
if (debug)
session.setDebug(true); // construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
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); MimeMultipart mp = new MimeMultipart(); mp.setSubType("related"); MimeBodyPart mbp1= new MimeBodyPart();
String html =
"<html>"+
"<head><title></title></head>"+
"<body>"+
"<b> see the following jpg : it is a car!</b><br>"+
"<a href=a.jsp>hello</a><br>"+
"<IMG SRC=cid:23abc@pc27 width=80% height=60%><br>"+
"<b> end of jpg</b>"+
"</body>"+
"</html>"; mbp1.setContent(html,"text/html"); MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(
"d:/html/bmp/1-1-95679_0005.jpg");
mbp2.setFileName(fds.getName());
mbp2.setText("This is a beautiful car !");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setHeader("Content-ID","23abc@pc27"); mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg);
System.out.println(mp.getCount());
System.out.println("\nMail was sent successfully."); } catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;public class sendhtml { public static void main(String[] argv) {
new sendhtml(argv);
} public sendhtml(String[] argv) { String to, subject = null, from = null,
cc = null, bcc = null, url = null;
String mailhost = null;
String mailer = "sendhtml";
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("-M")) {
mailhost = 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("-o")) { // originator
from = 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: sendhtml [[-L
store-url] | [-T prot] [-H host] [-U
user] [-P passwd]]");
System.out.println("\t[-s subject] [-o
from-address] [-c cc-addresses] [-b
bcc-addresses]");
System.out.println("\t[-f record-mailbox]
[-M transport-host] [-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);
} Properties props = System.getProperties();
// could use Session.getTransport() and Transport.connect()
// assume we're using SMTP
if (mailhost != null)
props.put("mail.smtp.host", mailhost); // Get a Session object
Session session = Session.getDefaultInstance(props, null);
if (debug)
session.setDebug(true); // construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
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); MimeMultipart mp = new MimeMultipart(); mp.setSubType("related"); MimeBodyPart mbp1= new MimeBodyPart();
String html =
"<html>"+
"<head><title></title></head>"+
"<body>"+
"<b> see the following jpg : it is a car!</b><br>"+
"<a href=a.jsp>hello</a><br>"+
"<IMG SRC=cid:23abc@pc27 width=80% height=60%><br>"+
"<b> end of jpg</b>"+
"</body>"+
"</html>"; mbp1.setContent(html,"text/html"); MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(
"d:/html/bmp/1-1-95679_0005.jpg");
mbp2.setFileName(fds.getName());
mbp2.setText("This is a beautiful car !");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setHeader("Content-ID","23abc@pc27"); mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg);
System.out.println(mp.getCount());
System.out.println("\nMail was sent successfully."); } catch (Exception e) {
e.printStackTrace();
}
}
}
解决方案 »
- IO流中最常见的while循环
- 防止自己的Jar文件被反编译
- java udp socket 通信中在接受端防止包过多而丢失,要写个队列,请高手写个代码看看,
- 蛙鸣我把int [][]aBak=new int [3][ ];改成int [][]aBak=new int [4][ ];后出现java.lang.NullPointerExcption错误?
- 怎样设置对话框标题栏的图标?JDialog没有setIconImage()函数可以用!!
- 无法响应事件,准备去跳楼!
- 跪求这个java程序的算法和时间复杂度。
- 请教高手!!两个类在一起怎么编译??
- ResultSet作为形参传送的问题,在线等待
- JBuilder6.0里面JDataStore使用问题
- 如何改变JBuilder4中"{"的风格
- again紧急求救!!!!用JAVAMAIL怎样发HTML(has attachments)邮件
你这个方法仍未解决MimeMultipart嵌套的问题。只是形成两个MimeBodyPart,仍然不能在multipart/mixed里形成一个multipart/alternative部分,这个问题怎么解决???另外,怎样与你联系,这样交流起来太麻烦!多谢!!