Published on blog.csdn.net/zxub
There is nothing fun. Recently, I have seen emails from many places, just reading Java and writing it in Java. I need to download mail. jar and activation. jar. In addition, I used a class (the previous article) that operated the XML file I wrote before. Well, I will not talk about it much. I will post the code and check myself for how to use it:
// Some classes we need in javamail
// 1. Properties
//
// Javamail needs properties to create a session object. Its attribute value is the host that sends the email, for example:
//
// Properties props = new properties ();
// Props. Put ("mail. SMTP. Host", "smtp.xxxx.com"); // you can change it to your SMTP host name, just like setting the SMTP host name in outlook.
// Props. Put ("mail. SMTP. Auth", "true"); // pass Verification
//
// 2. Session
//
// All javamail-based programs require at least one or all of the conversation targets.
//
// Session = session. getinstance (props, null );
//
// 3. mimemessage
//
// The information object will actually reflect the email you sent.
//
// Mimemessage MSG = new mimemessage (session );
//
// 4. Transport
//
// The mail is sent by transport:
//
// Transport. Send (MSG );
Import java. AWT. List;
Import java.net. malformedurlexception;
Import java. util. date;
Import java. util. properties;
Import javax. Activation. datahandler;
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;
Import org. dom4j. extends entexception;
/*
* Created on 21:27:16
*
*/
/**
* @ Author zxub
*
*/
Public class mailsender {
String host;
String username;
String password;
String from;
String;
String subject;
String body;
String filename;
// Used to save the list of file names for sending attachments
List arrfilename = new list ();
Public mailsender (string to, string subject, string body)
Throws malformedurlexception, disable entexception {
// Initialize the sender, recipient, and topic.
Operaxml ox = new operaxml ();
This. From = ox. getnodevalue ("config. xml", "config/host1/from ");
This. Host = ox. getnodevalue ("config. xml", "config/host1/smtphost ");
This. Username = ox. getnodevalue ("config. xml", "config/host1/username ");
This. Password = ox. getnodevalue ("config. xml", "config/host1/password ");
This. To =;
This. Subject = subject;
This. Body = body;
}
// Used to collect the attachment name
Public void attachfile (string filename ){
This. arrfilename. Add (filename );
}
// Start sending
Public Boolean startsend (){
// Create a properties object
Properties props = system. getproperties ();
// Create a mail server
Props. Put ("mail. SMTP. Host", this. HOST );
Props. Put ("mail. SMTP. Auth", "true"); // pass Verification
// Get the default dialog object
Session session = session. getdefaultinstance (props, null );
Try {
// Create a message and initialize its elements
Mimemessage MSG = new mimemessage (session );
MSG. setfrom (New internetaddress (this. From ));
If (this. To. Trim (). Length ()> 0 ){
String [] arr = this. To. Split (",");
// Int receivercount = 1;
Int receivercount = arr. length;
If (receivercount> 0 ){
Internetaddress [] address = new internetaddress [receivercount];
For (INT I = 0; I <receivercount; I ++ ){
Address [I] = new internetaddress (ARR [I]);
}
MSG. addrecipients (message. recipienttype. To, address );
} Else {
System. Out
. Println ("none faster er! The program will be exit! ");
System. Exit (0 );
}
} Else {
System. Out. println ("none faster er! The program will be exit! ");
System. Exit (0 );
}
MSG. setsubject (subject );
// The subsequent bodypart will be added to the multipart created here
Multipart MP = new mimemultipart ();
// Obtain the attachment
Int filecount = This. arrfilename. getitemcount ();
If (filecount> 0 ){
For (INT I = 0; I <filecount; I ++ ){
Mimebodypart MBP = new mimebodypart ();
// Select the name of the Attachment
Filename = arrfilename. getitem (I). tostring ();
// Obtain the data source
Filedatasource FDS = new filedatasource (filename );
// Obtain the attachment and add it to the bodypart
MBP. setdatahandler (New datahandler (FDS ));
// Get the same name as bodypart
MBP. setfilename (FDS. getname ());
MP. addbodypart (MBP );
}
Mimebodypart MBP = new mimebodypart ();
MBP. settext (this. Body );
MP. addbodypart (MBP );
// Remove all elements from the set
Arrfilename. removeall ();
// Add multipart to the mail
MSG. setcontent (MP );
} Else {
// Set the email body
MSG. settext (this. Body );
}
// Set the sending date of the mail header
MSG. setsentdate (new date ());
MSG. savechanges ();
// Send a letter
Transport transport = session. gettransport ("SMTP ");
Transport. Connect (this. Host, this. username, this. Password );
Transport. sendmessage (MSG, MSG
. Getrecipients (message. recipienttype. ));
Transport. Close ();
} Catch (messagingexception MEX ){
Mex. printstacktrace ();
Exception EX = NULL;
If (EX = Mex. getnextexception ())! = NULL ){
Ex. printstacktrace ();
}
Return false;
}
Return true;
}
Public static void main (string [] ARGs ){
Try {
Mailsender Sendmail = new mailsender ("email address 1, email address 2 ",
"Email Subject", "email content ");
Sendmail. attachfile ("E: \ eclipse \ workspace \ mailsender \ mailsender.rar"); // attachment address
// Sendmail. attachfile ("Attachment 2 address ");....................
Sendmail. startsend ();
System. Out. println ("OK ");
} Catch (exception e ){
E. printstacktrace ();
}
}
}