JavaMail Introductory Tutorials Email (3) _java

Source: Internet
Author: User
Tags email account

JavaMail The API defines a Java.mail.Transport class that is designed to perform mail-sending tasks, which encapsulate the underlying implementation details of a mail delivery protocol, and the application calls methods in this class to send mail data encapsulated in the message object to the specified SMTP server. The working relationship between the primary APIs designed to use JavaMail when sending mail is as follows:

1, from the session object to achieve a certain kind of mail delivery protocol transport object;

2, using the Session object to create a message object, and call the Messages object method to encapsulate the mail data;

3. Connect to the specified SMTP server and invoke the message data encapsulated in the message in the transport object.

In the second article on JavaMail, we learned to create messages, but at the time we were writing mail to our local disk, and then we used the transport class provided by the JavaMail API to send mail.

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 = ' 123456@qq.com ';
    String to = "123456@163.com";
    String subject = "Test";
    String BODY = "Test!!!";

    String smtphost = "smtp.qq.com";
    Properties Props = new properties (); Props.setproperty ("Mail.transport.protocol", "SMTP"); Use of the Protocol (JavaMail specification requirements) Props.setproperty ("Mail.smtp.host", SMTPHost); SMTP Server address Props.setproperty ("Mail.smtp.auth", "true") of the sender's mailbox;
    Request authentication, parameter name and specific implementation related//Create session Instance Object session = Session.getdefaultinstance (props);
    Creates a MimeMessage instance object mimemessage message = new MimeMessage (session);
    Sets the sender Message.setfrom (new internetaddress (from)); Set Recipient message.setrecipients (MESSAGE.recipienttype.to, Internetaddress.parse (to));
    Set the Send Date message.setsentdate (new date ());
    Set the message subject Message.setsubject (subject);
    Set the message body for plain text content Message.settext.
    Save and generate the final message content message.savechanges ();
    Set to debug mode, you can view the detailed send log session.setdebug (true);
    Gets the transport object transport transport = Session.gettransport ("SMTP");
    The 2nd parameter needs to fill in the QQ Mailbox SMTP authorization code, what is the authorization code, it is how to set up?
    Transport.connect (From, "****************"); Send, message.getallrecipients () gets all the recipients that were added when the mail object was created, CC, Transport.sendmessage.
    Message.getallrecipients ());
  Transport.close ();
 }
}

Note: The email account must be open to the SMTP service.

Send a complex message code with an embedded resource or an attachment with JavaMail the second article is similar to creating a message, except that the steps to write to the hard disk are replaced by the 42-47 lines in the above code.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.