1, first import the jar package in the project, I use the Mail-1.4.jar
2. Here is my code:
public class Sendmailutil {
private static final Logger Logger = Logger.getlogger (Sendmailutil.class);
//define sender, recipient, subject, etc.
private String to = null;//recipient e-mail address
private String from = null;//Sender e-mail address
private String password = null;//Sender password
Private String host = null;//mail server
private String filename = null;
A collection of file path names for saving send attachments
private Vector<string> fileList = new vector<string> ();
/*
* Can transmit the sender of the parameters such as the construction
*/
Public Sendmailutil (string to, string from, string password,
String smtpserver) {
//Initialize sender, recipient, subject, etc.
this.to = to;
this.from = from;
this.password = password;
this.host = smtpserver;
}
/*
* This method is used to collect the attachment name
*/
public void Attachfile (String fname) {
filelist.addelement (fname);
}
/*
* How to start sending letters
*/
Public boolean startsend (String emailtitle, String emailcontent) {
if (Stringutils.isblank (emailcontent)) {
logger.error ("The message content cannot be empty!") ");
return false;
}
try {
if (Stringutils.isblank (emailcontent)) {
throw new NullPointerException ("The content sent cannot be empty!") ");
}
//Create Properties Object
Properties props = System.getproperties ();
//Create a mail server
props.put ("Mail.smtp.auth", "true");
props.put ("Mail.smtp.host", host);
props.put ("Mail.smtp.password", "0");
//Get the default Dialog object
Session session = Session.getinstance (props,
New Popupauthenticator (This.from, This.password));
//Create a message and initialize the elements of the message
mimemessage msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress (from));
internetaddress[] address = {new internetaddress (to)};
msg.setrecipients (Message.RecipientType.TO, address);
//GetBytes ("Iso-8859-1")
//String Title=new string (emailtitle.getbytes ("iso-8859-1"), "GBK");
Msg.setsubject (emailtitle);
//BodyPart will be added to the multipart created here
Multipart MP = new Mimemultipart ("subtype");
//Add HTML body
bodypart htmlBody = new MimeBodyPart ();
Mimemultipart htmlcontent = new Mimemultipart ("related");
bodypart msgcontent = new MimeBodyPart ();
Htmlcontent.addbodypart (msgcontent);
msgcontent.setcontent (emailcontent, "text/html;charset=utf-8");
htmlbody.setcontent (htmlcontent);
Mp.addbodypart (htmlBody);
//Use enumerators to easily iterate through the collection
Enumeration efile = Filelist.elements ();
//Check if there are more objects in the sequence
while (efile.hasmoreelements ()) {
MimeBodyPart MBP = new MimeBodyPart ();
//select each attachment name
filename = efile.nextelement (). toString ();
//Get Data source
Filedatasource FDS = new Filedatasource (filename);
//Get the attachment itself and enter BodyPart
Mbp.setdatahandler (New DataHandler (FDS));
//Get the same file name into BodyPart
Mbp.setfilename (Fds.getname ());
Mp.addbodypart (MBP);
}
//Remove all elements from the collection
filelist.removeallelements ();
//multipart added to the letter
Msg.setcontent (MP);
//Set the sending date of the letter header
msg.setsentdate (New Date ());
//Send a letter
transport.send (msg);
} catch (Exception e) {
e.printstacktrace ();
return false;
}
return true;
}
//Authentication class
class Popupauthenticator extends Authenticator {
private String username;
private String password;
public Popupauthenticator (string user, String pass) {
this.username = user;
This.password = pass;
}
protected Passwordauthentication getpasswordauthentication () {
return new passwordauthentication (username, password);
}
}
//test
public static void Main (string[] args) {
sendmailutil mail = new Sendmailutil ("[Email protected]",
"[email protected]", "123456", "smtp.163.com");
mail.attachfile ("C:\\users\\awen\\desktop\\a.txt");
mail.startsend ("Handover document", "received please reply");
System.out.println ("Mail send Complete ...");
}
3, test success, we can try.
Java Mail Send Demo