//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 ="[email protected]";
//设定收件人地址,暂时设定最多为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("[email protected]");
send.addTo("[email protected]");
send.addTo("[email protected]");
send.addTo("[email protected]");
send.setTitle("test");
send.setText("asdlasdfkgjasdfkgjasd");
send.addFile("d:\\a.zip");
send.addFile("d:\\b.zip");
send.send();
}
}兄弟,多给些分吧!
//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 ="[email protected]";
//设定收件人地址,暂时设定最多为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("[email protected]");
send.addTo("[email protected]");
send.addTo("[email protected]");
send.addTo("[email protected]");
send.setTitle("test");
send.setText("asdlasdfkgjasdfkgjasd");
send.addFile("d:\\a.zip");
send.addFile("d:\\b.zip");
send.send();
}
}兄弟,多给些分吧!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货