首页 | 源码下载 | 网站模板 | 网页特效 | 广告代码 | 网页素材 | 字体下载 | 书库 | 站长工具
会员投稿 投稿指南 RSS订阅
当前位置:主页>网络编程>java教程>资讯:Java Mail 深入浅出(2)

Java Mail 深入浅出(2)

www.jz123.cn  2010-02-01   来源:   中国建站    责任编辑(袁袁)    我要投递新闻

 


 /**
  * SendMailService 构造子注解。
  */
  public SendMail() {
  super();
  }
  private void fillMail(Session session,MimeMessage msg) throws IOException, MessagingException{
  String fileName = null;
  Multipart mPart = new MimeMultipart();
  if (mailFrom != null) {
  msg.setFrom(new InternetAddress(mailFrom));
  System.out.println("发送人Mail地址:"+mailFrom);
  } else {
  System.out.println("没有指定发送人邮件地址!");
  return;
  }
  if (mailTo != null) {
  InternetAddress[] address = InternetAddress.parse(mailTo);
  msg.setRecipients(Message.RecipientType.TO, address);
  System.out.println("收件人Mail地址:"+mailTo);
  } else {
  System.out.println("没有指定收件人邮件地址!");
  return;
  }
  if (mailccTo != null) {
  InternetAddress[] ccaddress = InternetAddress.parse(mailccTo);
  System.out.println("CCMail地址:"+mailccTo);
  msg.setRecipients(Message.RecipientType.CC, ccaddress);
  }
  if (mailbccTo != null) {
  InternetAddress[] bccaddress = InternetAddress.parse(mailbccTo);
  System.out.println("BCCMail地址:"+mailbccTo);
  msg.setRecipients(Message.RecipientType.BCC, bccaddress);
  }
  msg.setSubject(subject);
  InternetAddress[] replyAddress = { new InternetAddress(mailFrom)};
  msg.setReplyTo(replyAddress);
  // create and fill the first message part
  MimeBodyPart mBodyContent = new MimeBodyPart();
  if (msgContent != null)
  mBodyContent.setContent(msgContent, messageContentMimeType);
  else
  mBodyContent.setContent("", messageContentMimeType);
  mPart.addBodyPart(mBodyContent);
  // attach the file to the message
  if (attachedFileList != null) {
  for (Enumeration fileList = attachedFileList.elements(); fileList.hasMoreElements();) {
  fileName = (String) fileList.nextElement();
  MimeBodyPart mBodyPart = new MimeBodyPart();
  // attach the file to the message
  FileDataSource fds = new FileDataSource(messageBasePath + fileName);
  System.out.println("Mail发送的附件为:"+messageBasePath + fileName);
  mBodyPart.setDataHandler(new DataHandler(fds));
  mBodyPart.setFileName(fileName);
  mPart.addBodyPart(mBodyPart);
  }
  }
  msg.setContent(mPart);
  msg.setSentDate(new Date());
  }
  /**
  * 此处插入方法说明。
  */
  public void init()
  {
  }
  /**
  * 发送e_mail,返回类型为int
  * 当返回值为0时,说明邮件发送成功
  * 当返回值为3时,说明邮件发送失败
  */
  public int sendMail() throws IOException, MessagingException {
  int loopCount;
  Properties props = System.getProperties();
  props.put("mail.smtp.host", smtpHost);
  props.put("mail.smtp.auth", "true");
  MailAuthenticator auth = new MailAuthenticator();
  Session session = Session.getInstance(props, auth);
  session.setDebug(debug);
  MimeMessage msg = new MimeMessage(session);
  Transport trans = null;
  try {
  fillMail(session,msg);
  // send the message
  trans = session.getTransport("smtp");


 

上一篇:Java Mail 深入浅出(1) 下一篇:JAVA类定义,成员类,抽象类及接口类

评论总数:0 [ 查看全部 ] 网友评论


关于我们隐私版权广告服务友情链接联系我们网站地图