JavaMail entry 3: send an email, and javamail entry 3

Source: Internet
Author: User
Tags email account

JavaMail entry 3: send an email, and javamail entry 3

The JavaMail API defines a java. mail. transport class, which is used to execute mail sending tasks. The instance objects of this class encapsulate the underlying implementation details of a mail sending protocol, by calling methods in this class, the application can send the mail data encapsulated in the Message object to the specified SMTP server. The working relationship between the main APIs designed for sending emails using JavaMail is as follows:

 

1. Obtain the Transport object that implements a mail sending protocol from the Session object;

2. Create a Message object using the Session object and call the Message object method to encapsulate the mail data;

3. Connect to the specified SMTP server and call the mail data encapsulated in the mail sending method Message object in the Transport object.

In the second article "create email" in JavaMail, we learned how to create emails. However, at that time, we wrote emails to our local disk, next we will use the Transport class provided by the JavaMail API to send emails.

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 = "123456@qq.com"; 13 String to = "123456@163.com"; 14 String Subject = "test"; 15 String body = "test !!! "; 16 String smtpHost =" smtp.qq.com "; 17 18 Properties props = new Properties (); 19 props. setProperty ("mail. transport. protocol "," smtp "); // The protocol used (JavaMail specification requirements) 20 props. setProperty ("mail. smtp. host ", smtpHost); // SMTP server address of the sender's mailbox 21 props. setProperty ("mail. smtp. auth "," true "); // request authentication. The parameter name is related to the specific implementation. 22 23 // create a Session Instance Object 24 Session session = Session. getDefaultInstance (props); 25 // create a MimeMessage Instance Object 26 Mim EMessage message = new MimeMessage (session); 27 // set the sender's 28 message. setFrom (new InternetAddress (from); 29 // set the recipient's 30 message. setRecipients (Message. recipientType. TO, InternetAddress. parse (to); 31 // set the sending date to 32 message. setSentDate (new Date (); 33 // set the mail subject 34 message. setSubject (subject); 35 // set the mail body 36 message of the plain text content. setText (body); 37 // save and generate the final message content 38 message. saveChanges (); 39 // set it to debug mode. You can view the details of sending log40 Session. setDebug (true); 41 // get the Transport object 42 Transport transport = session. getTransport ("smtp"); 43 // The authorization code of SMTP in QQ mail must be filled in with the 2nd parameters. What is the authorization code and how can it be set? 44 transport. connect (from, "*****************"); 45 // send, message. getAllRecipients () obtains all recipients, CC recipients, and BCC 46 transport added when the email object is created. sendMessage (message, message. getAllRecipients (); 47 transport. close (); 48} 49}

Note: The SMTP service must be enabled for the email account.

The code for sending complex emails with embedded resources or attachments is similar to the Code for creating emails in the second article of JavaMail entry. You just need to replace the steps for writing to the hard disk with lines 42-47 in the code above.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.