java限制金额控制_JavaMail 限额管理

JavaMail配额是限定或固定的号码或邮件的数量在电子邮件存储。JavaMail API为每个邮件服务请求调用计数配额。电子邮件服务可以适用下列配额标准:

外发邮件(包括附件)的最大大小。

邮件信息,包括附件的最大大小。

消息的最大大小时,管理员是一个收件人

对于配额管理的JavaMail有以下类别:

Class

描述

public class Quota

This class represents a set of quotas for a given quota root. Each quota root has a set of resources, represented by the Quota.Resource class. Each resource has a name (for example, "STORAGE"), a current usage, and a usage limit. This has only one method setResourceLimit(String name, long limit).

public static class Quota.Resource

Represents an individual resource in a quota root.

public interface QuotaAwareStore

An interface implemented by Stores that support quotas. The getQuota and setQuota methods support the quota model defined by the IMAP QUOTA extension.GmailSSLStore, GmailStore, IMAPSSLStore, IMAPStore are the known implementing classes of this interface.

让我们来看看和例子在下面的章节会检查邮件存储名称,并限制其使用。

创建Java类

创建一个Java类文件QuotaExample,是其内容如下:

packagecom.yiibai;importjava.util.Properties;importjavax.mail.Quota;importjavax.mail.Session;importjavax.mail.Store;importcom.sun.mail.imap.IMAPStore;publicclassQuotaExample{publicstaticvoidmain(String[]args){try{Propertiesproperties=newProperties();properties.put("mail.store.protocol","imaps");properties.put("mail.imaps.port","993");properties.put("mail.imaps.starttls.enable","true");SessionemailSession=Session.getDefaultInstance(properties);// emailSession.setDebug(true);// create the IMAP3 store object and connect with the pop serverStorestore=emailSession.getStore("imaps");//change the user and password accordinglystore.connect("imap.gmail.com","abc@gmail.com","*****");IMAPStoreimapStore=(IMAPStore)store;System.out.println("imapStore ---"+imapStore);//get quotaQuota[]quotas=imapStore.getQuota("INBOX");//Iterate through the Quotasfor(Quotaquota:quotas){System.out.println(String.format("quotaRoot:'%s'",quota.quotaRoot));//Iterate through the Quota Resourcefor(Quota.Resourceresource:quota.resources){System.out.println(String.format("name:'%s', limit:'%s', usage:'%s'",resource.name,resource.limit,resource.usage));}}}catch(Exceptione){e.printStackTrace();}}}

这里是连接通过IMAP(imap.gmail.com)服务器的Gmail服务,为IMAPStore实现QuotaAwareStore。一旦你获得了存储对象,获取配额阵列和遍历并打印相关信息。

编译并运行

现在,我们的类是准备好了,让我们编译上面的类。我已经保存了类QuotaExample.java到目录 : /home/manisha/JavaMailAPIExercise. 我们需要javax.mail.jar 和 activation.jar在classpath中。执行下面的命令从命令提示符编译类(两个jar被放置在/home/manisha/ 目录下):

javac-cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:QuotaExample.java

现在,这个类被编译,执行下面的命令来运行:

java-cp/home/manisha/activation.jar:/home/manisha/javax.mail.jar:QuotaExample

验证输出

您应该看到类似的消息在命令控制台上:

imapStore ---imaps://abc%40gmail.com@imap.gmail.com

quotaRoot:''

name:'STORAGE', limit:'15728640', usage:'513'

¥ 我要打赏

纠错/补充

收藏

加QQ群啦,易百教程官方技术学习群

注意:建议每个人选自己的技术方向加群,同一个QQ最多限加 3 个群。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值