JavaMail---Mail sending

Source: Internet
Author: User
Tags stringbuffer

Jar Package Dependencies:

1 /**2 * Mail integration via SMTP3  */4  Public classCmpsendmail {5     //Mail sending server host6     Private Final StaticString HOST = "url";7 8     //Mail Delivery Protocol9     Private Final StaticString PROTOCOL = "SMTP";Ten  One     //Whether you need identity authentication A     Private Final StaticString Is_auth = "true"; -  -     //Sender the     Private StaticString from = "[Email protected]"; -  -     /** - * Initialize connection right-click Server session Information +      */ -     Private StaticProperties props =NULL; +     Static { AProps =NewProperties (); atProps.setproperty ("Mail.transport.protocol", PROTOCOL); -Props.setproperty ("Mail.smtp.host", HOST); -Props.setproperty ("Mail.smtp.auth", Is_auth); -     } -  -     /** in * Submit authentication information to the server -      */ to     Static classMyauthenticatorextendsAuthenticator { +         PrivateString username = "40545"; -  the         PrivateString password = "11111"; *  $          PublicMyauthenticator () {Panax Notoginseng             Super(); -         } the  +          PublicMyauthenticator (string Username, string password) { A             Super(); the              This. Username =username; +              This. Password =password; -         } $  $          Publicpasswordauthentication getpasswordauthentication () { -             return Newpasswordauthentication (username, password); -         } the     } - Wuyi     /** the * CMP Mail Integration -      *  Wu      * @paramAddress -      */ About      Public Static voidSendcmpemail (internetaddress[] address, String theme, $String content)throwsException { -  -         //Get mail session strength Object -Session session = Session.getinstance (props,Newmyauthenticator ()); A  +         //Creating an MimeMessage instance Object theMimeMessage msg =NewMimeMessage (session); -  $         //Set Sender theMsg.setfrom (Newinternetaddress (from)); the  the         //Set Recipient the msg.setrecipients (recipienttype.to, address); -  in         //Set Send Message theMsg.setsentdate (NewDate ()); the  About         //Set Message subject theMsg.setsubject (Theme + "[System Alert]"); the  theStringBuffer con =NewStringBuffer (); +Con.append ("<a style= ' text-decoration:none; ' href= '" -+ content.tostring () + "' > View details </a></p>"); the Bayi         //Set Message text content theMsg.setcontent (Con, "Text/html;charset=utf-8"); the  -         //save and generate the final message content - msg.savechanges (); the  the         //Send mail the transport.send (msg); the     } -  the}

JavaMail Common objects:

(1) Javax.mail.Properties class
JavaMail needs the properties to create a Session object. It looks for the string "Mail.smtp.host" and the property value is the host that sent the message.
Usage:
Properties Props = new properties (); Properties props = System.getproperties ();
Props.put ("Mail.smtp.host", "smtp.163.com");//You can change your SMTP host name.
(2) Javax.mail.Session class
This session class represents an e-mail session in JavaMail. Each JavaMail-based 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:
Session sendmailsession = session.getinstance (props, null); No certification required
(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 messages, and the store is used to collect mail. We only need to use the transport object to send the message here.
Usage:
Transport Transport = Sendmailsession.gettransport ("SMTP");
Initializes the transport with the Gettransport method of the JavaMail session object. The passed string declares the protocol to be used by the object, such as "SMTP". This will save us a lot of time. Because JavaMail is implemented with many protocols built into it.
Note: JavaMail does not fully support each protocol and currently supports IMAP, SMTP, and POP3.
(4) Javax.mail.MimeMessage class
The message object stores the e-mail messages that we actually send, and the message object is created as a MimeMessage object and needs to know which JavaMail session should be selected.
Usage:
Message newmessage = new MimeMessage (sendmailsession);
(5) Javax.mail.InternetAddress class
Once you have created a Session and a message and filled it with messages, you can use address to determine the correspondence. Like the Message, Address is an abstract class. You are using the Javax.mail.internet.InternetAddress class.
Usage:
InternetAddress from=new internetaddress ("[email protected]"); Recipient email Address
(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:
Store Store=s.getsorte ("POP3"); S is a mail session
Store.connect (Popserver,username,password);//log in to your mailbox via the Popserver address (mailbox server) you provide, username and password
(7) Javax.mail.Folder class
The folder class is used to organize messages hierarchically and provides the ability to access email in Javax.mail.Message format.
Usage:
Folder Folder=store.getfolder ("INBOX");
Folder.open (folder.read_only);
(8) Javax.mail.Internet.MimeMultipart
The container that typically holds e-mail content is the multipart abstract class, which defines ways to add and delete and get different parts of the e-mail message. Since multipart is an abstract class, we must use a specific subclass for it, JavaMail The API provides the Javax.mail.Internet.MimeMultpart class to use the MimeMessage object.
Usage:
Mimemultipart multipart=new Mimemultipart ();
Note: One way we use the Mimemultipart object is Addbodypart (), which adds the BodyPart (BodyPart class below) to our e-mail content. The message can have a lot of parts, A bodypart can represent a part.
(9) Javax.mail.Internet.MimeBodyPart class
MimeBodyPart is a subclass of BodyPart specifically 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:
MimeBodyPart mdp=new MimeBodyPart ();
String text= "Hello javamail!";
Defines the MIME type as Text/plain and sets the contents of the MimeBodyPart.
Mdp.setcontent (text, "Text/plain");
(Ten) Javax.activation.DataHandler class (included in JAF)
The JavaMail API does not restrict information to text only, and any form of information may be part of a mimemessage. In addition to textual information, it is common to include a file attachment as part of an e-mail message. The JavaMail API provides an easy way to allow us to include non-text BodyPart objects by using the DataHandler object.
Usage:
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 local file and a resource that the server can access directly. A local file can be attached to a MimeMessage object by creating a new MimeBodyPart object.
Usage:
Mimemultipart mm=new Mimemultipart ();
MimeBodyPart mdp=new MimeBodyPart ();
Filedatasource fds=new Filedatasource ("C:/exam.txt");
Mdp.setdatahandler (New DataHandler (FDS)); Set up a data source
Mm.addbodypart (MDP); Adds MimeBodyPart to the current message Mimemultipart object
(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.

Related Learning JavaMail Blog links :

java send mail with URL, html

JavaMail sending e-mail using the SMTP protocol (detailed)

JavaMail---Mail sending

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.