package mail;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.mail.internet.MimeMessage.RecipientType;
/*******************************************************************************
* @author hanwesley java Mail exaple
*/
public class Email {
private static String context;
public void setContext(String context) {
this.context = context;
}
public static String getContext() {
return context;
}
public ArrayList<MimeBodyPart> getAttachmentMultipart(String[] ss)
throws Exception {
ArrayList<MimeBodyPart> parts = null;
if (ss != null && ss.length > 0) {
int filesize = 0;
for (String s : ss) {
File f = new File(s);
filesize += f.length();
if (filesize > 1 * 1024 * 1024) {
throw new Exception("attarch maxed!");
}
parts = new ArrayList<MimeBodyPart>();
for (int i = 0; i < ss.length; i++) {
MimeBodyPart mbp = new MimeBodyPart();
FileDataSource fds = new FileDataSource(ss[i]);
mbp.setDataHandler(new DataHandler(fds));
mbp.setFileName(MimeUtility.encodeText(fds.getName(),
"GB2312", "B"));
parts.add(mbp);
}
}
}
return parts;
}
public Session getSession() {
Properties prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", "smtp.163.com");
prop.put("mail.smtp.port", 25);
prop.put("mail.from", "hantx@163.com");
prop.put("mail.smtp.auth", "true");
Session session = null;// JavaMail核心类 一个会话
session = Session.getDefaultInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("hantx@163.com",
"123456");
}
});
// session=Session.getDefaultInstance(prop,null);
session.setDebug(true);
return session;
}
public void sendMessage(Message msg) throws Exception {
Transport transport = getSession().getTransport();
try {
// msg.setc
transport.connect();
// transport.connect("smtp.163.com", 25,
// "hantx@163.com","123456");
transport.send(msg);
} catch (MessagingException e) {
e.printStackTrace();
} finally {
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
public void receiveMessage() throws Exception{
Session session=getSession();
Store store=session.getStore("pop3");
store.connect("pop.163.com", "hantx@163.com", "123456");
Folder inbox=store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
Message msg=inbox.getMessage(1);
System.out.println(msg.getSubject());
}
public void testSendMessager() throws Exception {
Session session = getSession();
Message msg = new MimeMessage(session);
msg.setFrom();
msg.setSubject("hello,world");
String s = "hanwesley@163.com,hantx@163.com";
msg.setRecipients(RecipientType.TO, InternetAddress.parse(s));
msg.setRecipient(RecipientType.CC, new InternetAddress(
"hanwesley@gmail.com"));
// msg.setText("");
msg.setSentDate(new Date());
String[] arr = new String[] { "d:\\two.txt" };
Multipart mp = new MimeMultipart("mixed");
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent("it's a JavaMail example!<br/>hehe",
"text/html;charset=GB2312");
mp.addBodyPart(mbp);
ArrayList<MimeBodyPart> mbparr = getAttachmentMultipart(arr);
for (MimeBodyPart mb : mbparr) {
mp.addBodyPart(mb);
}
msg.setContent(mp);
sendMessage(msg);
}
public static void main(String[] args) throws Exception {
Email email = new Email();
email.receiveMessage();
}
}
分享到:
相关推荐
通过以上步骤,可以成功地搭建起一个具备基本邮件过滤和操作功能的邮件服务器,并通过Javamail实现邮件的收发功能。这种方式不仅提供了高度的灵活性和定制性,而且由于其基于纯Java技术的特性,也方便了跨平台部署和...
报告的结论是基于JavaMail的邮件客户端设计是一个功能强大且实用的电子邮件收发系统,可以为用户提供方便快捷的电子邮件服务。 报告的参考文献包括: [1] 谢希仁.计算机网络.第五版[M].北京:电子工业出版社,2008...
本资源“java邮件开发源代码”提供了一个很好的学习起点,特别适合那些正在入门阶段的Java学习者。下面我们将深入探讨Java邮件开发的相关知识点。 1. **JavaMail API**: JavaMail API是Java平台用于处理邮件的核心...
利用Hibernate进行多元化的级联操作-Web实例[Java_MySQL_jar]-数据库连接支持包[JavaMail]-收发邮件支持包[Java_MySQL_jar]Java访问数据库需要的包[Spring]基于Spring框架的Web应用演示(附带cglib工具进行动态代理)...
[JavaMail]-收发邮件支持包 [Java_MySQL_jar]Java访问数据库需要的包 [Spring]基于Spring框架的Web应用演示(附带cglib工具进行动态代理) [Tomcat7.0]Tomcat7版本安装包 [UltraISO]制作U盘启动盘需要的 [log4j_...
在技术实现上,MeyboMail Web利用了JavaMail API来处理邮件的发送与接收,这是一个强大的开源邮件处理库,支持SMTP、POP3、IMAP等多种邮件协议。同时,MeyboMail Web采用了Spring Boot框架,这使得项目的初始化、...