读书人

求完整可用的JAVAMAIL程序,可发送带图

发布时间: 2012-01-16 23:36:52 作者: rapoo

求完整可用的JAVAMAIL程序,可发送带图片HTML邮件,带附件邮件
我这边有很多是用不起来的.求可用的好的

[解决办法]
一搜一大片
[解决办法]
我这儿有一个完整的程序
不过我要先拿到分哦
[解决办法]
package com.geckosys.struts.javabean;

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

/**
* <p> Title: 使用javamail发送邮件 </p>
* <p> Description: 演示如何使用javamail包发送电子邮件。这个实例可发送多附件 </p>
* <p> Copyright: Copyright (c) 2003 </p>
* <p> Filename: Mail.java </p>
* @version 1.0
*/
public class SendHtmlMail {

String to = " ";//收件人
String from = " ";//发件人
String host = " ";//smtp主机
String username = " " ;
String password = " " ;
String filename = " ";//附件文件名
String subject = " ";//邮件主题
String content = " ";//邮件正文
Vector file = new Vector();//附件文件集合
/**
* <br> 方法说明:默认构造器
* <br> 输入参数:
* <br> 返回类型:
*/
public SendHtmlMail(){
}
/**
* <br> 方法说明:构造器,提供直接的参数传入
* <br> 输入参数:
* <br> 返回类型:
*/
public SendHtmlMail(String to,String from,String smtpServer,String username,String password,String subject,String content){
this.to = to;
this.from = from;
this.host = smtpServer;
this.username = username;
this.password = password;
this.subject = subject;
this.content = content;
}
/**
* <br> 方法说明:设置邮件服务器地址
* <br> 输入参数:String host 邮件服务器地址名称
* <br> 返回类型:
*/
public void setHost(String host){
this.host = host;
}
/**
* <br> 方法说明:设置登录服务器校验密码
* <br> 输入参数:
* <br> 返回类型:
*/
public void setPassWord(String pwd){
this.password = pwd;
}
/**
* <br> 方法说明:设置登录服务器校验用户
* <br> 输入参数:
* <br> 返回类型:
*/
public void setUserName(String usn){
this.username = usn;
}
/**
* <br> 方法说明:设置邮件发送目的邮箱
* <br> 输入参数:
* <br> 返回类型:
*/
public void setTo(String to){
this.to = to;
}
/**
* <br> 方法说明:设置邮件发送源邮箱
* <br> 输入参数:
* <br> 返回类型:
*/
public void setFrom(String from){
this.from = from;
}
/**
* <br> 方法说明:设置邮件主题
* <br> 输入参数:
* <br> 返回类型:
*/
public void setSubject(String subject){
this.subject = subject;
}
/**
* <br> 方法说明:设置邮件内容
* <br> 输入参数:
* <br> 返回类型:
*/
public void setContent(String content){
this.content = content;
}
/**
* <br> 方法说明:把主题转换为中文
* <br> 输入参数:String strText
* <br> 返回类型:
*/
public String transferChinese(String strText){
try{
strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312 "), "GB2312 ", "B ");
}catch(Exception e){
e.printStackTrace();
}
return strText;
}
/**
* <br> 方法说明:往附件组合中添加附件
* <br> 输入参数:
* <br> 返回类型:
*/
public void attachfile(String fname){
file.addElement(fname);
}
/**
* <br> 方法说明:发送邮件
* <br> 输入参数:
* <br> 返回类型:boolean 成功为true,反之为false
*/
public boolean sendMail(){



//构造mail session
Properties props = System.getProperties();
props.put( "mail.smtp.host ",host);
props.put( "mail.smtp.auth ", "true ");
Session session=Session.getDefaultInstance(props, new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
});

try {
//构造MimeMessage 并设定基本的值
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address={new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO,address);
subject = transferChinese(subject);
msg.setSubject(subject);



//构造Multipart
Multipart mp = new MimeMultipart();

//向Multipart添加正文
MimeBodyPart mbpContent = new MimeBodyPart();
mbpContent.setContent(content, "text/html;charset=gb2312 ");
//********************************************
//背景图片id
mbpContent.setHeader( "Content-ID ", "page ");

//********************************************
//向MimeMessage添加(Multipart代表正文)
mp.addBodyPart(mbpContent);

//////////////////////////////////
MimeMultipart mm = new MimeMultipart();
// 这句很重要,千万不要忘了
mm.setSubType( "related ");

mm.addBodyPart(mbpContent);

//////////////////////////////////
int i = 0;
//向Multipart添加附件
Enumeration efile=file.elements();
while(efile.hasMoreElements()){

MimeBodyPart mbpFile = new MimeBodyPart();
filename=efile.nextElement().toString();
FileDataSource fds = new FileDataSource(filename);
mbpFile.setDataHandler(new DataHandler(fds));
mbpFile.setFileName(fds.getName());
mbpFile.setHeader( "Content-ID ", "bg "+i);
//向MimeMessage添加(Multipart代表附件)
mp.addBodyPart(mbpFile);
i++;
}

file.removeAllElements();
//向Multipart添加MimeMessage
msg.setContent(mp);
msg.setSentDate(new Date());

Transport transport = session.getTransport( "smtp ");
transport.connect(host, username, password);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();

//发送邮件
//Transport.send(msg);

} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex=mex.getNextException())!=null){
ex.printStackTrace();
}
return false;
}
return true;
}
}
[解决办法]
package org.bromon.mail;
import javax.mail.*;
import java.util.*;
import java.io.*;

