您的位置:首页 > 其它

2016年3月23日作业

2016-04-05 21:52 337 查看
package org.sparon.email.send;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class MailSender {
public final static boolean sendMail(String smtphost,String mailfrom,
boolean mailauth,String mailuser,String mailpassword,
String mailto,String mailcc,String mailsubject,
String mailmessage,String mailattach)
{

/*
* smtphost: smtp.163.com   smtp.126.com 设置系统属性
* mailfron: 发送邮件的邮箱地址
* mailto: 接受邮件的邮箱地址
* mailcc: 抄送人邮箱地址
* mailauth: true   设置smtp身份认证:mail.smtp.auth = true
* mailuser: 接受邮件的邮箱用户名
* mailpassword: 接受邮件的邮箱密码
* mailsubject: 邮件的主题
* mailmessage: 邮件的内容
* mailattach:发送附件的文件名,在本地机器上的绝对地址
*/
//MIME邮件对象
MimeMessage mimeMsg;
//Multipart对象,邮件内容,标题,附件等内容均添加到其中之后再生成//MimeMessage对象
MimeMultipart mp;
//邮件会话对象
Session session;
//    String sep[]={";"};
//系统属性
Properties props=new java.util.Properties();
int i;

//设置SMTP主机
props.put("mail.smtp.host",smtphost);
try{
//获得邮件会话对象
session = Session.getDefaultInstance(props,null);

mimeMsg = new MimeMessage(session);
mp = new MimeMultipart();
if(mailauth)
props.put("mail.smtp.auth","true");
else
props.put("mail.smtp.auth","false");
System.out.println("Mail Host Address: "+smtphost);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
try
{
//设置发信人
mimeMsg.setFrom(new InternetAddress(mailfrom));
System.out.println("Mail From Address: "+mailfrom);
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
try{
//        java.util.Vector temp = WordsConvert.getWords(mailto,sep);
java.util.Vector temp=new java.util.Vector(1);
temp.add("wangbaoaiboy@163.com");
if (temp.size()==0)
{
System.out.println("Mail Target Address Requried.");
return false;
}
Address toaddress[] = new Address[temp.size()];
for(i=0;i<temp.size();i++)
toaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
//设置收信人
mimeMsg.setRecipients(Message.RecipientType.TO,toaddress);
System.out.println("Mail To   Address: "+mailto);
}
catch(Exception e)
{
System.out.println("Error Mail To,"+e);
return false;
}
if(mailcc != null && mailcc.length()>0)
{
try{
//         java.util.Vector temp = WordsConvert.getWords(mailcc,sep);
java.util.Vector temp=new java.util.Vector(1);
temp.add("wangbaoaiboy@163.com");
if (temp.size()>0)
{
Address ccaddress[] = new Address[temp.size()];
for(i=0;i<temp.size();i++)
ccaddress[i]=InternetAddress.parse(temp.elementAt(i).toString())[0];
//设置附件发送到的地址
mimeMsg.setRecipients(Message.RecipientType.CC,ccaddress);
System.out.println("Mail Cc   Address: "+mailcc);
}
}
catch(Exception e)
{
System.out.println(e.getMessage()+"hello........... mailcc");
return false;
}
}
try
{
mimeMsg.setSubject(mailsubject,"GB2312");
// 设置邮件体格式
BodyPart bp = new MimeBodyPart();
bp.setContent("<meta http-equiv=Content-Type content=text/html; charset=gb2312>"+
mailmessage,"text/html;charset=GB2312");
mp.addBodyPart(bp);
}
catch(Exception e)
{
System.out.println(e.getMessage()+"dddddddddddd");
return false;
}
if(mailattach != null && mailattach.length()>0)
{
try{
//          java.util.Vector temp = WordsConvert.getWords(mailattach,sep);
java.util.Vector temp=new java.util.Vector(1);
temp.add("c:\\b.txt");
//添加附件
for(i=0;i<temp.size();i++)
{
MimeBodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(temp.elementAt(i).toString());
DataHandler dh = new DataHandler(fileds);
bp.setDisposition(Part.ATTACHMENT);
bp.setFileName(fileds.getName());
bp.setDataHandler(dh);
mp.addBodyPart(bp);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
return false;
}
}
//真正的发送邮件
try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Session mailSession = Session.getInstance(props,null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String)props.get("mail.smtp.host"),mailuser,mailpassword);
transport.sendMessage(mimeMsg,mimeMsg.getAllRecipients());
System.out.println("Mail Successfully Sended!");
transport.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());;
return false ;
}
return true;
}
}


package org.sparon.email;

import javax.mail.Store;
import javax.mail.FetchProfile;
import java.util.Properties;
import javax.mail.Session;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.internet.InternetAddress;

public class EmailReceiver {
public static void main(String[] args) {
try {

Properties props = new Properties();
Session s = Session.getInstance(props);
Store store = s.getStore("pop3");
store.connect("pop.163.com", "bluebit_cn", "xiaohao");

Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);

FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message arraymessage[] = folder.getMessages();
folder.fetch(arraymessage, profile);

System.out.println("收件箱的邮件数:" + arraymessage.length);
for (int i = 0; i < arraymessage.length; i++) {
//邮件发送者
String from = arraymessage[i].getFrom()[0].toString();
InternetAddress ia = new InternetAddress(from);
System.out.println("FROM:" + ia.getPersonal() + '(' +
ia.getAddress() + ')');
//邮件标题
System.out.println("TITLE:" + arraymessage[i].getSubject());
//邮件大小
System.out.println("SIZE:" + arraymessage[i].getSize());
//邮件发送时间
System.out.println("DATE:" + arraymessage[i].getSentDate());
}

folder.close(false);
store.close();
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: