请问如何在HTML邮件里显示图片??JAVAMAIL

ggzzkk 2002-01-17 02:12:47
...全文
212 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ggzzkk 2002-01-19
  • 打赏
  • 举报
回复
我找了,都是发送的例子,没有显示图片的。

是不是要把消息中的图片内容保存为一个图片文件??要是这样,保存的路径是那里呢?
skyyoung 2002-01-19
  • 打赏
  • 举报
回复
If the message came across as text/html content, just send the HTML out as the output of the JSP request. All the different IMG tags will make separate requests to the server to display the images.
The only real problem is if the images come across with the JavaMail message and have a URL that begins with a cid: URL .If that is the case, then you deal with sending the image yourself (and saving it locally). You can try to create a protocol handler for cid, but I think the best way is to convert the URL into one that is handled by default, like any other image, with an HTTP request.


As far as the images go... you are actually better off leaving them on your server and letting the user's mail tool take care of pulling them across when viewing the HTML file. While yes you can include the images as attachments with the HTML body, what the mail tool does with them you have no control over. For instance, if all attachment files are saved to the same directory (like Eudora) then you must name the image files uniquely in order for the mail tool to be sure to display YOUR images and not some other image that the user received from you or someone else.

Remember to make your image URLs absolute, not relative, too. As relative ones will not resolve properly from the mail client.

If you really want to send them along with the message... You'll need to create a "multipart/related" message. One of the parts will be the HTML part which refers to the other parts which are the image parts. An RFC that contains information about multipart/related messages is available at http://www.faqs.org/rfcs/rfc2387.html.

To reference the other parts, the URL will have to specify the content id / message id, like cid:, instead of http:. This is described in RFC 2111.

The following example was posted to the JavaMail mailing list from lys to demonstrate using the cid:

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();
}
}
}

You can run it by following steps:
replace the .gif file with another one on your local machine.
javac sendhtml.java
java sendhtml -M 111.222.1.21 test@yahoo.com .
* 111.222.1.21 is the SMTP Server
lucong 2002-01-19
  • 打赏
  • 举报
回复
通常有两种办法:
1、在消息中加入图片的URL(如果图片已经放在某个网站上了),比如:
比如<img src="http://images.ucomics.com/comics/ga/2002/ga020104.gif" height=178 width=600>

2、把消息做成Multipart,每一幅图片都MIME encode。
skyyoung 2002-01-17
  • 打赏
  • 举报
回复
查找论坛,唷。

23,407

社区成员

发帖
与我相关
我的任务
社区描述
Java 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