Jmail 接收邮件:
import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;
import java.util.*;
import javax.mail.*;
import java.io.*;
public class ReceiveMail {
public ReceiveMail() {
}
public static void main(String[] args) throws Exception {
ReceiveMail receivemail = new ReceiveMail();
receivemail.receive();
}
public void receive() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.pop3.host", "pop3.sina.com");
prop.setProperty("mail.pop3.auth", "true");
MyAuthenticator auth = new MyAuthenticator();
Session session = Session.getDefaultInstance(prop, auth);
Store store = session.getStore("pop3");
store.connect("pop3.sina.com", "wpabbs@sina.com", "邮箱的密码");
Folder defaultFolder = store.getDefaultFolder();
Folder folder = defaultFolder.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
Message[] message = folder.getMessages();
for (int i = 0; i < message.length; i++) {
System.out.println(
"-------------------第" + i + " 封邮件------------");
Message msg = message;
System.out.println("邮件标题:" + msg.getSubject());
System.out.println("邮件正文:" + msg.getContent());
if (msg.getContent() instanceof Multipart) {
Multipart mp = (Multipart) msg.getContent();
for (int t = 0; t < mp.getCount(); t++) {
BodyPart part = mp.getBodyPart(t);
String fileName = part.getFileName();
if(fileName==null)
{
System.out.println(part.getContent());
}else
{
InputStream in=part.getInputStream();
byte[] date = new byte[in.available()];
in.read(date);
FileOutputStream out = new FileOutputStream("c://"+fileName);
out.write(date);
System.out.println("文件 "+fileName +"保存在c://");
}
}
}
}
}
public class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("wpabbs", "邮箱的密码");
}
}
}
Jmail带附件的邮件发送:
import javax.mail.*;
import java.util.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMailAddFile {
public SendMailAddFile() {
}
public static void main(String[] args) throws Exception {
SendMailAddFile send = new SendMailAddFile();
send.sendFile();
}
public void sendFile() throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.smtp.host","smtp.sina.com");
prop.setProperty("mail.smtp.auth","true");
MyAuthenticator authenticator = new MyAuthenticator();
Session session = Session.getInstance(prop,authenticator);
MimeMessage message = new MimeMessage(session);
Address from = new InternetAddress("wpabbs@sina.com"); //你的邮箱
Address to = new InternetAddress("要发送的Email"); //她的邮箱
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject("o(∩_∩)o...哈哈");
Multipart mpart = new MimeMultipart();
MimeBodyPart body = new MimeBodyPart();
body.setText("测试带附件的邮件发送情况");
mpart.addBodyPart(body);
body = new MimeBodyPart();
DataSource ds = new FileDataSource("c://1.jpg");
DataHandler dh = new DataHandler(ds);
body.setDataHandler(dh);
body.setFileName("readme.jpg");
mpart.addBodyPart(body);
message.setContent(mpart);
Transport trans = session.getTransport("smtp");
trans.connect("smtp.sina.com","用户名","密码"); //你的用户名及密码
trans.send(message,message.getAllRecipients());
trans.close();
System.out.println("send ok!");
}
public class MyAuthenticator extends Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("用户名","密码"); //你的用户名及密码
}
}
}
分享到:
相关推荐
利用Jmail发送邮件:收邮件利用Jmail发送邮件:利用Jmail发送邮件:利用Jmail发送邮件:利用Jmail发送邮件:
利用JMail收发邮件,可以修改后检测垃圾邮件
jmail发送邮件简要代码,是邮件发送的核心代码,供参考
利用jmail实现简单邮件的接收和发送功能,已经过本人测试,完整方案
VFP利用JMAIL发送邮件及附件,纯代码,效果好VFP利用JMAIL发送邮件及附件,纯代码,效果好
asp.net中使用Jmail发送邮件
jmail 发送邮件的源代码、 txt 文本形式
利用Jmail发送邮件,参考网上资料改良,在自己的程序中结合MS exchange多次使用,经过测试也可以适用于公共邮箱。
邮件发送系统主要由【重新登录】、【邮件群发】、【添加好友】、【查看好友】、【查看邮件信息】、【查看紧急邮件】等模块组成,进入邮件发送系统后,可将全部好友的Email添加到该程序里,使发送信息时能进行多个...
一个详细的例子,java jmail 发送邮件,绝对容易理解和接受
使用JMAIL收发邮件,附有JMAIL安装程序,JMAIL程序集及收发源码,经过实际测试,无乱码,无附件丢失,绝对正常
开发环境vs2008,语言C#,功能利用jmail组件发送电子邮件,项目中包含jmail及使用说明安装包。
ASP使用Jmail发送邮件的函数及使用方法
目前网上流行的jmail邮件发送资料不是很实用,要么发送不成功,要么发送失败,本实例是作者本人从事开发的实践经验,拿出来与大家共享,内有详细的实用说明教程,如果有任何疑问请联系QQ278742549
jmail 发送邮件控件,支持winxp win200 win2003 版本4.3
asp.net利用Jmail发送邮件和接收全部代码
Jmail发送邮件系统源码.zip
jmail是一个第三方邮件操作组件,通常位于web服务器端,和站点程序紧密配合来接收及提交邮件到邮件服务器的控件,让网站拥有发送邮件既接收邮件的功能 与此相同的还有CDONTS.NewMail,Persits.MailSender,IISmail...
Jmail发送邮件系统源码_jmailsend