/**
* @author Bromon
*/
public class Receiver
{
Folder inbox;
Store store;

//连接邮件服务器,获得所有邮件的列表
public Message[] getMail(String host,String name,String password) throws Exception
{
Properties prop=new Properties();
prop.put( "mail.pop3.host ",host);
Session session=Session.getDefaultInstance(prop);
store=session.getStore( "pop3 ");
store.connect(host,name,password);

inbox=store.getDefaultFolder().getFolder( "INBOX ");


inbox.open(Folder.READ_ONLY);

Message[] msg=inbox.getMessages();

FetchProfile profile=new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
inbox.fetch(msg,profile);

return(msg);
}

//处理任何一种邮件都需要的方法
private void handle(Message msg) throws Exception
{
System.out.println( "邮件主题: "+msg.getSubject());
System.out.println( "邮件作者: "+msg.getFrom()[0].toString());
System.out.println( "发送日期: "+msg.getSentDate());
}

//处理文本邮件
public void handleText(Message msg) throws Exception
{
this.handle(msg);
System.out.println( "邮件内容: "+msg.getContent());
}

//处理Multipart邮件,包括了保存附件的功能
public void handleMultipart(Message msg) throws Exception
{
String disposition;
BodyPart part;

Multipart mp=(Multipart)msg.getContent();
int mpCount=mp.getCount();//Miltipart的数量,用于除了多个part,比如多个附件
for(int m=0;m <mpCount;m++)
{
this.handle(msg);

part=mp.getBodyPart(m);
disposition=part.getDisposition();
if(disposition!=null && disposition.equals(Part.ATTACHMENT))//判断是否有附件
{
//this.saveAttach(part);//这个方法负责保存附件,注释掉是因为附件可能有病毒,请清理信箱之后再取掉注释
}else{
System.out.println(part.getContent());
}
}
}

private void saveAttach(BodyPart part) throws Exception
{
String temp=part.getFileName();//得到未经处理的附件名字
String s=temp.substring(11,temp.indexOf( "?= ")-1);//去到header和footer

//文件名一般都经过了base64编码,下面是解码
String fileName=this.base64Decoder(s);
System.out.println( "有附件: "+fileName);

InputStream in=part.getInputStream();
FileOutputStream writer=new FileOutputStream(new File(fileName));
byte[] content=new byte[255];
int read=0;
while((read=in.read(content))!=-1)
{
writer.write(content);
}
writer.close();
in.close();
}

//base64解码
private String base64Decoder(String s) throws Exception
{
sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
byte[] b=decoder.decodeBuffer(s);

return(new String(b));
}

//关闭连接
public void close() throws Exception
{
if(inbox!=null)
{
inbox.close(false);
}

if(store!=null)
{
store.close();
}
}

public static void main(String args[])
{
String host= "pop.163.com ";
String name= "bromon ";
String password= "My password ";

Receiver receiver=new Receiver();

try
{
Message[] msg=receiver.getMail(host,name,password);

for(int i=0;i <msg.length;i++)
{
if(msg[i].isMimeType( "text/* "))//判断邮件类型


{
receiver.handleText(msg[i]);
}else{
receiver.handleMultipart(msg[i]);
}
System.out.println( "**************************** ");
}
receiver.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
[解决办法]
自己写一个撒,又不难,先在网上找找

读书人网 >Java Web开发

热点推荐