Reprinted from: http: //www.cnblogs.com/huangminwen/p/6087262.html
Getting third in JavaMail to send mail
JavaMail API defines a class java.mail.Transport that dedicated to performing mail transmission job, the object instance of this class encapsulates the underlying transmission protocol messages some implementation details, this class method calls the application can put send message object encapsulated mail data to a specified SMTP server. Working relationship between the main API design are shown below. JavaMail sending:
1, obtained from the Session object achieved a mail transmission protocol Transport object;
2, to create objects using Message Session object and calls methods Message object encapsulating data message;
3, specified SMTP server connection, call send e-mail messages in the Transport object data encapsulation method of Message object.
In the entry to create a mail JavaMail The second article, we learned how to create e-mail, but when we are to write a message to our local disk, Transport category Next we use JavaMail API provided to send mail
1 import java.util.Date; 2 import java.util.Properties; 3 4 import javax.mail.Message; 5 import javax.mail.Session; 6 import javax.mail.Transport; 7 import javax.mail.internet.InternetAddress; 8 import javax.mail.internet.MimeMessage; 9 10 public class SendTextMail { 11 public static void main(String[] args) throws Exception { 12 String from = "[email protected]"; 13 is String to = "[email protected]" ; 14 String = Subject "Test" ; 15 String body = "Test !!!" ; 16 String smtpHost = "smtp.qq.com" ; . 17 18 is the Properties The props = new new the Properties (); . 19 props.setProperty ( "mail.transport.protocol", "SMTP"); // protocol (the JavaMail specifications) used 20 is props.setProperty ( "the mail.smtp.host", smtpHost); // send pieces of people's mailboxes SMTP server address 21 props.setProperty ( "mail.smtp.auth", "to true");// request certification, implementation-specific parameter names 22 23 // Create Session instance objects 24 Session the session = Session.getDefaultInstance (The props); 25 // create an object instance MimeMessage 26 is MimeMessage Message = new new MimeMessage (the session); 27 // Set sender 28 message.setFrom ( new new (from the InternetAddress )); 29 // disposed recipient 30 message.setRecipients (Message.RecipientType.TO, InternetAddress.parse (to)); 31 is // set the transmission date 32 message.setSentDate ( new new a date ()); 33 is // set message Subject 34 message.setSubject (Subject); 35 // set the plain text content of the message body 36 message.setText (body); 37 [ // Save and generate a final message content 38 is message.saveChanges (); 39 // set debug mode, transmission can view the detailed log 40 session.setDebug ( to true ); 41 is // Get Transport object 42 is Transport Transport Session.getTransport = ( "SMTP" ); 43 is // second parameter is required to complete the SMTP mail authorization QQ code, what is the authorization code, how it is set up? 44 is transport.connect (from, "*********************************************************" ); 45 //Transmitting, message.getAllRecipients () to obtain all the recipients are added when creating the message object, Cc, Bcc 46 is transport.sendMessage (Message, message.getAllRecipients ()); 47 transport.close () ; 48 } 49 }
Note: The email account must be open SMTP service.
Complex code to send messages with inline resources or with attachments and entry JavaMail The second message is created in an article similar, but the steps will be written to the hard disk replacement 42-47 lines of code to the above, since there send messages, Of course there receive mail, the next JavaMail fourth in receiving mail entry describes how to receive messages.