`
z75148885
  • 浏览: 191418 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

用JavaMail发送带附件的邮件

阅读更多

本文根据Ian F. Darwin的《Java Cookbook》整理而成,原书用整章的文字介绍如何发邮件,可能头绪会比较乱,本文则将其浓缩成一篇文章,力求使完全不懂JavaMail的人,都可以根据文中指示稍作修改,拿来就可以用。如果对其中原理还有不清楚,你可以参考原书。

一、首先要用到三个java文件:

1.MailConstants.java,properties文件的助记符:
///////////////////////////////////////////////////////////////////////
package untitled2;

/** Simply a list of names for the Mail System to use.
 * If you "implement" this interface, you don't have to prefix
 * all the names with MailProps in your code.
 */
public interface MailConstants {
  public static final String PROPS_FILE_NAME = "MailClient.properties";

  public static final String SEND_PROTO = "Mail.send.protocol";
  public static final String SEND_USER = "Mail.send.user";
  public static final String SEND_PASS = "Mail.send.password";
  public static final String SEND_ROOT = "Mail.send.root";
  public static final String SEND_HOST = "Mail.send.host";
  public static final String SEND_DEBUG = "Mail.send.debug";

  public static final String RECV_PROTO = "Mail.receive.protocol";
  public static final String RECV_PORT = "Mail.receive.port";
  public static final String RECV_USER = "Mail.receive.user";
  public static final String RECV_PASS = "Mail.receive.password";
  public static final String RECV_ROOT = "Mail.receive.root";
  public static final String RECV_HOST = "Mail.receive.host";
  public static final String RECV_DEBUG = "Mail.receive.debug";
}
///////////////////////////////////////////////////////////////////////

2.FileProperties.java,从文件中读取properties:
///////////////////////////////////////////////////////////////////////
package untitled2;

import java.io.*;
import java.util.*;

/**
 * The <CODE>FileProperties</CODE> class extends <CODE>Properties</CODE>,
 * "a persistent set of properties [that] can be saved to a stream
 * or loaded from a stream". This subclass attends to all the mundane
 * details of opening the Stream(s) for actually saving and loading
 * the Properties.
 *
 * <P>This subclass preserves the useful feature that
 * a property list can contain another property list as its
 * "defaults"; this second property list is searched if
 * the property key is not found in the original property list.
 *
 * @author Ian F. Darwin, ian@darwinsys.com
 * @version $Id: FileProperties.java,v 1.5 2001/04/28 13:22:37 ian Exp $
 */
public class FileProperties
    extends Properties {
  protected String fileName = null;

  /**
   *  Construct a FileProperties given a fileName.
   *  @param loadsaveFileName the progerties file name
   *  @throws IOException
   */
  public FileProperties(String loadsaveFileName) throws IOException {
    super();
    fileName = loadsaveFileName;
    load();
  }

  /** Construct a FileProperties given a fileName and
   * a list of default properties.
   * @param loadsaveFileName the properties file name
   * @param defProp the default properties
   * @throws IOException
   */
  public FileProperties(String loadsaveFileName, Properties defProp) throws
      IOException {
    super(defProp);
    fileName = loadsaveFileName;
    load();
  }

  /** The InputStream for loading */
  protected InputStream inStr = null;

  /** The OutputStream for loading */
  protected OutputStream outStr = null;

  /** Load the properties from the saved filename.
   * If that fails, try again, tacking on the .properties extension
   * @throws IOException
   */
  public void load() throws IOException {
    try {
      if (inStr == null) {
        inStr = new FileInputStream(fileName);
      }
    }
    catch (FileNotFoundException fnf) {
      if (!fileName.endsWith(".properties")) {
        inStr = new FileInputStream(fileName + ".properties");
        // If we succeeded, remember it:
        fileName += ".properties";
      }
      else {

        // It did end with .properties and failed, re-throw exception.
        throw fnf;
      }
    }
    // now message the superclass code to load the file.
    load(inStr);
  }

  /** Save the properties for later loading. *
   *  @throws IOException
   */

  public void save() throws IOException {
    if (outStr == null) {
      outStr = new FileOutputStream(fileName);
    }
    // Get the superclass to do most of the work for us.
    store(outStr, "# Written by FileProperties.save() at " + new Date());
  }

  public void close() {
    try {
      if (inStr != null) {
        inStr.close();
      }
      if (outStr != null) {
        outStr.close();
      }
    }
    catch (IOException e) {
      // don't care
    }
  }
}
///////////////////////////////////////////////////////////////////////

3.Mailer.java,将javamail发邮件的部分进行封装:
///////////////////////////////////////////////////////////////////////
package untitled2;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class Mailer {
  /** The javamail session object. */
  protected Session session;
  /** The sender's email address */
  protected String from;
  /** The subject of the message. */
  protected String subject;
  /** The recipient ("To:"), as Strings. */
  protected ArrayList toList = new ArrayList();
  /** The CC list, as Strings. */
  protected ArrayList ccList = new ArrayList();
  /** The BCC list, as Strings. */
  protected ArrayList bccList = new ArrayList();
  /** The text of the message. */
  protected String body;
  /** The SMTP relay host */
  protected String mailHost;
  /** The accessories list, as Strings.*/
  protected ArrayList accessories = new ArrayList();
  /** The verbosity setting */
  protected boolean verbose;

  /** Get from
   * @return where the mail from
   */
  public String getFrom() {
    return from;
  }

  /** Set from
   * @param fm where the mail from
   */
  public void setFrom(String fm) {
    from = fm;
  }

  /** Get subject
   * @return the mail subject
   */
  public String getSubject() {
    return subject;
  }

  /** Set subject
   * @param subj the mail subject
   */
  public void setSubject(String subj) {
    subject = subj;
  }

  // SETTERS/GETTERS FOR TO: LIST

  /** Get tolist, as an array of Strings
   * @return the list of toAddress
   */
  public ArrayList getToList() {
    return toList;
  }

  /** Set to list to an ArrayList of Strings
   * @param to the list of toAddress
   */
  public void setToList(ArrayList to) {
    toList = to;
  }

  /** Set to as a string like "tom, mary, robin@host". Loses any
   * previously-set values.
   * @param s the list of toAddress*/
  public void setToList(String s) {
    toList = tokenize(s);
  }

  /** Add one "to" recipient
   * @param to the toAddress
   */
  public void addTo(String to) {
    toList.add(to);
  }

  // SETTERS/GETTERS FOR CC: LIST

  /** Get cclist, as an array of Strings
   * @return the list of ccAddress
   */
  public ArrayList getCcList() {
    return ccList;
  }

  /** Set cc list to an ArrayList of Strings
   * @param cc the list of ccAddress
   */
  public void setCcList(ArrayList cc) {
    ccList = cc;
  }

  /** Set cc as a string like "tom, mary, robin@host". Loses any
   * previously-set values.
   * @param s the list of ccAddress
   */
  public void setCcList(String s) {
    ccList = tokenize(s);
  }

  /** Add one "cc" recipient
   * @param cc the address of cc
   */
  public void addCc(String cc) {
    ccList.add(cc);
  }

  // SETTERS/GETTERS FOR BCC: LIST

  /** Get bcclist, as an array of Strings
   * @return the list of bcc
   */
  public ArrayList getBccList() {
    return bccList;
  }

  /** Set bcc list to an ArrayList of Strings
   * @param bcc the address of bcc
   */
  public void setBccList(ArrayList bcc) {
    bccList = bcc;
  }

  /** Set bcc as a string like "tom, mary, robin@host". Loses any
   * previously-set values.
   * @param s the address of bcc
   */
  public void setBccList(String s) {
    bccList = tokenize(s);
  }

  /** Add one "bcc" recipient
   * @param bcc the address of bcc
   */
  public void addBcc(String bcc) {
    bccList.add(bcc);
  }

  // SETTER/GETTER FOR MESSAGE BODY

  /** Get message
   * @return the mail body
   */
  public String getBody() {
    return body;
  }

  /** Set message
   * @param text the mail body
   */
  public void setBody(String text) {
    body = text;
  }

  // SETTER/GETTER FOR ACCESSORIES

  /** Get accessories
   * @return the arrayList of the accessories
   */
  public ArrayList getAccessories() {
    return accessories;
  }

  /** Set accessories
   * @param accessories the arrayList of the accessories
   */
  public void setAccessories(ArrayList accessories) {
    this.accessories = accessories;
  }

  // SETTER/GETTER FOR VERBOSITY

  /** Get verbose
   * @return verbose
   */
  public boolean isVerbose() {
    return verbose;
  }

  /** Set verbose
   * @param v the verbose
   */
  public void setVerbose(boolean v) {
    verbose = v;
  }

  /** Check if all required fields have been set before sending.
   * Normally called e.g., by a JSP before calling doSend.
   * Is also called by doSend for verification.
   * @return if complete return true else return false
   */
  public boolean isComplete() {
    if (from == null || from.length() == 0) {
      System.err.println("doSend: no FROM");
      return false;
    }
    if (subject == null || subject.length() == 0) {
      System.err.println("doSend: no SUBJECT");
      return false;
    }
    if (toList.size() == 0) {
      System.err.println("doSend: no recipients");
      return false;
    }
    if (body == null || body.length() == 0) {
      System.err.println("doSend: no body");
      return false;
    }
    if (mailHost == null || mailHost.length() == 0) {
      System.err.println("doSend: no server host");
      return false;
    }
    return true;
  }

  public void setServer(String s) {
    mailHost = s;
  }

  /** Send the message.
   * @throws MessagingException
   */
  public synchronized void doSend() throws MessagingException {

    if (!isComplete()) {
      throw new IllegalArgumentException(
          "doSend called before message was complete");
    }

    /** Properties object used to pass props into the MAIL API */
    Properties props = new Properties();
    props.put("mail.smtp.host", mailHost);

    // Create the Session object
    if (session == null) {
      session = Session.getDefaultInstance(props, null);
      if (verbose) {
        session.setDebug(true); // Verbose!
      }
    }

    // create a message
    final Message mesg = new MimeMessage(session);

    InternetAddress[] addresses;

    // TO Address list
    addresses = new InternetAddress[toList.size()];
    for (int i = 0; i < addresses.length; i++) {
      addresses[i] = new InternetAddress( (String) toList.get(i));
    }
    mesg.setRecipients(Message.RecipientType.TO, addresses);

    // From Address
    mesg.setFrom(new InternetAddress(from));

    // CC Address list
    addresses = new InternetAddress[ccList.size()];
    for (int i = 0; i < addresses.length; i++) {
      addresses[i] = new InternetAddress( (String) ccList.get(i));
    }
    mesg.setRecipients(Message.RecipientType.CC, addresses);

    // BCC Address list
    addresses = new InternetAddress[bccList.size()];
    for (int i = 0; i < addresses.length; i++) {
      addresses[i] = new InternetAddress( (String) bccList.get(i));
    }
    mesg.setRecipients(Message.RecipientType.BCC, addresses);

    // The Subject
    mesg.setSubject(subject);

    // Now the message body.
    Multipart mp = new MimeMultipart();
    MimeBodyPart mbp = null;
    mbp = new MimeBodyPart();
    mbp.setText(body);
    mp.addBodyPart(mbp);

    // Now the accessories.
    int accessoriesCount = accessories.size();
    File f;
    DataSource ds;
    String uf;
    int j;
    for (int i = 0; i < accessoriesCount; i++) {
      mbp = new MimeBodyPart();
      f = new File( (String) accessories.get(i));
      ds = new FileDataSource(f);
      mbp.setDataHandler(new DataHandler(ds));
      j = f.getName().lastIndexOf(File.separator);
      uf = f.getName().substring(j + 1);
      mbp.setFileName(uf);
      mp.addBodyPart(mbp);
    }

    mesg.setContent(mp);

    // Finally, send the message! (use static Transport method)
    // Do this in a Thread as it sometimes is too slow for JServ
    // new Thread() {
    // public void run() {
    // try {

    Transport.send(mesg);

    // } catch (MessagingException e) {
    // throw new IllegalArgumentException(
    // "Transport.send() threw: " + e.toString());
    // }
    // }
    // }.start();
  }

  /** Convenience method that does it all with one call.
   * @param mailhost - SMTP server host
   * @param recipient - domain address of email (user@host.domain)
   * @param sender - your email address
   * @param subject - the subject line
   * @param message - the entire message body as a String with embedded \n's
   * @param accessories - the accessories list
   * @throws MessagingException
   */
  public static void send(String mailhost,
                          String recipient, String sender, String subject,
                          String message, ArrayList accessories) throws
      MessagingException {
    Mailer m = new Mailer();
    m.setServer(mailhost);
    m.addTo(recipient);
    m.setFrom(sender);
    m.setSubject(subject);
    m.setBody(message);
    m.setAccessories(accessories);
    m.doSend();
  }

  /** Convert a list of addresses to an ArrayList. This will work
   * for simple names like "tom, mary@foo.com, 123.45@c$.com"
   * but will fail on certain complex (but RFC-valid) names like
   * "(Darwin, Ian) <ian@darwinsys.com>".
   * Or even "Ian Darwin <ian@darwinsys.com>".
   * @param s the string of some list
   * @return the list after split
   */
  protected ArrayList tokenize(String s) {
    ArrayList al = new ArrayList();
    StringTokenizer tf = new StringTokenizer(s, ",");
    // For each word found in the line
    while (tf.hasMoreTokens()) {
      // trim blanks, and add to list.
      al.add(tf.nextToken().trim());
    }
    return al;
  }
}
///////////////////////////////////////////////////////////////////////

二、创建一个properties文件:

MailClient.properties:
///////////////////////////////////////////////////////////////////////
# This file contains my default Mail properties.
#
# Values for sending
Mail.address=xx@zsu.edu.cn
Mail.send.proto=smtp
Mail.send.host=student.zsu.edu.cn
Mail.send.debug=true
#
# Values for receiving
Mail.receive.host=student.zsu.edu.cn
Mail.receive.protocol=pop3
Mail.receive.user=xx
Mail.receive.pass=ASK
Mail.receive.root=d:\test
///////////////////////////////////////////////////////////////////////

三、创建主程序,生成Mailer.java里面的Mailer类的对象,设置参数,发出邮件。

首先import:
///////////////////////////////////////////////////////////////////////
import java.io.*;
import java.util.*;
import javax.mail.internet.*;
import javax.mail.*;
import javax.activation.*;
///////////////////////////////////////////////////////////////////////

然后用下面的代码完成发送:
///////////////////////////////////////////////////////////////////////
    try {
      Mailer m = new Mailer();

      FileProperties props =
          new FileProperties(MailConstants.PROPS_FILE_NAME);
      String serverHost = props.getProperty(MailConstants.SEND_HOST);
      if (serverHost == null) {
        System.out.println("\"" + MailConstants.SEND_HOST +
                                      "\" must be set in properties");
        System.exit(0);
      }
      m.setServer(serverHost);

      String tmp = props.getProperty(MailConstants.SEND_DEBUG);
      m.setVerbose(tmp != null && tmp.equals("true"));

      String myAddress = props.getProperty("Mail.address");
      if (myAddress == null) {
        System.out.println("\"Mail.address\" must be set in properties");
        System.exit(0);
      }
      m.setFrom(myAddress);

//以下根据具体情况设置:===============================================
      m.setToList("xx@zsu.edu.cn");//收件人
      m.setCcList("xx@163.com,yy@163.com");//抄送,每个地址用逗号隔开;或者用一个ArrayList的对象作为参数
      // m.setBccList(bccTF.getText());

      m.setSubject("demo");//主题

      // Now copy the text from the Compose TextArea.
      m.setBody("this is a demo");//正文
      // XXX I18N: use setBody(msgText.getText(), charset)
     
      ArrayList v=new ArrayList();
      v.add("d:\\test.htm");
      m.setAccessories(v);//附件
//以上根据具体情况设置=================================================
      // Finally, send the sucker!
      m.doSend();

    }
    catch (MessagingException me) {
      me.printStackTrace();
      while ( (me = (MessagingException) me.getNextException()) != null) {
        me.printStackTrace();
      }
      System.out.println("Mail Sending Error:\n" + me.toString());
    }
    catch (Exception ex) {
      System.out.println("Mail Sending Error:\n" + ex.toString());
    }
///////////////////////////////////////////////////////////////////////

 

 

分享到:
评论

相关推荐

    06 使用JavaMail发送带附件的邮件.exe

    06 使用JavaMail发送带附件的邮件.exe06 使用JavaMail发送带附件的邮件.exe

    JavaMail(3)——发送带附件的邮件.docx

    以上就是使用JavaMail发送带有附件的邮件的基本步骤。这个过程需要正确配置邮件服务器的属性,如SMTP服务器地址、端口、用户名和密码(如果需要身份验证)。`MailSenderInfo` 类通常会封装这些配置信息。 总的来说...

    JavaMail发送带附件的邮件

    以上就是使用JavaMail发送带附件的邮件的基本流程。需要注意的是,实际应用中可能需要处理更多复杂情况,例如错误处理、SSL/TLS加密连接、多部分邮件(包含HTML内容和文本内容)等。同时,确保你有正确的SMTP服务器...

    Gmail邮件通过JavaMail发送(不带附件与带一个附件)

    在使用JavaMail发送邮件前,你需要在项目中引入相关的依赖包。在描述中提到的"javamail"可能是指JavaMail的jar包,这是进行邮件操作的基础。确保你的项目已包含以下两个关键的JavaMail库: 1. `javax.mail-api.jar`...

    JavaMail发送邮件(文本、html、附件)

    以上就是使用JavaMail发送文本、HTML和附件邮件的详细步骤。在实际应用中,还需要考虑错误处理、邮件格式验证、多线程发送等复杂情况。同时,对于企业级应用,通常会使用邮件服务提供商如SendGrid、Mailgun等,它们...

    james和javamail实现邮件收发(带发送附件)

    下面是一个简化的JavaMail发送邮件的过程: 1. **配置邮件会话**:首先,我们需要创建一个`Properties`对象,并设置SMTP服务器的相关参数,如主机名和端口号。然后,通过`Session.getInstance()`方法初始化一个邮件...

    javamail发送邮件(带附件)

    这个资源提供了一个具体的示例,展示了如何使用JavaMail API 来发送带有附件的邮件。以下是对这个主题的详细解释: 首先,你需要理解JavaMail的核心组件。`javax.mail` 和 `javax.mail.internet` 包含了发送邮件所...

    javaMail发送邮件 发送成功 收不到邮件 或收到邮件无主题无收件人乱码

    ### JavaMail发送邮件时遇到的问题及解决方法 在使用JavaMail进行邮件发送的过程中,可能会遇到以下几种常见问题:发送成功但收件方未收到邮件、邮件收到后无主题或无收件人信息以及邮件内容出现乱码等情况。本文将...

    用javaMail发送邮件

    以上就是使用JavaMail发送邮件的基本过程。在实际开发中,可能还需要处理更多复杂情况,如SSL/TLS加密、HTML邮件、多部分消息等。JavaMail API提供了丰富的功能来应对这些需求,让开发者能够灵活地构建邮件系统。 ...

    JavaMail邮件发送(带附件)

    JavaMail邮件发送(带附件)

    javamail发送、接收邮件

    给定的部分内容展示了如何使用JavaMail API发送带有附件的邮件。代码中使用了`MimeMessage`、`MimeBodyPart`、`MimeMultipart`等类来构建邮件消息,同时通过`DataHandler`和`FileDataSource`来处理邮件附件。此外,...

    JavaMail发送邮件的例子

    下面我们将深入探讨JavaMail的基本概念、如何使用JavaMail发送邮件以及在Struts2框架中的实现方式。 JavaMail API主要包括以下组件: 1. `javax.mail.Session`:是JavaMail的核心,负责配置邮件服务器的信息,如...

    使用JAVAMail代发邮件

    使用 JavaMail 库可以轻松地在 Java 应用程序中发送电子邮件。JavaMail 是一个 Java API,用于在 Java 应用程序中发送和接收电子邮件。它提供了一个抽象层,允许开发者使用不同的电子邮件协议,例如 SMTP、POP3 和 ...

    javamail发送邮件.zip

    这个“javamail发送邮件.zip”压缩包显然包含了一个示例项目,演示如何使用JavaMail API发送包含正文文本、图片以及附件的邮件。以下是对这个主题的详细解释: 1. **JavaMail API**: JavaMail API 是一组接口和类...

    使用javaMail发送邮件

    在本篇文章中,我们将深入探讨如何使用JavaMail API来实现邮件的发送功能。 首先,我们需要引入JavaMail所需的依赖库。在提供的文件列表中,我们看到有以下jar文件: 1. mail.jar:这是JavaMail的核心库,包含了...

    javamail 可带附件demo 源码

    JavaMail 是一个开源库,它提供了在Java应用程序中发送和接收电子邮件的标准API。这个源码示例是关于如何使用JavaMail来发送带有附件...这个源码示例是一个很好的起点,帮助开发者理解并应用JavaMail发送带附件的邮件。

    javamail发送html邮件

    ### JavaMail 发送 HTML 格式邮件的知识点详解 ...综上所述,使用 JavaMail 发送 HTML 格式的邮件涉及到邮件服务器的配置、邮件内容的构建等多个方面,通过合理的类设计和代码实现可以有效地完成这一任务。

    javamail实现邮箱与邮箱发送邮件

    下面是一个使用JavaMail发送邮件的详细示例代码: ```java import javax.mail.*; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class EmailSender { public ...

Global site tag (gtag.js) - Google Analytics