一
SMTP 的全稱是
POP
而IMAP(Internet Mail Access Protocol)提供webmail 與電子郵件客戶端之間的雙向通信
同時
總之
注
二
package com
import java
import java
import java
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import javax
/**
* @author like
* @E
* @CreateTime
*/
public class SMTPSendTest {
private static final int RECEIPT =
private static final String attachmentDir =
public static void sendEmail(Email emailInfo) throws UnsupportedEncodingException
Properties props = new Properties()
props
props
props
Authenticator auth = new SMTPAuthenticator(emailInfo
Session session = Session
session
Message msg = new MimeMessage(session)
msg
msg
msg
msg
if (emailInfo
msg
}
msg
// 設置郵件內容(包括附件的HTML格式內容)
msg
msg
Transport
}
/**
* 封裝郵件地址
*
* @param address
* @return
* @throws AddressException
*/
private static InternetAddress[] getEmailRecipient(ArrayList<String> address) throws AddressException {
int toLen =
if (address != null) {
toLen = address
}
InternetAddress[] addressTo = new InternetAddress[toLen];
if (toLen !=
String m_st_email =
for (int i =
m_st_email = (String) address
if (m_st_email != null)
addressTo[i] = new InternetAddress(m_st_email
}
}
return addressTo;
}
private static Multipart getMultipart(String text
// 混合型郵件內容
Multipart multi = new MimeMultipart(
// 加入文本內容
multi
// 加入附件內容
for (int i =
String attachmentI = (String) attachment
// 附件的真是存儲名稱
String fileRealName = attachmentI
// 附件在郵件中的顯示名稱
String fileShowName = attachmentI
multi
}
return multi;
}
/**
* 郵件附件的處理
*
* @param fileName
* 附件顯示名稱
* @param file
* 文件
* @return javax
* @throws MessagingException
*/
private static BodyPart createAttachment(String fileName
BodyPart attach = new MimeBodyPart()
DataSource ds = new FileDataSource(file)
attach
try {
// 文件名重新編碼
attach
} catch (UnsupportedEncodingException e) {
e
}
return attach;
}
/**
* 郵件文本內容的處理
*
* @param text
* 文本內容
* @return javax
* @throws MessagingException
*/
private static BodyPart createContent(String text) throws MessagingException {
BodyPart content = new MimeBodyPart()
// 郵件正文也是一種組合消息
Multipart relate = new MimeMultipart(
BodyPart html = new MimeBodyPart()
html
relate
content
return content;
}
}
郵件內容結構圖
三
package com
import java
import javax
import javax
import javax
import javax
import javax
import javax
import javax
import com
/**
* POP
* @author like
* @E
* @CreateTime
*/
public class OP
public static void receive(String server
Properties props = System
Authenticator auth = new POPAuthenticator(username
Session session = Session
session
Store store = session
nnect(server
//獲得收件箱
POP
try {
//讀寫方式打開
folder
} catch (MessagingException ex) {
//制度方式打開
folder
}
// int totalMessages = folder
Message m_message = null;
Message[] msgs = folder
for (int i =
m_message = msgs[i];
String UID = folder
if (haveReceived(UID)) {
// 插入數據庫
// mailList
// p_st_attachmentParentDir
// 設置為已讀
m_message
// POP
m_message
}
}
}
/**
* POP
* 服務器會給每一封郵件一個獨一無二的UID
* 有兩種方式
* 優點是只讀一次數據庫
* (
*
* @param UID
* @return
*/
private static boolean haveReceived(String UID) {
//個人覺得第一種較好
//第一種注意把取得的UID集放入到Set集合中
return true;
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26993.html