www.pudn.com > srmail.rar > sendway.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class sendway {
private String mailFrom;
private String mailTo;
private String mailTitle;
private String mailBoby1;
private String mailBoby2;
private String smtpServer;
private String pwd;
public sendway(String mailFrom,String mailTo,String mailTitle,String mailBoby1,String mailBoby2,String smtpServer,String pwd){
this.mailTo=mailTo;
this.mailTitle=mailTitle;
this.mailBoby1=mailBoby1;
this.mailBoby2=mailBoby2;
this.mailFrom=mailFrom;
this.smtpServer=smtpServer;
this.pwd=pwd;
}
public String getMailTo(){
return mailTo;
}
public String getMailFrom(){
return mailFrom;
}
public String getMailTitle(){
return mailTitle;
}
public String getMailBoby1(){
return mailBoby1;
}
public String getMailBoby2(){
return mailBoby2;
}
public String getSmtpServer(){
return smtpServer;
}
public String getPwd(){
return pwd;
}
public void setMailTo(String mailTo){
this.mailTo=mailTo;
}
public void setMailFrom(String mailFrom){
this.mailFrom=mailFrom;
}
public void setMailTitle(String mailTitle){
this.mailTitle=mailTitle;
}
public void setSmtpServer(String smtpServer){
this.smtpServer=smtpServer;
}
public void setMailBoby1(String mailBoby1){
this.mailBoby1=mailBoby1;
}
public void setMailBoby2(String mailBoby2){
this.mailBoby2=mailBoby2;
}
public void setpwd(String pwd){
this.pwd=pwd;
}
public sendway(){
}
//
public void sendway(String from,String to,String title,String boby1,String boby2,String smtpServer,String pwd){
boolean debug = true;
Properties props = new Properties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
try {
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};//new a internet address
msg.setRecipients(Message.RecipientType.TO, address);//get internet address group
msg.setSubject(title);//set mail title
msg.setSentDate(new Date()); //set mail date
MimeBodyPart mbp1 = new MimeBodyPart();//new a mail boby
if(boby1!=null)
{
mbp1.setText(boby1);
}
MimeBodyPart mbp2 = new MimeBodyPart();
if(boby2!=null){
DataSource source = new FileDataSource(boby2);
mbp2.setDataHandler(new DataHandler(source));
mbp2.setFileName(boby2);
}
Multipart mp = new MimeMultipart();
if(boby1!=null)
{
mp.addBodyPart(mbp1);
}
if(boby2!=null)
{
mp.addBodyPart(mbp2);
}
msg.setContent(mp);
if(boby2!=null){
File f=new File(boby2);
long length = f.length(); //文件大小
}
Transport transport = session.getTransport("smtp");
transport.connect(smtpServer,from,pwd); //connect smtp server
transport.sendMessage(msg, msg.getAllRecipients()); //send mail
transport.close();
} catch (MessagingException mex) {
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) {
ex.printStackTrace();
}
}
}
public static String readTxt(String filePathAndName,String encoding) throws IOException{
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
String st = "";
try{
FileInputStream fs = new FileInputStream(filePathAndName);
InputStreamReader isr;
if(encoding.equals("")){
isr = new InputStreamReader(fs);
}else{
isr = new InputStreamReader(fs,encoding);
}
BufferedReader br = new BufferedReader(isr);
try{
String data = "";
while((data = br.readLine())!=null){
str.append(data+" ");
}
}catch(Exception e){
str.append(e.toString());
}
st = str.toString();
}catch(IOException es){
st = "";
}
return st;
}
public static void createFile(String filePathAndName, String fileContent) {
try {
String filePath = filePathAndName;
filePath = filePath.toString();
File myFilePath = new File(filePath);
if (!myFilePath.exists()) {
myFilePath.createNewFile();
}
FileWriter resultFile = new FileWriter("E:/JavaApp/srmail/sendlist.txt");
PrintWriter myFile = new PrintWriter(resultFile);
String strContent = fileContent;
myFile.println(strContent);
myFile.close();
resultFile.close();
}
catch (Exception e) {
// message = "创建文件操作出错";
}
}
}