怎么写html邮件

1. 怎么用HTML格式发送邮件.既怎么发HTML格式的邮件.. 发送html格式的邮件void jButton8_actionPerformed(ActionEvent e) { try {//此段代码用来发送带有html信息的邮件 Properties props = new Properties(); Authenticator auth = new Email_Autherticator(); props.put( "mail.smtp.host ",host); props.put( "mail.smtp.auth ", "true "); Session session = Session.getDefaultInstance(props,auth); MimeMessage message = new MimeMessage(session); //设置邮件正文 String htmltext= "hello"+ " click me please! "; message.setContent(htmltext, "text/html ");//text/plain设置邮件的格式因为是带有html文档的,所以用text/html格式 message.setSubject(mail_subject); // message.setText(mail_body);在使用message.setContent()时候,好象不能用message.setText() message.setHeader(mail_head_name,mail_head_value); message.setSentDate(new Date()); Address address = new InternetAddress(mail_from, "sunxiaoming "); message.setFrom(address); Address toAddress = new InternetAddress(mail_to); message.addRecipient(Message.RecipientType.TO,toAddress); Transport.send(message); System.out.println( "send ok! "); } catch(Exception ex) { System.out.println( "faild "+ex); } } void jButton9_actionPerformed(ActionEvent e) { try {//此段代码用来发送带有图片的邮件 String file = "./20020423/test.gif "; Properties props = new Properties(); Authenticator auth = new Email_Autherticator(); props.put( "mail.smtp.host ",host); props.put( "mail.smtp.auth ", "true "); Session session = Session.getDefaultInstance(props,auth); MimeMessage message = new MimeMessage(session); message.setSubject( "Embedded Image "); Address address = new InternetAddress(mail_from, "sunxiaoming "); message.setFrom(address); Address toAddress = new InternetAddress(mail_to); message.addRecipient(Message.RecipientType.TO,toAddress); message.setHeader(mail_head_name,mail_head_value); message.setSentDate(new Date()); BodyPart messageBodyPart = new MimeBodyPart(); String htmlText = ""+ " ";//加入图片 messageBodyPart.setContent(htmlText, "text/html "); //原理同发送附件一样,将邮件看成有多个部分组成的,先设置每一个部分,然后再全部将它们合起来即可 。
//Create a related multi-part to combine the parts MimeMultipart multipart = new MimeMultipart( "related "); multipart.addBodyPart(messageBodyPart); //Create part for the image messageBodyPart = new MimeBodyPart(); //Fetch the image and associate to part DataSource fds = new FileDataSource(file); messageBodyPart.setDataHandler(new DataHandler(fds)); messageBodyPart.setHeader( "Content-ID ", "memememe "); //Add part to multi-part multipart.addBodyPart(messageBodyPart); //Associate multi-part with message message.setContent(multipart); Transport.send(message); System.out.println( "send ok! "); } catch(Exception ex) { System.out.println( "faild "+ex); } } void jButton7_actionPerformed(ActionEvent e) { try {//该程序用来接受带有html信息的邮件 Properties props = System.getProperties(); Authenticator auth = new Email_Autherticator(); props.put( "mail.smtp.host ",host); props.put( "mail.smtp.auth ", "true "); Session session = Session.getDefaultInstance(props,auth); Store store = session.getStore( "pop3 "); store.connect(host,username,password); //After connecting to the Store,you can get a Folder,which must be opened before you can read messages from it: Folder folder = store.getFolder( "INBOX "); folder.open(Folder.READ_WRITE); Message message[] = folder.getMessages(); for (int i=0,n=message.length;i 2. 如何制作HTML邮件 有以下几种方法推荐:1、使用现成的邮件代码 。
比如使用别人发送到您邮箱中的HTML邮件,它就是现成的代码,将邮件中的内容或图片更换为自己的,然后进行发送 。2、使用网上现成的服务 。
网上很多这种服务,比如,您可以百度搜索肯为旎DIY就能找到,使用这种代码兼容性更强,更能显出专业性 。3、自己写代码 。
使用DW、OUTLOOK EXPRESS等等进行编写代码,这个需要一定的专业知识,需要经过多种兼容性测试,否则可能会产生错位(不同的邮箱显示不同甚至错位,造成不能观看),所以不太建议自己写代码,费力不讨好,个人建议应该站在巨人的肩上 。