求jsp最简单的发email方法

stonemusic 2002-10-29 12:57:17
前提是我不可以动服务器,因为空间是租的。
...全文
33 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
superbamboo 2002-11-04
  • 打赏
  • 举报
回复
//Title: send mail
//Version: 1.0
//Copyright: Copyright (c) 2002
//Author: superbamboo
//Company: CS&S
//Description: 支持群发和附件

import java.util.*;
import javax.activation.*;//这些包可以上网下载
import javax.mail.*;
import javax.mail.internet.*;
/**
* @author superbamboo
*如果发送成功返回“1”
* 发送失败返回“0”
*
*/
public class SendMail {

//设定邮件服务器
String host ="263.net";
/**转发邮件的服务器,可以用smtp.163.com
*sina.com
*eyou.com 等服务器试一试,还可以将2000的smtp服务打开,用
×自己的机器做转发服务器
*/

//设定发送人邮件地址,有默认值
String from ="pasManager@cosix.com.cn";

//设定收件人地址,暂时设定最多为20个人;
String[] to=new String[20];

//设定邮件标题
String title;

//设定邮件正文内容
String text;

//邮件附件文件名,暂时设定附件最多为10个
String[] files=new String[10];//10 file max


//实际收件人数目
int toNumber=0;

/**
* Constructor for SendMail.
*/
public SendMail() {

}

//set host
public void setHost(String host){
this.host=host;
}

//set form address
public void setForm(String form){
this.from=form;
}

//add to address
public void addTo(String receive){
to[toNumber]=receive;
toNumber=toNumber+1;
}

//set title
public void setTitle(String title){
this.title=title;
}

//set text
public void setText(String text){
this.text=text;
}

//add file
public void addFile(String file){
for(int i=0;i<10;i++){
if (files[i]==null){
files[i]=file;
break;
}
}
}


//发送邮件
public int send(){
try{


for(int j=0;j<toNumber;j++){
// Get system properties
Properties props = System.getProperties();

// Setup mail server
props.put("mail.smtp.host", host);

// Get session
Session session = Session.getDefaultInstance(props, null);

// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));


message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to[j]));
message.setSubject(title);


// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Fill the message
messageBodyPart.setText(text);

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Part two is attachment
for (int i=0;i<files.length;i++){
if (files[i]==null){
break;
}
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(files[i]);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(files[i].substring(files[i].lastIndexOf("\\")+1));
multipart.addBodyPart(messageBodyPart);
}
// Put parts in message
message.setContent(multipart);


// Send message
Transport.send(message);

messageBodyPart=null;
multipart=null;
props=null;
session=null;
message=null;

} //end for

//return
return 1;

}catch (Exception e){
System.out.println(e);
return 0;
}


}

//调用实例:
public static void main(String[] args) {

SendMail send=new SendMail();
send.setForm("hehe@163.com");

send.addTo("xixi@163.com");
send.addTo("super@cosix.com.cn");
send.addTo("zhenhua@cosix.com.cn");

send.setTitle("test");
send.setText("asdlasdfkgjasdfkgjasd");

send.addFile("d:\\a.zip");
send.addFile("d:\\b.zip");

send.send();
}
}



兄弟,多给些分吧!

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