发送带图片邮件例子

  1. import java.io.BufferedReader;   
  2. import java.io.IOException;   
  3. import java.util.Date;   
  4. import java.util.Properties;   
  5.   
  6. import javax.activation.DataHandler;   
  7. import javax.activation.DataSource;   
  8. import javax.activation.FileDataSource;   
  9. import javax.mail.BodyPart;   
  10. import javax.mail.Folder;   
  11. import javax.mail.Message;   
  12. import javax.mail.MessagingException;   
  13. import javax.mail.SendFailedException;   
  14. import javax.mail.Session;   
  15. import javax.mail.Store;   
  16. import javax.mail.URLName;   
  17. import javax.mail.internet.InternetAddress;   
  18. import javax.mail.internet.MimeBodyPart;   
  19. import javax.mail.internet.MimeMessage;   
  20. import javax.mail.internet.MimeMultipart;   
  21.   
  22. import com.sun.mail.smtp.SMTPAddressFailedException;   
  23. import com.sun.mail.smtp.SMTPAddressSucceededException;   
  24. import com.sun.mail.smtp.SMTPSendFailedException;   
  25. import com.sun.mail.smtp.SMTPTransport;   
  26.   
  27. public class smtpsend {   
  28.   
  29.     public static void main(String[] argv) {   
  30.     String   from = "name@example.com", url = null;   
  31.     String mailhost = "smtp.example.com";   
  32.     String mailer = "smtpsend";   
  33.     String protocol = null, host = "smtp.example.com", user = "name", password = "psd";   
  34.     String record = null;   // name of folder in which to record mail   
  35.     boolean debug = false;   
  36.     boolean verbose = false;   
  37.     boolean auth = true;   
  38.     String prot = "smtp";   
  39.   
  40.     try {   
  41.   
  42.         Properties props = System.getProperties();   
  43.         if (mailhost != null)   
  44.         props.put("mail." + prot + ".host", mailhost);   
  45.         if (auth)   
  46.         props.put("mail." + prot + ".auth""true");   
  47.   
  48.         Session session = Session.getInstance(props, null);   
  49.         if (debug)   
  50.         session.setDebug(true);   
  51.   
  52.         Message msg = new MimeMessage(session);   
  53.         if (from != null)   
  54.         msg.setFrom(new InternetAddress(from));   
  55.         else  
  56.         msg.setFrom();   
  57.   
  58.         msg.setRecipients(Message.RecipientType.TO,   
  59.                     InternetAddress.parse("name@example.com"false));   
  60.   
  61.         msg.setSubject("dd");   
  62.         MimeMultipart multipart = new MimeMultipart("related");   
  63.   
  64.         BodyPart messageBodyPart = new MimeBodyPart();   
  65.         String htmlText = "<H1>Hello</H1><img src=\"cid:image\">ddddddd";   
  66.         String htmlText1 = "<H1>Hello</H1><img src=\"cid:image1\">ddddddd";   
  67.         messageBodyPart.setContent(htmlText + htmlText1, "text/html");   
  68.   
  69.         multipart.addBodyPart(messageBodyPart);   
  70.   
  71.         messageBodyPart = new MimeBodyPart();   
  72.         DataSource fds = new FileDataSource   
  73.           ("E:\\aa\\bb\\icons\\incomingLinksNavigatorGroup.gif");   
  74.         messageBodyPart.setDataHandler(new DataHandler(fds));   
  75.         messageBodyPart.setHeader("Content-ID","<image>");   
  76.         multipart.addBodyPart(messageBodyPart);   
  77.         messageBodyPart = new MimeBodyPart();   
  78.         fds = new FileDataSource   
  79.           ("E:\\aa\\bb\\icons\\incomingLinksNavigatorGroup.gif");   
  80.         messageBodyPart.setDataHandler(new DataHandler(fds));   
  81.         messageBodyPart.setHeader("Content-ID","<image1>");   
  82.   
  83.         // add it   
  84.         multipart.addBodyPart(messageBodyPart);   
  85.   
  86.   
  87.         msg.setContent(multipart);   
  88.         SMTPTransport t =   
  89.         (SMTPTransport)session.getTransport(prot);   
  90.         try {   
  91.         if (auth)   
  92.             t.connect(mailhost, user, password);   
  93.         else  
  94.             t.connect();   
  95.         t.sendMessage(msg, msg.getAllRecipients());   
  96.         } finally {   
  97.         if (verbose)   
  98.             System.out.println("Response: " +   
  99.                         t.getLastServerResponse());   
  100.         t.close();   
  101.         }   
  102.   
  103.         System.out.println("\nMail was sent successfully.");   
  104.   
  105.         if (record != null) {   
  106.         // Get a Store object   
  107.         Store store = null;   
  108.         if (url != null) {   
  109.             URLName urln = new URLName(url);   
  110.             store = session.getStore(urln);   
  111.             store.connect();   
  112.         } else {   
  113.             if (protocol != null)   
  114.             store = session.getStore(protocol);   
  115.             else  
  116.             store = session.getStore();   
  117.   
  118.             // Connect   
  119.             if (host != null || user != null || password != null)   
  120.             store.connect(host, user, password);   
  121.             else  
  122.             store.connect();   
  123.         }   
  124.         Folder folder = store.getFolder(record);   
  125.         if (folder == null) {   
  126.             System.err.println("Can't get record folder.");   
  127.             System.exit(1);   
  128.         }   
  129.         if (!folder.exists())   
  130.             folder.create(Folder.HOLDS_MESSAGES);   
  131.   
  132.         Message[] msgs = new Message[1];   
  133.         msgs[0] = msg;   
  134.         folder.appendMessages(msgs);   
  135.   
  136.         System.out.println("Mail was recorded successfully.");   
  137.         }   
  138.   
  139.     } catch (Exception e) {   
  140.         if (e instanceof SendFailedException) {   
  141.         MessagingException sfe = (MessagingException)e;   
  142.         if (sfe instanceof SMTPSendFailedException) {   
  143.             SMTPSendFailedException ssfe =   
  144.                     (SMTPSendFailedException)sfe;   
  145.             System.out.println("SMTP SEND FAILED:");   
  146.             if (verbose)   
  147.             System.out.println(ssfe.toString());   
  148.             System.out.println("  Command: " + ssfe.getCommand());   
  149.             System.out.println("  RetCode: " + ssfe.getReturnCode());   
  150.             System.out.println("  Response: " + ssfe.getMessage());   
  151.         } else {   
  152.             if (verbose)   
  153.             System.out.println("Send failed: " + sfe.toString());   
  154.         }   
  155.         Exception ne;   
  156.         while ((ne = sfe.getNextException()) != null &&   
  157.             ne instanceof MessagingException) {   
  158.             sfe = (MessagingException)ne;   
  159.             if (sfe instanceof SMTPAddressFailedException) {   
  160.             SMTPAddressFailedException ssfe =   
  161.                     (SMTPAddressFailedException)sfe;   
  162.             System.out.println("ADDRESS FAILED:");   
  163.             if (verbose)   
  164.                 System.out.println(ssfe.toString());   
  165.             System.out.println("  Address: " + ssfe.getAddress());   
  166.             System.out.println("  Command: " + ssfe.getCommand());   
  167.             System.out.println("  RetCode: " + ssfe.getReturnCode());   
  168.             System.out.println("  Response: " + ssfe.getMessage());   
  169.             } else if (sfe instanceof SMTPAddressSucceededException) {   
  170.             System.out.println("ADDRESS SUCCEEDED:");   
  171.             SMTPAddressSucceededException ssfe =   
  172.                     (SMTPAddressSucceededException)sfe;   
  173.             if (verbose)   
  174.                 System.out.println(ssfe.toString());   
  175.             System.out.println("  Address: " + ssfe.getAddress());   
  176.             System.out.println("  Command: " + ssfe.getCommand());   
  177.             System.out.println("  RetCode: " + ssfe.getReturnCode());   
  178.             System.out.println("  Response: " + ssfe.getMessage());   
  179.             }   
  180.         }   
  181.         } else {   
  182.         System.out.println("Got Exception: " + e);   
  183.         if (verbose)   
  184.             e.printStackTrace();   
  185.         }   
  186.     }   
  187.     }   
  188.   
  189.     public static String collect(BufferedReader in) throws IOException {   
  190.     String line;   
  191.     StringBuffer sb = new StringBuffer();   
  192.     while ((line = in.readLine()) != null) {   
  193.         sb.append(line);   
  194.         sb.append("\n");   
  195.     }   
  196.     return sb.toString();   
  197.     }   
  198. }   
  1. da shang
    donate-alipay
               donate-weixin weixinpay

发表评论↓↓