public static void main(String[] args) throws Exception {
Properties prop = new Properties();//得到一个properties的实例 得到session的时候要用到
  prop.put("mail.transport.protocol", "smtp");//协议
  prop.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop);//用Session类的一个静态方法得到session的实例 
  session.setDebug(true);
Transport transport = session.getTransport();//得到一个Transport的实例 这个类是一个抽象类,我们可以使用这个类的静态方法send()来发送消息
  transport.connect("smtp.163.com",username, password);
InputStream iss = new BufferedInputStream( new FileInputStream("G:/pics/it.jpg"));
InternetAddress[] ia = new InternetAddress[1];
  ia[0] = new InternetAddress("[email protected]");
MimeMessage message = new MimeMessage(session);//得到一个message对象
  
  message.setFrom(new InternetAddress("[email protected]"));//设置发件人
  message.setRecipients(Message.RecipientType.TO, ia);//设置收件人
  message.setSubject("确认邮件主题");//邮件主题
MimeBodyPart text = new MimeBodyPart();//得到一个正文bodypart的实例
  text.setContent("<html>&nbsp;&nbsp;&nbsp;&nbsp;这个破图片研究的<img src=cid:logo.gif>头都大了!!!</html>", "text/html;charset=gb2312");
/** 代表正文所引用的图片的bodypart **/
  MimeBodyPart image = new MimeBodyPart();//得到一个图片的bodypart实例
  DataHandler dh1 = new DataHandler(new ByteArrayDataSource(iss,"application/octet-stream"));//一种数据类型的处理类  初始化需要传入一个数据源
  image.setDataHandler(dh1);//放入多媒体数据
  image.setDisposition(MimeBodyPart.INLINE);
  image.setContentID("logo.gif");//定义正文内容引用id
  image.setHeader("Content-ID", "logo.gif");
MimeMultipart text_image = new MimeMultipart();
text_image.addBodyPart(text);//将文本放入数据容器中
  text_image.addBodyPart(image);//将图片放入数据容器中
  text_image.setSubType("related");
MimeMultipart multipart = new MimeMultipart();
multipart.addBodyPart(text_image);
message.setContent(multipart);//将数据容器放入message类
  message.saveChanges();//保存修改
transport.sendMessage(message,ia);//发送邮件的方法需要两个参数 一个是内容类(Message)的实例 一个是地址数组(会发送给多个人所以是数组)
  transport.close();//关闭通信
}package mail;import java.io.*;
import javax.activation.*;public class ByteArrayDataSource implements DataSource {
/** * Data to write. */
private byte[] _data;
/** * Content-Type. */
private String _type; /* Create a datasource from an input stream */
public ByteArrayDataSource(InputStream is, String type) {
_type = type;
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
int ch; // XXX : must be made more efficient by
// doing buffered reads, rather than one byte reads
while ((ch = is.read()) != -1)
os.write(ch);
_data = os.toByteArray();
} catch (IOException ioe) {
}
} /* Create a datasource from a byte array */
public ByteArrayDataSource(byte[] data, String type) {
_data = data;
_type = type;
} /* Create a datasource from a String */
public 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()
_data = data.getBytes("iso-8859-1");
} catch (UnsupportedEncodingException uee) {
}
_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";
}
}
以上是我的源码,这个代码给163,sina,gmail,yahoo都能把图片正常的现实在邮件正文中,但是就是sohu不行,把图片放到了附件中,正文中是XXX,研究了半天也没研究出个结果,如果把new ByteArrayDataSource(iss,"application/octet-stream")换成
new FileDataSource("G:/pics/it.jpg")就完全正常,ByteArrayDataSource是直接用inputStream创建实例的,但是为啥就不行呢?哪位高人做过这个方面或者有啥意见,我是没辙了,先谢过各位大虾了....