Nothing is fun, I recently saw a lot of places with emails, just watching Java, I wrote with Java, I need to download mail.jar and activation.jar, in addition, I use the previous written operation XML file A class (on the article), well, not much to say, code stickers, how to use yourself:
//////1.properties we need class //1.properties //// javamail Requires Properties to create a session object, its property value is the host that sends an email, such as ////properties PROPS = new property (); //props.put ("mail.smtp.host "," smtp.xxxx.com "); // can be replaced with your SMTP hostname, just like you set the SMTP host name in Outlook. //props.put ("mail.smtp.auth "," true "); // By verifying ////2.session //// All JavaMail-based programs require at least one or all of the dialogue targets. //// session session = session.getInstance (PrOPS, NULL); ////3.mimeMessage //// The information object will reflect the message you sent true. //// mimeMessage msg = new mimeMessage (session); ////4.Transport //// The send 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.documentexception;
/ * * Created on 2005-4-14 21:27:16 * * /
/ ** * @Author zxub * / public class mailsender {
String username; string.com; string. "String." STRING "STRING" STRING
String filename;
// List of file names for saving sending attachments list ();
public MailSender (String to, String subject, String body) throws MalformedURLException, DocumentException {// initialize the sender, recipient, subject, etc. OperaXML OX = new OperaXML (); this.from = OX.getNodeValue ( "config.xml "," config / host1 / from "); this.host = ox.getnodevalue (" config.xml "," config / host1 / smtphost "); this.useval = ox.getnodevalue (" config.xml "," config / Host1 / username "); this.password = ox.getnodeValue (" config.xml "," config / host1 / password "); this.to = TO; this.Subject = Subject; this.body = body;}
// Used to collect attachment name public void attachfile (string filename) {this.arrfilename.add (filename);
// Start send public boolean startsend () {
// Create a Properties object Properties PROPERTIES PROPS = system.getProperties (); // Create a Letter Server Props.Put ("Mail.Smtp.host", this.host); Props.Put ("mail.smtp.auth", "TRUE "); // Get the default dialogue object session session = session.getDefaultInstance (Props, NULL); Try {// Create a message, and initialize the event of the message MIMEMESSAGE MSG = New MimeMessage (SESSION) Msg.SetFrom (new internetdress (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 mp.addbodypart (MBP); // Remove all elements in the collection ArrFileName.RemoveAll (); // Multipart Add to Letter msg.setContent (MP);} else {// Setup mail body Msg.Settext (this.body );} // Set the send date of the letters head msg.setSentDate (new Date ()); msg.saveChanges (); // Send Letters Transport Transport = session.GetTransport ("SMTP"); Transport.Connect (this.host , this.userName, this.password); transport.sendMessage (msg, msg .getRecipients (Message.RecipientType.TO)); 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 ("Mailbox Address 1, Mailbox Address 2", "Mail Subject", "Mail Content"); Sendmail.attachfile ("E: //Eclipse//Workspace/Mailsender//mailsender.rar) "); // Accessories address //sendmail.attachfile (" Attachment 2 address "); ........................................StartSend (); System.out. Println ("OK");} catch (exception e) {E.PrintStackTrace ();} }