用Java发送图文并茂的HTML邮件(4)

来源:未知 浏览 391次 时间 2021-03-17 08:50

使用

imgBodyPart。setHeader(内容识别,Javanew InternetAddress (toMailAddress));/ /收件人mimeMessage.setSubject(核心);mimeMessage。setSentDate(新日期())…

imgBodyPart。setHeader(内容识别、发送新的InternetAddress (toMailAddress));/ /收件人mimeMessage.setSubject(核心);mimeMessage。setSentDate(新日期());/ /发送日期Multipartmp = new MimeMultipart(相关);/ /相关显示您可能希望发送一个电子邮件在html / * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / BodyPartbodyPart = new MimeBodyPart ();/ /身体bodyPart。setDataHandler(新DataHandler(测试imgsrc = cid: IMG1 /尝试,用图片和文字(字节)0);}}捕捉(IOExceptione1) {e1.printStackTrace();}最后{尝试{如果(bos != null) bos.close ();} (IOExceptione) {e。printStackTrace();}}返回bos.toByteArray ();}} / * * * Smtp认证* /类SmtpAuthenticator扩展身份{Stringusername =零;Stringpassword =零;/ / SMTP身份修正公共SmtpAuthenticator (Stringusername,真实);道具.put (mail.smtp。主机,HTMLthis .password);}、邮件IMG1 / IMG1);/ /运输在html中使用这张照片来测量src = cid: IMG1 mp.addBodyPart (imgBodyPart);/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / mp.addBodyPart (bodyPart);mimeMessage.setContent (mp);/ /建立邮件的本质就像Transport.send (mimeMessage);/ /发送邮件}/ * * * * * @paramfile *文件路径读取文件* @return返回一个二进制数组* /公共静态byte [] readFile (Stringfile) {FileInputStreamfis =零;ByteArrayOutputStreambos =零;尝试{fis = new FileInputStream(文件);bos = new ByteArrayOutputStream ();int bytesRead;字节缓冲区[]=新字节(1024 * 1024);而((bytesRead = fis.read(缓冲))!= - 1){bos。写(缓冲区,使用Java与图片和文本发送HTML邮件(4)text / HTML; charset = GBK));/ /网页方法/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / BodyPartattachBodyPart = new MimeBodyPart ();/ /通用附件FileDataSourcefds = new FileDataSource (c: /引导。ini);attachBodyPart。setDataHandler(新DataHandler (fds));attachBodyPart。setFileName (B = GBK +新sun.misc.BASE64Encoder () .encode (fds.getName () .getBytes ()) + ? =);/ /处理附件名称在中国的mp.addBodyPart (attachBodyPart);/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * / MimeBodyPartimgBodyPart = new MimeBodyPart ();/ /附件图标byte[]字节= readFile (C: /按钮。gif);ByteArrayDataSourcefileds = new ByteArrayDataSource(字节。

共有4页:

上一页

标签: newnull发送byte