javamail发送带图片的html信件,图片怎么显示不出来?

时间:2021-04-16 18:09:44
javamail发送带图片的html信件,图片怎么显示不出来?

用foxmail发送的邮件到163.com,图片就可以显示出来,为什么用javamail就不行呢?邮件也收到了,图片也是附件,就是看到邮件内容的html图片显示不出来。
代码如下:

import java.util.Properties; 
import java.util.Date; 
import javax.mail.*; 
import javax.mail.internet.*; 
import javax.activation.*; 
public class Test {

/**
 * @param args
 */
public static void main(String[] args)throws Exception{
sendhtml(args);
}

public static void sendhtml(String[] argv) throws Exception{

    String  to="q123456789@163.com", subject ="null", from ="abcdef@163.com",
      cc = null, bcc = null;
    String mailhost ="smtp.163.com";
    boolean debug = false;
    String username ="abcdef"; 
String password ="123456"; 
      Properties props = System.getProperties();
props.put("mail.smtp.auth","true");
SMTPAuth auth = new SMTPAuth(username,password); 
      //  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, auth);
      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);


      MimeBodyPart mbp1= new MimeBodyPart();
      String html ="<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"+
        "<html>"+
        "<head><title></title></head>"+
        "<body>"+
        "<b> see the following jpg : it is a car!</b><br>"+
        "<a href=http://www.a.com/a.jsp>hello</a><br>"+
        "<IMG SRC=cid:7e2a34e1.jpg width=80% height=60%><br>"+
        "<b> end of jpg</b>"+
        "</body>"+
        "</html>";

      mbp1.setContent(html,"text/html");

      FileDataSource fds = new FileDataSource("c:/7e2a34e1.jpg");
      MimeBodyPart mbp2 = new MimeBodyPart();
      mbp2.setFileName(fds.getName());
      mbp2.setText("This is a beautiful car !");
      mbp2.setDataHandler(new DataHandler(fds));
      mbp2.setHeader("Content-ID",fds.getName());

      MimeMultipart mp = new MimeMultipart("related");//alternative
      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.");

  }


}
class SMTPAuth extends javax.mail.Authenticator{
private String user,password; 
public SMTPAuth(String u,String p){
user=u;
password=p;
}
public void getuserinfo(String getuser,String getpassword){ 
  user = getuser; 
  password = getpassword; 

protected javax.mail.PasswordAuthentication getPasswordAuthentication(){ 
  return new javax.mail.PasswordAuthentication(user,password); 


}

7 个解决方案

#1


关注!顶一下!

#2


up

#3


mark

#4




估计图片没有发过去!

#5


图片确实发送过去了!
用foxmail收下来在html模式下也能看到图片。
就是在163邮箱中在线看时看不到图片,显示一个带小红叉的图片位置(网页上没找到图片都这样显示)。

奇怪,网上搜到的代码差不多都这样写的,难道没有人测试过吗?

#6




如果是这样肯定是相对地址变了, 你用其他邮箱测试一下!

#7


花了两个小时,终于找到了原因:

mbp2.setHeader("Content-ID",fds.getName());
改成
mbp2.setHeader("Content-ID", "<" + fds.getName() + ">");

参照过 RFC2387,发现<>不是必须的.就是说,163的信箱检查的时候有点太严格了.

我就是用你的代码测试的,修改过的就是上面的那一行和账号信息了.

#1


关注!顶一下!

#2


up

#3


mark

#4




估计图片没有发过去!

#5


图片确实发送过去了!
用foxmail收下来在html模式下也能看到图片。
就是在163邮箱中在线看时看不到图片,显示一个带小红叉的图片位置(网页上没找到图片都这样显示)。

奇怪,网上搜到的代码差不多都这样写的,难道没有人测试过吗?

#6




如果是这样肯定是相对地址变了, 你用其他邮箱测试一下!

#7


花了两个小时,终于找到了原因:

mbp2.setHeader("Content-ID",fds.getName());
改成
mbp2.setHeader("Content-ID", "<" + fds.getName() + ">");

参照过 RFC2387,发现<>不是必须的.就是说,163的信箱检查的时候有点太严格了.

我就是用你的代码测试的,修改过的就是上面的那一行和账号信息了.