用Java发送图文并茂的HTML邮件_.Net教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:用在JavaScript的RequestHelper
碰到一个小小的需求,就是要根据传入的锚(也就是url中#后面的东西啦)来显示不同的内容,记得以前写了的,不知道被我丢到哪去了,又要重新写一个,顺便把功能整理加强了一些,加入了取QueryString

  1. package com.syj;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.util.Arrays;
  6. import java.util.Date;
  7. import java.util.Properties;
  8. import javax.activation.DataHandler;
  9. import javax.activation.FileDataSource;
  10. import javax.mail.Authenticator;
  11. import javax.mail.Message;
  12. import javax.mail.PasswordAuthentication;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeMessage;
  17. import javax.mail.BodyPart;
  18. import javax.mail.Multipart;
  19. import javax.mail.internet.MimeBodyPart;
  20. import javax.mail.internet.MimeMultipart;
  21. import com.sun.istack.internal.ByteArrayDataSource;
  22. /**
  23. * <P>
  24. * Title:用java发送邮件的例子
  25. * </P>
  26. *
  27. * <P>
  28. * Description:发送图片附件并在html中使用该图片
  29. * </P>
  30. *
  31. * <P>
  32. * Copyright: Copyright (c) 2007
  33. * </P>
  34. *
  35. * @author 孙钰佳
  36. * @main sunyujia@yahoo.cn
  37. * @date Jun 10, 2008 12:35:26 AM
  38. */
  39. public class SendMail {
  40. private static String username = "xxxx";
  41. private static String password = "xxxx";
  42. private static String smtpServer = "smtp.163.com";
  43. private static String fromMailAddress = "xxxx@163.com";
  44. private static String toMailAddress = "sunyujia@yahoo.cn";
  45. public static void main(String[] args) throws Exception {
  46. Properties props = new Properties();
  47. props.put("mail.smtp.auth", "true");
  48. props.put("mail.smtp.host", smtpServer);
  49. // 获得邮件会话对象
  50. Session session = Session.getDefaultInstance(props,
  51. new SmtpAuthenticator(username, password));
  52. /** *************************************************** */
  53. // 创建MIME邮件对象
  54. MimeMessage mimeMessage = new MimeMessage(session);
  55. mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
  56. mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(
  57. toMailAddress));// 收件人
  58. mimeMessage.setSubject("主题");
  59. mimeMessage.setSentDate(new Date());// 发送日期
  60. Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
  61. /** *************************************************** */
  62. BodyPart bodyPart = new MimeBodyPart();// 正文
  63. bodyPart.setDataHandler(new DataHandler("测<img src="cid:IMG1" />试",
  64. "text/html;charset=GBK"));// 网页格式
  65. /** *************************************************** */
  66. BodyPart attachBodyPart = new MimeBodyPart();// 普通附件
  67. FileDataSource fds = new FileDataSource("c:/boot.ini");
  68. attachBodyPart.setDataHandler(new DataHandler(fds));
  69. attachBodyPart.setFileName("=?GBK?B?"
  70. new sun.misc.BASE64Encoder().encode(fds.getName().getBytes())
  71. "?=");// 解决附件名中文乱码
  72. mp.addBodyPart(attachBodyPart);
  73. /** *************************************************** */
  74. MimeBodyPart imgBodyPart = new MimeBodyPart(); // 附件图标
  75. byte[] bytes = readFile("C:/button.gif");
  76. ByteArrayDataSource fileds = new ByteArrayDataSource(bytes,
  77. "application/octet-stream");
  78. imgBodyPart.setDataHandler(new DataHandler(fileds));
  79. imgBodyPart.setFileName("button.gif");
  80. imgBodyPart.setHeader("Content-ID", "<IMG1></IMG1>");// 在html中使用该图片方法src="cid:IMG1"
  81. mp.addBodyPart(imgBodyPart);
  82. /** *************************************************** */
  83. mp.addBodyPart(bodyPart);
  84. mimeMessage.setContent(mp);// 设置邮件内容对象
  85. Transport.send(mimeMessage);// 发送邮件
  86. }
  87. /**
  88. * 读取文件
  89. *
  90. * @param file
  91. * 文件路径
  92. * @return 返回二进制数组
  93. */
  94. public static byte[] readFile(String file) {
  95. FileInputStream fis = null;
  96. ByteArrayOutputStream bos = null;
  97. try {
  98. fis = new FileInputStream(file);
  99. bos = new ByteArrayOutputStream();
  100. int bytesRead;
  101. byte buffer[] = new byte[1024 * 1024];
  102. while ((bytesRead = fis.read(buffer)) != -1) {
  103. bos.write(buffer, 0, bytesRead);
  104. Arrays.fill(buffer, (byte) 0);
  105. }
  106. } catch (IOException e1) {
  107. e1.printStackTrace();
  108. } finally {
  109. try {
  110. if (bos != null)
  111. bos.close();
  112. } catch (IOException e) {
  113. e.printStackTrace();
  114. }
  115. }
  116. return bos.toByteArray();
  117. }
  118. }
  119. /**
  120. * Smtp认证
  121. */
  122. class SmtpAuthenticator extends Authenticator {
  123. String username = null;
  124. String password = null;
  125. // SMTP身份验证
  126. public SmtpAuthenticator(String username, String password) {
  127. this.username = username;
  128. this.password = password;
  129. }
  130. public PasswordAuthentication getPasswordAuthentication() {
  131. return new PasswordAuthentication(this.username, this.password);
  132. }
  133. }

分享:ASP.NET2.0中控件的简单异步回调
虽然已经有了ASP.NET AJAX了,最近学习ASP.NET控件的时候,逐步理解了原始的控件异步回调(代码取自《ASP.NET 2.0 高级编程》): 首先,在Render事件中添加好一个事件

来源:模板无忧//所属分类:.Net教程/更新时间:2008-08-22
相关.Net教程