如何子类化Java Mail Message

时间:2014-02-24 10:54:04

标签: java class javamail

我是Java Mail API的新手。任何人都可以请告诉我如何我可以继承javamail消息,因为我需要这个来编组和解组代码。当我子类化时,我必须实现很多函数,这是子类化的方式吗?子类化后,会发生以下代码段:

<code>
public class MailMessage extends Message{

    @Override
    public void addHeader(String arg0, String arg1) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public Enumeration getAllHeaders() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Object getContent() throws IOException, MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getContentType() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public DataHandler getDataHandler() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getDescription() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getDisposition() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getFileName() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String[] getHeader(String arg0) throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public InputStream getInputStream() throws IOException, MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getLineCount() throws MessagingException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public Enumeration getMatchingHeaders(String[] arg0)
            throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Enumeration getNonMatchingHeaders(String[] arg0)
            throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int getSize() throws MessagingException {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isMimeType(String arg0) throws MessagingException {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void removeHeader(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setContent(Multipart arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setContent(Object arg0, String arg1) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setDataHandler(DataHandler arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setDescription(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setDisposition(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setFileName(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setHeader(String arg0, String arg1) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setText(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void writeTo(OutputStream arg0) throws IOException,
            MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void addFrom(Address[] arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void addRecipients(RecipientType arg0, Address[] arg1)
            throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public Flags getFlags() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Address[] getFrom() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getReceivedDate() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Address[] getRecipients(RecipientType arg0)
            throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Date getSentDate() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getSubject() throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Message reply(boolean arg0) throws MessagingException {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void saveChanges() throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setFlags(Flags arg0, boolean arg1) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setFrom() throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setFrom(Address arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setRecipients(RecipientType arg0, Address[] arg1)
            throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setSentDate(Date arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

    @Override
    public void setSubject(String arg0) throws MessagingException {
        // TODO Auto-generated method stub

    }

}
</code>

2 个答案:

答案 0 :(得分:2)

MailMessage由JavaMail API创建,因此很难更改它。 考虑为扩展javax.mail.Message的{​​{1}}创建一个包装器,并将所有Message方法委托给包装好的方法。然后将您的功能添加到包装类中。

示例:

Message's

答案 1 :(得分:0)

上面的包装方法工作正常,或者您可以只使用“容器”对象:

public class MyMessage implements Serializable {
    public MimeMessage message;

    public MyMessage(MimeMessage message) {
        this.message = message;
    }

    private void readObject(ObjectInputStream stream) throws IOException {
        // implement using MimeMessage(Session, InputStream) constructor
    }

    private void writeObject(ObjectOutputStream stream) throws IOException {
        // implement using MimeMessage.writeTo() method
    }
}

使用myMsg.message.getSubject()或其他任何内容引用真实消息。