在网上有一些说法,说用Content-ID类型就可以在正文中显示图片,我的测试类如下
package cn.itcast.javamail2;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.util.ByteArrayDataSource;
/**
* @author hansong
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class MyMailTest {
public static LinkedList getImages(String path, String[] imagesNames)
throws Exception {
LinkedList attachList = new LinkedList();
for (int i = 0; i < imagesNames.length; i++) {
byte[] buffer = new byte[1024];
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(path + imagesNames[i]));
ByteArrayOutputStream bo = new ByteArrayOutputStream();
int ii;
while ((ii = in.read()) != -1) {
bo.write(ii);
}
in.close();
bo.close();
attachList.add(bo.toByteArray());
}
return attachList;
}
public static String getContent()
{
StringBuffer content = new StringBuffer(1000); // 邮件的html源代码
content.append("Image0 is <img src=cid:IMG0></img>");
// content.append("Image1 is <img src=cid:IMG1></img>");
return content.toString();
}
public static void main(String[] args) throws Exception {
String smtp = "smtp.sohu.com";
String to = "whw_0501@sina.com";
String from = "whw_0501@sohu.com";
String name = "whw_0501";
String password = "13487512789";
String subject = "This is a test";
//得到正文
String content = getContent();
//得到图片
String path = "D:\\";
String[] imageNames = { "test.gif" };
LinkedList attachList = getImages(path, imageNames); // 附件的list,它的element都是byte[],即图片的二进制流
//配置邮件环境
Properties props = new Properties();
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.auth", "true");
Session session = null;
session = Session.getInstance(props,
(javax.mail.Authenticator) new Email_Autherticatorbean(name,
password));
MimeMessage message;
session.setDebug(true);
InternetAddress[] address = { new InternetAddress(to) };
message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, address);
message.setSubject(subject);
message.setSentDate(new Date());
// 新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
MimeMultipart mm = new MimeMultipart();
// 新建一个存放信件内容的BodyPart对象
BodyPart mdp = new MimeBodyPart();
// 给BodyPart对象设置内容和格式/编码方式
// mdp.setContent(content.toString(), "text/html;charset=GBK");
mdp.setContent(content, "text/html;charset=utf-8");
// 这句很重要,千万不要忘了
mm.setSubType("related");
mm.addBodyPart(mdp);
// add the attachments
for (int i = 0; i < imageNames.length; i++) {
// 新建一个存放附件的BodyPart
mdp = new MimeBodyPart();
DataHandler dh = new DataHandler(new ByteArrayDataSource(
(byte[]) attachList.get(i), "application/octet-stream"));
mdp.setDataHandler(dh);
// 加上这句将作为附件发送,否则将作为信件的文本内容
//mdp.setFileName(new Integer(i).toString() + ".gif");
mdp.setHeader("Content-ID", "IMG" + new Integer(i).toString());
// 将含有附件的BodyPart加入到MimeMultipart对象中
mm.addBodyPart(mdp);
}
// 把mm作为消息对象的内容
message.setContent(mm);
message.saveChanges();
javax.mail.Transport transport = session.getTransport("smtp");
transport.connect(smtp, name, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
class Email_Autherticatorbean extends Authenticator {
private String m_username = null;
private String m_userpass = null;
public void setUsername(String username) {
m_username = username;
}
public void setUserpass(String userpass) {
m_userpass = userpass;
}
public Email_Autherticatorbean(String username, String userpass) {
super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(m_username, m_userpass);
}
}
我登录上whw_0501@sina.com邮箱,发现正文里图片显示不出,却把图片显示在附件上,请高手帮忙啊,我不要附件,只要在正文里显示图片
10 个解决方案
#1
LZ对javamail很感兴趣啊,最近一直在研究?
O(∩_∩)O~
O(∩_∩)O~
#2
是的,项目需要,呵呵,但问题非常多,谁知道帮忙解决下啊
#3
public void SendMessage(String msghtm) {
String to = "name@21cn.com";
String from = "name@21cn.com";
String subject = "testhtml你好";
String mailhost = "smtp.21cn.com";
String content="<html><head> 你好 !</head><body><a href='http://www.sina.com'>content</a><br>contentcontentcontentcontent</body></html>";
MimeMessage mimeMsg = null;
Session session = null;
String fileAttachment = "C:\\mm.jpg";
try {
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
props.put("mail.transpont.protocol","smtp");
props.put("mail.smtp.auth","true");
session = Session.getInstance(props,new PassAuth("name","pass"));
mimeMsg = new MimeMessage( session );
mimeMsg.setFrom(new InternetAddress( from ) );
if(to!=null){
mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
}
mimeMsg.setSubject(subject,"GB2312");
// 第一部分信息
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText( content, "GB2312");
// 第二部分信息
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource( fileAttachment );
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mbp2.setHeader("Content-ID","image1");
// 在第三部分信息中附加一个文件
StringBuffer msg=new StringBuffer();
String line="";
FileReader fr=new FileReader("c:\\test.htm");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null){
msg.append(line);
}
BodyPart mbp3=new MimeBodyPart();
mbp3.setContent(msg.toString(),"text/html;charset=gb2312");
// 创建 Multipart 并放入每个 MimeBodyPart
Multipart mp = new MimeMultipart("related");
mp.addBodyPart( mbp1 );
mp.addBodyPart( mbp2 );
mp.addBodyPart( mbp3);
// 增加 Multipart 到信息体
//mimeMsg.setContent( mp );
//System.out.println(msg.toString());
//String temp=MimeUtility.encodeText(msg.toString(),"gb2312","B");
//String temp=new String(msg.toString().getBytes("iso-8859-1"),"gb2312");
System.out.println(msghtm);
BodyPart mbp6=new MimeBodyPart();
mbp6.setContent(msg.toString(),"text/html;charset=gb2312");
Multipart mm=new MimeMultipart();
mm.addBodyPart(mbp6);
mm.addBodyPart(mbp2);
mimeMsg.setContent(mm);
//mimeMsg.setContent(msghtm,"text/html;charset=gb2312");
mimeMsg.setDisposition("Inline");
mimeMsg.setSentDate(new Date());
Transport.send( mimeMsg );
System.out.println("email send!");
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
#4
回贴了
#5
你把你要发送的那个网页里面图片的地址改成绝对路径,直接发送就可以了
#6
没做过JAVAMAIL方面的东西。。MARK..
#7
鼎一个
#8
顶,一直等答案!!!不知道有谁知道正确答案的!!
#9
我正需要这个功能,一起顶起来!
#10
刚刚试运行了一下,发现如果是发给QQ邮箱和hotmail的话没问题,给163发底下多了一个莫名的附件,给雅虎邮箱发看不到图片。
#1
LZ对javamail很感兴趣啊,最近一直在研究?
O(∩_∩)O~
O(∩_∩)O~
#2
是的,项目需要,呵呵,但问题非常多,谁知道帮忙解决下啊
#3
public void SendMessage(String msghtm) {
String to = "name@21cn.com";
String from = "name@21cn.com";
String subject = "testhtml你好";
String mailhost = "smtp.21cn.com";
String content="<html><head> 你好 !</head><body><a href='http://www.sina.com'>content</a><br>contentcontentcontentcontent</body></html>";
MimeMessage mimeMsg = null;
Session session = null;
String fileAttachment = "C:\\mm.jpg";
try {
Properties props = System.getProperties(); //获得系统属性
props.put("mail.smtp.host", mailhost); //设置SMTP主机
props.put("mail.transpont.protocol","smtp");
props.put("mail.smtp.auth","true");
session = Session.getInstance(props,new PassAuth("name","pass"));
mimeMsg = new MimeMessage( session );
mimeMsg.setFrom(new InternetAddress( from ) );
if(to!=null){
mimeMsg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( to ) );
}
mimeMsg.setSubject(subject,"GB2312");
// 第一部分信息
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText( content, "GB2312");
// 第二部分信息
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource( fileAttachment );
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName());
mbp2.setHeader("Content-ID","image1");
// 在第三部分信息中附加一个文件
StringBuffer msg=new StringBuffer();
String line="";
FileReader fr=new FileReader("c:\\test.htm");
BufferedReader br=new BufferedReader(fr);
while((line=br.readLine())!=null){
msg.append(line);
}
BodyPart mbp3=new MimeBodyPart();
mbp3.setContent(msg.toString(),"text/html;charset=gb2312");
// 创建 Multipart 并放入每个 MimeBodyPart
Multipart mp = new MimeMultipart("related");
mp.addBodyPart( mbp1 );
mp.addBodyPart( mbp2 );
mp.addBodyPart( mbp3);
// 增加 Multipart 到信息体
//mimeMsg.setContent( mp );
//System.out.println(msg.toString());
//String temp=MimeUtility.encodeText(msg.toString(),"gb2312","B");
//String temp=new String(msg.toString().getBytes("iso-8859-1"),"gb2312");
System.out.println(msghtm);
BodyPart mbp6=new MimeBodyPart();
mbp6.setContent(msg.toString(),"text/html;charset=gb2312");
Multipart mm=new MimeMultipart();
mm.addBodyPart(mbp6);
mm.addBodyPart(mbp2);
mimeMsg.setContent(mm);
//mimeMsg.setContent(msghtm,"text/html;charset=gb2312");
mimeMsg.setDisposition("Inline");
mimeMsg.setSentDate(new Date());
Transport.send( mimeMsg );
System.out.println("email send!");
} catch (MessagingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
#4
回贴了
#5
你把你要发送的那个网页里面图片的地址改成绝对路径,直接发送就可以了
#6
没做过JAVAMAIL方面的东西。。MARK..
#7
鼎一个
#8
顶,一直等答案!!!不知道有谁知道正确答案的!!
#9
我正需要这个功能,一起顶起来!
#10
刚刚试运行了一下,发现如果是发给QQ邮箱和hotmail的话没问题,给163发底下多了一个莫名的附件,给雅虎邮箱发看不到图片。