package com.smartteam.common.email;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/ ** * Use the SMTP protocol to send e-mail * / public class SendMailTest { // e-mail transmission protocol Private Final static String PROTOCOL = "the SMTP" ; // SMTP mail server Private Final static String HOST = "xxx.xxx.xx" ; // the SMTP mail server (written when ip ip) default port Private Final static String pORT = "xx.xxx.xxx.xx" ; // whether to require authentication Private Final static String IS_AUTH = "to true" ; // whether to enable (a question-and-answer response messages enable debug mode when printing client and server interaction) debug mode Private Final staticIS_ENABLED_DEBUG_MOD = String "to true" ; // sender Private static String from = "xxx.xx" ; // recipient Private static String to = "xxx.xxx" ; // initialize the connection to the mail server session information Private static The props = the Properties null ; static { The props = new new the Properties (); props.setProperty ( "mail.transport.protocol" , the PROTOCOL); props.setProperty ( "the mail.smtp.host" , the HOST); props.setProperty ( "mail .smtp.port " ,PORT); props.setProperty ( "mail.smtp.auth" , IS_AUTH); props.setProperty ( "mail.debug" , IS_ENABLED_DEBUG_MOD); } public static void main (String [] args) throws Exception { // sending simple text messages sendTextEmail (); } / ** * sending simple text messages * / public static void sendTextEmail () throws Exception { // create an object instance Session Session the session = Session.getDefaultInstance (the props); // create an object instance MimeMessage The Message = the MimeMessage new new the MimeMessage (the session); // Set the sender message.setFrom ( new new InternetAddress (from)); // set the message subject message.setSubject ( "Use javamail send simple text messages" ); // set Write people message.setRecipient (RecipientType.TO, new new InternetAddress (to)); // set the transmission time message.setSentDate ( new new a Date ()); // set plain text as the message body message.setText ( "use smtp agreement to send text Mail test !!! " ); // save and generate the final message content message.saveChanges (); //Examples of objects obtained Transport Transport transport = Session.getTransport (); // open a connection (send messages side user name and password) transport.connect ( "xxx.xx", "XXX" ); // the message object to the transport objects , message sent transport.sendMessage (message, message.getAllRecipients ()); // close the connection transport.close (); } }