import javaio*;
import javautil*;
import javaxmail*;
import javaxmailinternet*;
import javaxactivation*;
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 fromString toString usernameString password){
thisfrom=from;
thisto=to;
thisusername=username;
thispassword=password;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
thisfrom = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
thisto = to;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
thisusername = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
thispassword = password;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
thissubject = subject;
}
public String getText() {
return text;
}
public void setText(String text) {
thistext = text;
}
public File getFile() {
return file;
}
public void setFile(File file) {
thisfile = file;
}
public boolean send(){
Properties props=new Properties();
propsput(mailsmtphost );
//propsput(mailsmtphost localhost);
propsput(mailsmtpauth true);
Session mailSession=SessiongetDefaultInstance(propsnull);
try{
Transport trans=mailSessiongetTransport(smtp);
nnect(usernamepassword);
//nnect();
Message newMessage=new MimeMessage(mailSession);
newMessagesetSubject(subject);
newMessagesetFrom(new InternetAddress(from));
/*
* 上传一个文件
* */
BodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(file);
fileBodyPartsetDataHandler(new DataHandler(fds));
fileBodyPartsetFileName(awav);
Address addressTo[] = { new InternetAddress()};
newMessagesetRecipients(MessageRecipientTypeTO addressTo);
newMessagesetText(text);
/*
* 将文件保存到Message中
* */
MimeMultipart multi = new MimeMultipart();
multiaddBodyPart(fileBodyPart);
newMessagesetContent(multi);
newMessagesaveChanges();
transsendMessage(newMessage newMessagegetRecipients(MessageRecipientTypeTO)); //发送邮件
transclose();
return true;
}catch(Exception ex){
return false;
}
}
public static void main(String[] args){
SendMail sendmail=new SendMail( zxctch );
//SendMail sendmail=new SendMail();
sendmailsetSubject(测试);
sendmailsetText(测试代码);
File file=new File(E:\\workspace\\applet\\bin\\awav);
sendmailsetFile(file);
if(sendmailsend()){
Systemoutprintln(send success);
}
else
Systemoutprintln(send failed);
}