The JavaMail API defines a java.mail.Transport class, which is specifically used to perform mail sending tasks. The instance object of this class encapsulates the underlying implementation details of a certain mail sending protocol. The application calls the methods in this class to send the encapsulated mail data in the Message object to the specified SMTP server. The working relationship between the main APIs designed to send mail using JavaMail is as follows:
1. Obtain a Transport object that implements a certain email sending protocol from the Session object;
2. Use the Session object to create a Message object and call the Message object method to encapsulate the email data;
3. Connect to the specified SMTP server and call the mail sending method in the Transport object to encapsulate the mail data in the Message object.
In the second article about creating emails in JavaMail, we learned to create emails, but at that time we wrote the emails to our local disk. Next, we used the Transport class provided by the JavaMail API to send emails.
import java.util.Date;import java.util.Properties;import javax.mail.Message;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class SendTextMail { public static void main(String[] args) throws Exception { String from = "[email protected]"; String to = "[email protected]"; String subject = "test"; String body = "test!!!"; String smtpHost = "smtp.qq.com"; Properties props = new Properties(); props.setProperty("mail.transport.protocol", "smtp"); // Protocol used (required by JavaMail specification) props.setProperty("mail.smtp.host", smtpHost); // The SMTP server address of the sender's mailbox is props.setProperty("mail.smtp.auth", "true"); // Request authentication, the parameter name is related to the specific implementation // Create Session instance object Session session = Session.getDefaultInstance(props); // Create MimeMessage instance object MimeMessage message = new MimeMessage(session); // Set sender message.setFrom(new InternetAddress(from)); // Set recipient message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); // Set send date message.setSentDate(new Date()); // Set email subject message.setSubject(subject); // Set the email body message.setText(body); // Set plain text content message.setText(body); // Save and generate the final email content message.saveChanges(); // Set to debug mode, you can view the detailed sending log session.setDebug(true); // Get the Transport object Transport transport = session.getTransport("smtp"); // The second parameter needs to be filled in the SMTP authorization code of the QQ mailbox. What is the authorization code, and how is it set? transport.connect(from, "******************"); // Send, message.getAllRecipients() Gets all recipients added when creating the mail object, cc, and send Message(message, message.getAllRecipients()); transport.close(); }}Note: The SMTP service must be enabled for the email account.
Sending complex mail codes with embedded resources or attachments is similar to those in the second article in JavaMail to create emails, except that the steps to write to the hard disk are replaced with lines 42-47 in the above code.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.