gmailutils.java
来自「Struts2 + Spring JPA Hibernate demo.」· Java 代码 · 共 137 行
JAVA
137 行
package com.vegeta.utils;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import javax.activation.MimeType;
import javax.mail.Authenticator;
import javax.mail.FetchProfile;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
public class GMailUtils {
public static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
public static String login = "";
public static String pwd = "";
protected static String decodeText(String text)
throws UnsupportedEncodingException {
if (text == null)
return null;
if (text.startsWith("=?GB") || text.startsWith("=?gb"))
text = MimeUtility.decodeText(text);
else
text = new String(text.getBytes("ISO8859_1"));
return text;
}
@SuppressWarnings("unchecked")
public static boolean filter(Part part) {
Object o = null;
try {
for (Enumeration e = part.getAllHeaders(); e.hasMoreElements();) {
Header header = (Header) e.nextElement();
System.out.println(header.getName() + ": " + header.getValue());
}
System.out.println("\n");
o = part.getContent();
} catch (Exception e) {
System.out.println("Couldn't get content.");
return false;
}
if (o instanceof String) {
String lMessage = (String) o;
System.out.println(lMessage);
return true;
} else if (o instanceof Multipart) {
Multipart lPart = (Multipart) o;
try {
int count = lPart.getCount();
for (int i = 0; i < count; i++)
return filter(lPart.getBodyPart(i));
} catch (Exception e) {
System.out.println("Couldn't access parts.");
e.printStackTrace();
}
} else if (o instanceof Message) {
return filter((Part) o);
} else if (o instanceof InputStream) {
BufferedReader lReader = new BufferedReader(new InputStreamReader(
(InputStream) o));
int c;
StringBuffer lBuffer = new StringBuffer();
try {
while ((c = lReader.read()) != -1)
lBuffer.append((char) c);
lReader.close();
} catch (Exception e) {
System.out.println("Read error.");
e.printStackTrace();
}
System.out.println(lBuffer.toString());
return true;
}
return false;
}
// s
public static String getMailContent(Part part) throws IOException,
MessagingException {
Object o = part.getContent();
String lMessage = "";
if (o instanceof String) {
lMessage = (String) o;
System.out.println(lMessage);
} else if (o instanceof Multipart) {
Multipart lPart = (Multipart) o;
try {
int count = lPart.getCount();
for (int i = 0; i < count; i++)
return getMailContent(lPart.getBodyPart(i));
} catch (Exception e) {
System.out.println("Couldn't access parts.");
e.printStackTrace();
}
} else if (o instanceof Message) {
return getMailContent((Part) o);
} else if (o instanceof InputStream) {
BufferedReader lReader = new BufferedReader(new InputStreamReader(
(InputStream) o));
int c;
StringBuffer lBuffer = new StringBuffer();
try {
while ((c = lReader.read()) != -1)
lBuffer.append((char) c);
lReader.close();
} catch (Exception e) {
System.out.println("Read error.");
e.printStackTrace();
}
}
return lMessage;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?