📄 emailmanage.java
字号:
MimeBodyPart messageBodyPart = new MimeBodyPart();
String type = "text/plain; charset=\"GB2312\"";
messageBodyPart.setContent(new String(body.getBytes("gb2312")),
type);
// out.println("start:"+new String(Body.getBytes("gb2312")));
multipart.addBodyPart(messageBodyPart);
newMessage.setContent(multipart);
InternetAddress[] iadd = new InternetAddress[1];
iadd[0] = new InternetAddress(reveiver);
newMessage.setRecipients(Message.RecipientType.TO, iadd);// 设置收件人,并设置其接收类型为TO
newMessage.saveChanges();
Transport transport = s.getTransport("smtp");
try {
transport.connect(hostname, sender, password);// 以smtp方式登录邮箱
} catch (Exception ex) {
try {
transport.connect(hostname, user, password);// 以smtp方式登录邮箱
} catch (Exception e) {
ret = -1;
}
}
transport.sendMessage(newMessage, newMessage.getAllRecipients());// 发送邮件,其中第二个参数是所有
transport.close();
} catch (Exception ex) {
ret = -2;
}
return ret;
}
public static String formatHTML(String strValue) {
// System.out.println(strValue);
int p = 0;
if (strValue == null)
return "";
int len = 0;
if (strValue.indexOf("<") == -1)
return strValue;
if (strValue.substring(0, 5).equals("<pre>"))
return strValue;
String returnValue = strValue.substring(0, strValue.indexOf("<"));
len = strValue.indexOf("<", len);
while (len != -1) {
p = strValue.indexOf(">", len) + 1;
String tmpValue = strValue.substring(strValue.indexOf("<", len), p);
String vlist[] = tmpValue.split(" ");
tmpValue = "";
for (int i = 0; i < vlist.length; i++) {
// System.out.println(i+" : "+vlist[i]);
tmpValue += " "
+ vlist[i].replaceAll("\\n", "").replaceAll("\\r", "");
}
returnValue += tmpValue;
len = strValue.indexOf("<", p);
if (len != -1) {
returnValue += strValue.substring(p, len);
} else
returnValue += strValue.substring(p);
// len = p;
// System.out.println(returnValue);
}
return returnValue;
}
public static String formateSingleQuotes(String strValue){
if(strValue==null) return "";
return strValue.replaceAll("'","''").replaceAll("\"",""");
}
public static Integer getNoReadMailNumber(String emailDirPath)
{
String[] emaillist=new File(emailDirPath).list();
float emaillength =0f;
int NoRead = 0;
int listlen = 0;
if(emaillist!=null){
for(int i=0;i<emaillist.length;i++)
{
if(emaillist[i].endsWith(".xml"))
{
String tmpstr=XMLTool.getNodeValue(emailDirPath+emaillist[i],"isRead");
listlen++;
if(tmpstr.equals("0"))
{
NoRead++;
}
emaillength += (new File(emailDirPath+emaillist[i])).length();
}
}}
return(new Integer(NoRead));//news
}
public static Map xmlEmailToMap(Element email)
{
Map map=new HashMap();
for(int i=0;i<email.elements().size();i++)
{
Element node=(Element)email.elements().get(i);
map.put(node.getName(),node.getText());
}
return map;
}
public static boolean doDelAddress(String addressPath,Map keys)
{
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
if(node.attributeValue("id").equals(keys.get("id").toString()))
{
doc.elements().remove(i);
}
}
}
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
}
return true;
}
public static boolean doModifyAddress(String addressPath,Map keys)
{
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
if(node.attributeValue("id").equals(keys.get("id").toString()))
{
node.element("name").setText(keys.get("name").toString());
node.element("email").setText(keys.get("email").toString());
}
}
}
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
}
return true;
}
public static boolean doAddAddress(String addressPath,Map keys)
{
String Name=CommUtil.null2String(keys.get("name"));
String Email=CommUtil.null2String(keys.get("email"));
String ID =CommUtil.getNewFileID("","");
Element doc=null;
try{
doc=XMLTool.load(addressPath);
}
catch(Exception e)
{
}
if(doc==null)doc=DocumentHelper.createElement("root");
Element address=DocumentHelper.createElement("address").addAttribute("id",ID);
address.add(DocumentHelper.createElement("name").addText(Name));
address.add(DocumentHelper.createElement("email").addText(Email));
doc.add(address);
try{
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
return false;
}
return true;
}
public static List doListAddress(String addressPath,IPageList plist,int pageSize,int pageNo)
{
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
plist.setQuery(new ListQuery(doc.elements()));
plist.doList(pageSize,pageNo,"","");
List r=plist.getResult();
List tag=new ArrayList();
if(r!=null)
{
for(int i=0;i<r.size();i++)
{
Map map=new HashMap();
map.put("id",((Element)r.get(i)).attributeValue("id"));
map.put("name",((Element)r.get(i)).element("name").getText());
map.put("email",((Element)r.get(i)).element("email").getText());
tag.add(map);
}
}
return tag;
}
}
catch(Exception e)
{
}
return null;
}
public static Map getAddress(String addressPath,String id)
{
Map map=new HashMap();
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
logger.debug(id);
if(node.attributeValue("id").equals(id))
{
map.put("id",node.attributeValue("id"));
map.put("name",node.element("name").getText());
map.put("email",node.element("email").getText());
}
}
}
}
catch(Exception e)
{
}
return map;
}
/*
* <root><address-group id="200510301309362705" name="">
* <address>test</address></address-group></root>
*/
public static boolean doDelAddressGroup(String addressPath,Map keys)
{
try{
Element doc=XMLTool.load(addressPath);
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
if(node.attributeValue("id").equals(keys.get("id").toString()))
{
doc.elements().remove(i);
}
}
}
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
}
return true;
}
public static boolean doModifyAddressGroup(String addressPath,Map keys)
{
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
String id=CommUtil.null2String(keys.get("id"));
if(node.attributeValue("id").equals(id))
{
node.element("name").setText(CommUtil.null2String(keys.get("name")));
node.remove(node.element("address"));
String addrs=CommUtil.null2String(keys.get("address"));
String[] add=addrs.split(",");
for(int j=0;j<add.length;j++)
{
node.elements().add(DocumentHelper.createElement("address").addText(add[j]));
}
}
}
}
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
}
return true;
}
public static boolean doAddAddressGroup(String addressPath,Map keys)
{
String Name=CommUtil.null2String(keys.get("name"));
String addrs=CommUtil.null2String(keys.get("address"));
String[] add=addrs.split(",");
String ID =CommUtil.getNewFileID("","");
Element doc=null;
try{
doc=XMLTool.load(addressPath);
}
catch(Exception e)
{
}
if(doc==null)doc=DocumentHelper.createElement("root");
Element aNode=DocumentHelper.createElement("address-group").addAttribute("id",ID);
aNode.addAttribute("name",Name);
for(int i=0;i<add.length;i++)
aNode.elements().add(DocumentHelper.createElement("address").addText(add[i]));
doc.elements().add(aNode);
try{
XMLTool.save(doc,addressPath);
}
catch(Exception e)
{
return false;
}
return true;
}
public static List doListAddressGroup(String addressPath,IPageList plist,int pageSize,int pageNo)
{
try{
Element doc=XMLTool.load(addressPath);
logger.debug("total address:"+doc.elements().size());
if(doc!=null){
plist.setQuery(new ListQuery(doc.elements()));
plist.doList(pageSize,pageNo,"","");
List r=plist.getResult();
List tag=new ArrayList();
if(r!=null)
{
for(int i=0;i<r.size();i++)
{
Map map=new HashMap();
map.put("id",((Element)r.get(i)).attributeValue("id"));
map.put("name",((Element)r.get(i)).attributeValue("name"));
String addrs="";
List clist=((Element)r.get(i)).elements("address");
for(int j=0;j<clist.size();j++)addrs+=((Element)clist.get(j)).getText()+",";
map.put("address",addrs);
map.put("addrLength",new Integer(clist.size()));
tag.add(map);
}
}
return tag;
}
}
catch(Exception e)
{
}
return null;
}
public static Map getAddressGroup(String addressPath,String id)
{
Map map=new HashMap();
try{
Element doc=XMLTool.load(addressPath);
//logger.debug("total address:"+doc.getChildren().size());
if(doc!=null){
for(int i=0;i<doc.elements().size();i++)
{
Element node=(Element)doc.elements().get(i);
//logger.debug(id);
if(node.attributeValue("id").equals(id))
{
map.put("id",node.attributeValue("id"));
map.put("name",node.attributeValue("name"));
String addrs="";
List clist=node.elements("address");
for(int j=0;j<clist.size();j++)addrs+=((Element)clist.get(j)).getText()+",";
map.put("address",addrs);
map.put("addrLength",new Integer(node.elements().size()));
}
}
}
}
catch(Exception e)
{
}
return map;
}
public static void main(String[] args) {
ActiveUser user=UserManage.login("csy","cai","csy","cai","127.0.0.1");
if(user!=null)
{
System.out.println("domain:"+user.getServerDomain());
System.out.println("host:"+user.getServerHost());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -