The code is as follows:
Package demo;
import java.util.Properties;
import Javax.activation.DataHandler;
import Javax.activation.DataSource;
import Javax.activation.FileDataSource;
import Javax.mail.BodyPart;
import Javax.mail.Message;
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;
import Sun.misc.BASE64Encoder;
public class MailSender ... {
public static void Main (string[] args) ... {
MailSender sender = new MailSender ();
Sender.sethost ("smtp.sina.com.cn");
sender.setfromaddr ("");
sender.settoaddr ("");
sender.setusername ("wwwwwwww");
Sender.setpassword ("ssssssss");
Sender.settitle ("a few ideas on improving efficiency");
Sender.setattachpath ("C:singleuser.sql");
sender.setattachname ("Singleuser.sql");
Try ... {
Sender.sendmail ();
} catch (Exception e) ... {
E.printstacktrace ();
}
}
public void SendMail () throws Exception ... {
Properties Props = new properties ();
props.put ("Mail.smtp.host", GetHost ());
props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (props);
Session.setdebug (TRUE);
MimeMessage message = new MimeMessage (session);
Message.setfrom (New InternetAddress (GETFROMADDR ()));
message.addrecipient (message.recipienttype.to,new internetaddress (Gettoaddr ()));
Message.setsubject (GetTitle ());
Multipart Multipart = new Mimemultipart ();
bodypart Contentpart = new MimeBodyPart ();
Contentpart.settext ("The details of the message is Here");
multipart. Addbodypart (Contentpart);
if (Getattachpath ()!= null && getattachname ()!= null) ... {
bodypart attachmentpart= New MimeBodyPart ();
DataSource Source = new Filedatasource (Getattachpath ());
Attachmentpart.setdatahandler (new DataHandler (source));
Base64encoder enc = new Base64encoder ();
attachmentpart.setfilename ("=? GBK? B? "+enc.encode (Getattachname (). GetBytes ()) +"? = ");
Multipart.addbodypart (Attachmentpart);
}
message.setcontent (multipart);
message.savechanges ();
Transport transport = Session.gettransport ("SMTP");
Transport.connect (Host, GetUserName (), GetPassword ());
transport.sendmessage (Message, message.getallrecipients ());
Transport.close ();
}
private String host = null;
private String fromaddr = null;
private String toaddr = null;
private String username = null;
private String password = null;
private String title = null;
private String attachpath = null;
private String attachname = null;
public String gethost () ... {
return host;
}
public void Sethost (String host) ... {
this.host = host;
}
public String getfromaddr () ... {
return fromaddr;
}
public void Setfromaddr (String fromaddr) ... {
this.fromaddr = fromaddr;
}
public String gettoaddr () ... {
return toaddr;
}
public void Settoaddr (String toaddr) ... {
this.toaddr = toaddr;
}
public String getusername () ... {
return username;
}
public void Setusername (String username) ... {
this.username = Username;
}
public String GetPassword () ... {
return password;
}
public void SetPassword (String password) ... {
This.password = password;
}
public String getTitle () ... {
return title;
}
public void Settitle (String title) ... {
this.title = title;
}
public String Getattachpath () ... {
return attachpath;
}
public void Setattachpath (String attachpath) ... {
This.attachpath = Attachpath;
}
public String getattachname () ... {
return attachname;
}
public void Setattachname (String attachname) ... {
this.attachname = attachname;
}
}