计算机二级:使用javamail发送html格式邮件计算机二级考试

文章作者 100test 发表时间 2009:05:10 17:20:57
来源 100Test.Com百考试题网


  2009年下半年全国计算机等级考试你准备好了没?考计算机等级考试的朋友,2009年下半年全国计算机等级考试时间是2009年9月19日至23日。更多优质资料尽在百考试题论坛 百考试题在线题库
  实例代码如下:
  package test.
  /*****************************************************
  *
  *
  * @since May 6, 2009
  *
  * 该测试例子演示了使用javamail发送html格式邮件,
  *
  * 分别以发送带附件和不带附件两种形式演示
  *
  * 同时实现了向多人发邮件
  *****************************************************/
  import java.util.Date.
  import java.util.Properties.
  import javax.activation.DataHandler.
  import javax.activation.DataSource.
  import javax.activation.FileDataSource.
  import javax.mail.BodyPart.
  import javax.mail.Message.
  import javax.mail.MessagingException.
  import javax.mail.Multipart.
  import javax.mail.Session.
  import javax.mail.Transport.
  import javax.mail.internet.InternetAddress.
  import javax.mail.internet.MimeBodyPart.
  import javax.mail.internet.MimeMessage.
  import javax.mail.internet.MimeMultipart.
  public class SandMail{
  @SuppressWarnings("static-access")
  public void SendMail2(String title, String attachment){
  // 获得属性,并生成Session对象
  Properties props = new Properties().
  Session sendsession.
  Transport transport.
  MimeMessage message = null.
  BodyPart messageBodyPart = new MimeBodyPart().
  Multipart multipart = new MimeMultipart().
  String from = "[email protected]".
  String to[]={"[email protected]"}.
  String cc[] = {"[email protected]"}.
  String bcc[] = {"[email protected]"}.
  String content = "测试格式化内容测试格式化内容测试格式化内容".
  try{
  sendsession = Session.getInstance(props, null).
  //向属性中写入SMTP服务器的地址
  props.put("mail.smtp.host", "smtp.139.com").
  //设置SMTP服务器需要权限认证
  props.put("mail.smtp.auth", "true").
  //设置输出调试信息
  // sendsession.setDebug(true).
  //根据Session生成Message对象
  message = new MimeMessage(sendsession).
  //设置发信人地址
  message.setFrom(new InternetAddress(from)).
  //设置收信人地址
  String toList = getMailList(to).
  InternetAddress[] iaToList = new InternetAddress().parse(toList).
  message.setRecipients(Message.RecipientType.TO,iaToList).
  if (cc != null){
  String ccList = this.getMailList(cc).
  InternetAddress[] iaCCList = new InternetAddress().parse(ccList).
  message.setRecipients(Message.RecipientType.CC,iaCCList).
  }
  if (bcc != null){
  String bccList = this.getMailList(bcc).
  InternetAddress[] iaBCCList = new InternetAddress().parse(bccList).
  message.setRecipients(Message.RecipientType.BCC,iaBCCList).
  }

相关文章


java中ArrayList中是用remove注意事项计算机二级考试
计算机二级考试:修改JAVA的虚拟内存的方法计算机二级考试
计算机二级:使用javamail发送html格式邮件计算机二级考试
计算机二级java辅导:如何让一个字符串反转计算机二级考试
澳大利亚华人论坛
考好网
日本华人论坛
华人移民留学论坛
英国华人论坛