java mail学习笔记——Message类详解

Message类
在Java Mail 中Message类是所有电子邮件的的超类它的定义如下:
public abstract class javax.mail.Message implements javax.mail.Part

1.标准的Java Mail API中有一个Message的子类:MimeMessage,它可用于电子邮件和Usenet新闻消息。除此之外,其他厂商可以自由扩展Message来满足自身需求。
Message类主要声明了定义大多数消息公共属性的抽象获取和设置方法。这些属性包括
(1)消息地址
(2)消息接收方
(3)消息主题和主体等
可以将这些属性视为包含消息的信封。

2.Message还实现了Part接口。Part接口用于处理消息的主体

创建消息
Message类有三个构造函数可以创建消息:
protected Message();
protected Message(Folder folder,int messageNumber);
protected Message(Session);
其中,第二个方法的参数说明如下:
folder - 包含文件夹
msgnum - 在这个文件夹中消息的序列号
回复消息
如果已经有了一个Message对象,要创建一个新的Message对象,可以使用:
public abstract Message reply(boolean replyAll)throws MessagingException
此方法用加了前缀“Re:”的相同主题和最初消息的发送方地址创建一个新的Message对象。如果参数为true,
消息会寻址到最初消息的所有接收方。消息的内容为空。如果要引用初始消息,就必须自己来完成这个工作。

From地址
下面四个方法用来获得和设置消息的“From”
public abstract Address[] getFrom()
                           throws MessagingException
public abstract void setFrom()
                      throws MessagingException
public abstract void addFrom(Address[] addresses)
                      throws MessagingException
public abstract void addFrom(Address[] addresses)
                      throws MessagingException

Reply-to地址
有些消息包含一个Reply-to,指示回复消息应当发送到与发送消息不同的地址。有两个方法可以设置和获得这些地址:
public Address[] getReplyTo()
                     throws MessagingException

Get the addresses to which replies should be directed.
public void setReplyTo(Address[] addresses)
                throws MessagingException
Set the addresses to which replies should be directed
接收方地址
消息的发送方一般只存在于From:首部中,而消息的接收方却分为To,Cc,Bcc三个字段。在Java Mail中,这三个字段分别是:
Message.RecipientType.TO
Message.RecipientType.CC
Message.RecipientType.BCC

获取Message的接收方地址有两个方法:
public abstract Address[] getRecipients(Message.RecipientType type)
                                 throws MessagingException
public Address[] getAllRecipients()
                           throws MessagingException
消息主题
主要有两个方法:
public abstract void setSubject(String subject)
                         throws MessagingException
public abstract String getSubject()
                           throws MessagingException
消息日期
消息还有发送、接收日期:
public abstract Date getSentDate()
                          throws MessagingException
public abstract void setSentDate(Date date)
                          throws MessagingException
public abstract Date getReceivedDate()
                              throws MessagingException

package com.mail;  
  
import java.util.Date;  
import java.util.Properties;  
  
import javax.mail.*;  
import javax.mail.internet.InternetAddress;  
  
public class HeaderClient {  
  
