Application Server
(1) Javax.mail.Properties class
JavaMail requires properties to create a Session object. It will look for the string "Mail.smtp.host", the property value being the host that sent the message.
Usage:
Copy Code code as follows:
Properties Props = new properties ();
Props.put ("Mail.smtp.host", "smtp.163.com");//Can be replaced with your SMTP host name.
Copy Code code as follows:
Properties Props = new properties ();
Props.put ("Mail.smtp.host", "smtp.163.com");//Can be replaced with your SMTP host name.
(2) Javax.mail.Session class
This session class represents a message session in JavaMail. Each JavaMail application has at least one session but can have any number of sessions. In this example, the session object needs to know the SMTP server that is used to process the message.
Usage:
Copy Code code as follows:
Session sendmailsession;
Sendmailsession = session.getinstance (props, null);
Copy Code code as follows:
Session sendmailsession;
Sendmailsession = session.getinstance (props, null);
(3) Javax.mail.Transport class
Messages are either sent or can be received. JavaMail uses two different classes to accomplish these two functions: transport and store. Transport is used to send information, and the store is used to collect letters. For this tutorial we only need to use the transport object.
Usage:
Copy Code code as follows:
Transport transport;
Transport = Sendmailsession.gettransport ("SMTP");
Copy Code code as follows:
Transport transport;
Transport = Sendmailsession.gettransport ("SMTP");
Initializes the transport with the Gettransport method of the JavaMail session object. A string that passes past declares the protocol to use for the object, such as "SMTP." This will save us a lot of time. Because JavaMail has a lot of protocols implemented in the Territory.
Note: JavaMail does not absolutely support each protocol and currently supports IMAP, SMTP, and POP3.
(4) Javax.mail.MimeMessage class
The Message object stores the e-mail messages we actually send, and the messages object is created as a MimeMessage object and needs to know which JavaMail session should be chosen.
Usage:
Copy Code code as follows:
Message Newmessage = new MimeMessage (sendmailsession);
Copy Code code as follows:
Message Newmessage = new MimeMessage (sendmailsession);
Message Newmessage = new MimeMessage (sendmailsession);
(5) Javax.mail.InternetAddress class
Once you have created the session and message and filled it in with messages, you can use address to determine the mailing addresses. As with the message, the address is an abstract class. You are using the Javax.mail.internet.InternetAddress class.
Usage:
Copy Code code as follows:
InternetAddress from=new internetaddress ("xxf@cafe.com");
Copy Code code as follows:
InternetAddress from=new internetaddress ("xxf@cafe.com");
(6) Javax.mail.Store class
The store class implements read, write, monitor, and find operations on specific mail protocols. The Javax.mail.Folder class can be accessed through the Javax.mail.Store class.
Usage:
Copy Code code as follows:
Store Store=s.getsorte ("POP3");//s for a mail session
Store.connect (Popserver,username,password)//through the pop address, username and password you provide to login to your mailbox
Copy Code code as follows:
Store Store=s.getsorte ("POP3");//s for a mail session
Store.connect (Popserver,username,password)//through the pop address, username and password you provide to login to your mailbox
(7) Javax.mail.Folder class
The folder class is used to organize messages hierarchically and to provide the ability to access email in Javax.mail.Message format.
Usage:
Copy Code code as follows:
Folder Folder=store.getfolder ("INBOX");
Folder.open (folder.read_only);
Copy Code code as follows:
Folder Folder=store.getfolder ("INBOX");
Folder.open (folder.read_only);
(8) Javax.mail.Internet.MimeMultpart
A container that typically holds e-mail content is a multipart abstract class that defines ways to add and remove and get different portions of e-mail. Since multipart is an abstract class, we have to use a specific subclass for it, JavaMail The API provides the Javax.mail.Internet.MimeMultpart class to use the MimeMessage object.
Usage:
Copy Code code as follows:
Mimemultipart multipart=new Mimemultipart ();
Copy Code code as follows:
Mimemultipart multipart=new Mimemultipart ();
Note: One way we use the Mimemultipart object is Addbodypart (), which adds bodypart to our email content (BodyPart class is described below). Messages can have many parts, A bodypart can represent a part.
(9) Javax.mail.Internet.MimeBodyPart class
MimeBodyPart is a subclass of BodyPart specifically used for mimemessage.
The MimeBodyPart object represents a part of the content of a MimeMessage object. Each mimebodypart is considered to have two parts:
⊙ a MIME type
⊙ match this type of content
Usage:
Copy Code code as follows:
MimeBodyPart mdp=new MimeBodyPart ();
String text= "Hello javamail!";
Mdp.setcontent (text, "Text/plain");//define MIME type as Text/plain and set MimeBodyPart content.
Copy Code code as follows:
MimeBodyPart mdp=new MimeBodyPart ();
String text= "Hello javamail!";
Mdp.setcontent (text, "Text/plain");//define MIME type as Text/plain and set MimeBodyPart content.
(a) Javax.activation.DataHandler class (included in JAF)
JavaMail APIs do not restrict information to text only, and any form of information can be a part of the mimemessage. In addition to textual information, it is common to include file attachments as part of the e-mail message. The JavaMail API provides an easy way to allow us to include text-BodyPart objects by using the DataHandler object.
Usage:
Copy Code code as follows:
DataHandler dh=new DataHandler (text,type);
Mdp.setdatahandler (DH);//MDP is a MimeBodyPart object
Copy Code code as follows:
DataHandler dh=new DataHandler (text,type);
Mdp.setdatahandler (DH);//MDP is a MimeBodyPart object
(one) Javax.activation.FileDataSource class (included in JAF)
A Filedatasource object can represent a resource that local files and servers can access directly. A local file can be attached to a MimeMessage object by creating a new MimeBodyPart object.
Usage:
Copy Code code as follows:
Mimemultipart mm=new Mimemultipart ();
MimeBodyPart mdp=new MimeBodyPart ();
Filedatasource fds=new Filedatasource ("C:/exam.txt");
Mdp.setdatahandler (New DataHandler (FDS)); Setting up a data source
Mm.addbodypart (MDP); Mimemultipart object for current message MimeBodyPart
Copy Code code as follows:
Mimemultipart mm=new Mimemultipart ();
MimeBodyPart mdp=new MimeBodyPart ();
Filedatasource fds=new Filedatasource ("C:/exam.txt");
Mdp.setdatahandler (New DataHandler (FDS)); Setting up a data source
Mm.addbodypart (MDP); Mimemultipart object for current message MimeBodyPart
(Javax.activation.URLDataSource) class (included in JAF)
Remote resources, URLs do not point to them and are represented by a Urldatasource object. A remote resource can be attached to a MimeMessage object (similar to Filedatasource) by creating a new MimeBodyPart object.
Usage:
Copy Code code as follows:
The only difference with Filedatasource is the settings for the data source:
Urldatasource uds=new Urldatasource ("/java/uploadfiles_6441/200703/20070320105128501.gif");