import java
import java
import javax
import javax
import javax
public class SendMail {
private String from;
private String to;
private String username;
private String password;
private String subject;
private String text;
private File file;
public SendMail(){
}
public SendMail(String from
this
this
this
this
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this
}
public String getTo() {
return to;
}
public void setTo(String to) {
this
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this
}
public String getText() {
return text;
}
public void setText(String text) {
this
}
public File getFile() {
return file;
}
public void setFile(File file) {
this
}
public boolean send(){
Properties props=new Properties();
props
//props
props
Session mailSession=Session
try{
Transport trans=mailSession
nnect(
// nnect();
Message newMessage=new MimeMessage(mailSession);
newMessage
newMessage
/*
* 上傳一個文件
* */
BodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(file);
fileBodyPart
fileBodyPart
Address addressTo[] = { new InternetAddress(
newMessage
newMessage
/*
* 將文件保存到Message中
* */
MimeMultipart multi = new MimeMultipart();
multi
newMessage
newMessage
trans
trans
return true;
}catch(Exception ex){
return false;
}
}
public static void main(String[] args){
SendMail sendmail=new SendMail(
//SendMail sendmail=new SendMail();
sendmail
sendmail
File file=new File(
sendmail
if(sendmail
System
}
else
System
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26077.html