    /** 
     * @param args 
     */  
    public static void main(String[] args) {  
        try {  
            Properties props=new Properties();    
            props.setProperty("mail.transport.protocol", "pop3");    
            props.setProperty("mail.host", "pop3.sina.com");  
            Session session=Session.getDefaultInstance(props,   
                    new Authenticator()    
            {    
  
                protected PasswordAuthentication getPasswordAuthentication()    
                {    
                    return new PasswordAuthentication("yananlemon@sina.com","111111");    
                }    
            }  );  
  
            //连接服务器,并打开文件夹  
             Store store=session.getStore("pop3");    
                store.connect("pop3.sina.com", "yananlemon@sina.com", "1111111");  
            Folder folder=store.getFolder("INBOX");  
            if(folder==null){  
                System.out.println("Folder not found!");  
                System.exit(1);  
            }  
            folder.open(Folder.READ_ONLY);  
            //从服务器获取消息  
            Message[] ms=folder.getMessages();  
            for(int i=0;i<ms.length;i++){  
                System.out.println("--------------Message"+(i+1)+"---------------");  
                String from =InternetAddress.toString(ms[i].getFrom());  
                if(from!=null){  
                    System.out.println("消息来自:"+from);  
                }  
                String to=InternetAddress.toString(ms[i].getRecipients(Message.RecipientType.TO));  
                if(to!=null){  
                    System.out.println("消息去往:"+to);  
                }  
                String replyTo=InternetAddress.toString(ms[i].getReplyTo());  
                if(replyTo!=null){  
                    System.out.println("消息回复给:"+replyTo);  
                }  
                String cc=InternetAddress.toString(ms[i].getRecipients(Message.RecipientType.CC));  
                if(cc!=null){  
                    System.out.println("消息抄送:"+cc);  
                }  
                Date sent=ms[i].getSentDate();  
                if(sent!=null){  
                    System.out.println("消息发送时间::"+sent);  
                }  
                String subject=ms[i].getSubject();  
                if(subject!=null){  
                    System.out.println("消息主题:"+subject);  
                }  
                Date received=ms[i].getReceivedDate();  
                if(received!=null){  
                    System.out.println("消息接收时间:"+received);  
                }  
                System.out.println();  
  
            }  
            folder.close(false);  
            store.close();  
        } catch (MessagingException e) {  
            e.printStackTrace();  
        }  
    }  
  
}  

 



下面是输出:



 

javaMail的详细文档,都有以下多有的详细信息: ACL Address AddressException AddressStringTerm AddressTerm AndTerm AuthenticationFailedException Authenticator BodyPart BodyTerm ByteArrayDataSource ComparisonTerm ConnectionAdapter ConnectionEvent ConnectionListener ContentDisposition ContentType DateTerm DeliveryStatus DispositionNotification FetchProfile FetchProfile.Item Flags Flags.Flag FlagTerm Folder FolderAdapter FolderClosedException FolderEvent FolderListener FolderNotFoundException FromStringTerm FromTerm Header HeaderTerm HeaderTokenizer HeaderTokenizer.Token IllegalWriteException IMAPFolder IMAPFolder.FetchProfileItem IMAPFolder.ProtocolCommand IMAPMessage IMAPSSLStore IMAPStore IntegerComparisonTerm InternetAddress InternetHeaders InternetHeaders.InternetHeader MailDateFormat MailEvent MailHandler MailSSLSocketFactory Message Message.RecipientType MessageAware MessageChangedEvent MessageChangedListener MessageContext MessageCountAdapter MessageCountEvent MessageCountListener MessageHeaders MessageIDTerm MessageNumberTerm MessageRemovedException MessagingException MethodNotSupportedException MimeBodyPart MimeMessage MimeMessage.RecipientType MimeMultipart MimePart MimePartDataSource MimeUtility Multipart MultipartDataSource MultipartReport NewsAddress NoSuchProviderException NotTerm OrTerm ParameterList ParseException Part PasswordAuthentication POP3Folder POP3Message POP3SSLStore POP3Store PreencodedMimeBodyPart Provider Provider.Type Quota Quota.Resource QuotaAwareStore ReadOnlyFolderException ReceivedDateTerm RecipientStringTerm RecipientTerm Report Rights Rights.Right SearchException SearchTerm SendFailedException SentDateTerm Service Session SharedByteArrayInputStream SharedFileInputStream SharedInputStream SizeTerm SMTPAddressFailedException SMTPAddressSucceededException SMTPMessage SMTPSendFailedException SMTPSSLTransport SMTPTransport Store StoreClosedException StoreEvent StoreListener StringTerm SubjectTerm Transport TransportAdapter TransportEvent TransportListener UIDFolder UIDFolder.FetchProfileItem URLName
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值