Java Send mail

Source: Internet
Author: User

The sending process of a message

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/93/wKioL1d2f-ailJN6AAAiVfUD4NE309.png "title=" Send a message flowchart. PNG "alt=" Wkiol1d2f-ailjn6aaaivfud4ne309.png "/>

① Sender Edit Message

② mail to the sending account all SMTP servers

③ if the sending account and the receiving account are not on the same SMTP server, the server to which the sending account belongs is responsible for sending the message to the SMTP server to which the recipient belongs

④ the recipient's server passes the message to the recipient's client when the recipient views the message


What you need to do to use Java to develop your program: Create and edit messages, send messages to the sender's server


The basic content of two mails

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/83/96/wKiom1d2hDWzZcT4AABfp9qJxkw968.png "title=" The contents of the message "width=" "height=" 286 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:600px;height:286px; "alt=" Wkiom1d2hdwzzct4aabfp9qjxkw968.png "/>

as shown, the main components of the message are

Sender, Recipient, CC, BCC, subject, attachment, body

The text is edited with a rich text editor, which can be unformatted text or HTML (with inline pictures)


Three based on Apache Commons email Send mail

1 Introduction of development kits

Apache Commons Email is a wrapper for javamail, so it is necessary to introduce the jar package required by JavaMail and introduce its own jar package.

Activation.jar Bag: http://www.oracle.com/technetwork/java/javase/downloads/index-135046.html

Jdk6 and above have included the jar without introducing

Mail.jar Bag: Https://java.net/projects/javamail/downloads

Java EE contains the jar package without introducing

Apache Commons email:http://commons.apache.org/proper/commons-email/download_email.cgi

2 Introduction to common APIs

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/83/96/wKioL1d3UqiTjqxnAAAx0i7_oyE185.png "title=" Apacheemail class diagram. png "alt=" Wkiol1d3uqitjqxnaaax0i7_oye185.png "/>


The above class is an implementation of different representations of the message for the body and the attachment, and specific classes add specific functionality.

The e-mail class is an implementation of other content, in addition to the text and attachments,

The Simpleemail class adds unformatted text,

Multipartemail Add an attachment, emailattachment is the encapsulation of the attachment,

Htmlemail add display HTML formatting features

Imagehtmlemail implement inline picture, Htmlemail can also realize inline picture display, but need CID, and Imagehtmlemail no CID


Three sample code

Here is a sample code for developing common

Public void sendemail ()  throws emailexception{    //Create a message that displays body content in HTML format     htmlemail email = new htmlemail ();     //Setting Environment parameters     email.sethostname ("smtp.sina.com");     email.setcharset ("UTF-8");     //set the user name password for the connection server     email.setauthentication ("account",  " Password ")     //set the sender and recipient of the message     email.setfrom (" Email account ");     //can also call Email.setto (acollection) to set multiple receivers,     //or multiple calls to Email.addto     email.addto ("Recepient");     //settings Theme      Email.setsubject ("Apache email send Mail")     //Add Attachment     email.attach ( New file ("g:\\workspace\\javamail\\bin\\mail.properties"));     email.attach (new  File ("g:\\workspace\\javamail\\bin\\notice.txt "));     //Add html    string cid =  Email.embed (New file ("c:\\documents and settings\\administrator\\ Desktop \\ApacheEmail class diagram. png"));     String aHtml =  "


650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/83/98/wKiom1d3Y2jzrYSbAABKQsnF6LQ752.png "title=" sends the message. PNG "alt=" Wkiom1d3y2jzrysbaabkqsnf6lq752.png "/>

Four common mailbox Send message description

Later continue to explain

This article is from the "Philipzone" blog, make sure to keep this source http://philipzone.blog.51cto.com/8856558/1795142

Java Send mail

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.