Java in the use of JavaMail e-mail and message verification and attachment implementation _java

Source: Internet
Author: User
Tags auth sessions

Multi-hair and user authentication
here's how to implement user authentication by sending messages to multiple recipients and using the Authenticators object.
When you specify a recipient, there are two ways to specify it. The last blog is to specify the recipient temporarily when sending the message, but it can also be specified in the Messages object.

Message.addrecipient (message.recipienttype.to,new internetaddress ("995812509@99.com")); 

This is only sent to a recipient, but how many recipients handle it? There are also two ways to handle this.
1, when sending the message when the transport SendMessage () method to specify the recipient is to use an array to specify the recipient, this time we only need to add more recipient address to complete.
2, in the use of the Message object to add a recipient we can use the parse (string) method of the InternetAddress object, which returns a internetaddress array, which can also be implemented to send to multiple recipients.

We know that in the development of javamail we must verify the authorization, the authorization to check the purpose is to prevent other people arbitrary hair mail, reduce the production of spam.
We can validate the session object when we get it. There are two methods in the Session object:

    • Getdefaultinstance (Prop,authenticator),
    • GetInstance (Prop,authenticator),

Both of these methods have a common parameter authenticator, which is a authenticator object. The authenticator object is to help the user to authenticate the information, complete the authorization check. The authenticator object has the Getpasswordauthentication () method, which returns a Passwordauthentication object, There are two methods in the Passwordauthentication object: GetPassword (), GetUserName () also say that we will password, username encapsulated in the Passwordauthentication object, These two methods allow you to get the username and password. Can complete user information verification.

Examples are as follows:

public class Javamail_02 {public static void main (string[] args) throws Exception {Properties props = new Prope 
    Rties (); 
    Props.setproperty ("Mail.smtp.auth", "true"); 
    Props.setproperty ("Mail.transport.protocol", "SMTP"); 
     
    Props.setproperty ("Mail.host", "smtp.163.com"); Session sessions = Session.getinstance (props, New authenticator () {protected passwordauthentication get 
          Passwordauthentication () {Return to New Passwordauthentication ("********", "*********"); 
    } 
    }); 
     
    Session.setdebug (TRUE); 
    msg = new MimeMessage (session); 
     
    Msg.setfrom (New InternetAddress ("chenssy995812509@163.com")); 
    Msg.setsubject ("JavaMail test Procedure ..."); 
    Msg.setcontent ("<span style= ' color:red ' > This is my second JavaMail test program ....</span>", "TEXT/HTML;CHARSET=GBK"); Msg.setrecipients (recipienttype.to, New address[]{new internetaddress ("1111@ @qq. com"), New InternetAddress ("2222@ 
    QQ.CPM ")}); Msg.setreCipients (recipienttype.to, Internetaddress.parse ("995812509@qq.com,1247723213@qq.com")); 
  Transport.send (msg); 

 } 
 
}

Messages with pictures and attachments
in the actual e-mail we will generally involve more complex e-mail structures, such as attachments, message body contains pictures, including songs, etc., at this time we have to have a clear understanding of the structure of the message. You need to understand the structure of a composite message before you can develop a composite message.

The image above shows the overall structure of a composite message, and we can see that a complex e-mail message is made up of several parts. It has a head and body, but the body is not as simple as it used to be, but it is made up of several parts. The head needs to play an indicative role, it needs to explain the text needs to use what kind of separator to separate, the body of several parts use what kind of combination relationship. For the above email it consists of three parts, each part having its own head and body, and the first part is composed of two parts.

Composition of composite messages:
There are multiple combinations of body parts. The combined relationship is as follows:

Alternative: Select the relationship. Between the plain text and the hypertext above is a choice relationship.
Related: Associating relationships. If the above hypertext text is to display a picture, then we must send this picture to include in the mail, namely the so-called embedded resources, this embedded resource is for hypertext. So there is a correlation between them.
Mixed: Mixed relationship. In plain text, hypertext and embedded resources to form a whole and, they are tied to the attachment, between the two is a mixed relationship.

The API for composite Mail organization structure:

The MimeMessage class represents an entire e-mail message.
The MimeBodyPart class represents a MIME message for a message.
The Mimemultipart class represents a combined MIME message that is composed of multiple MIME messages.

Here's an example: The message contains two attachments, the body part includes plain text and hypertext, and the hypertext representation shows a picture. The source code is as follows:

public class Javamail_03 {public static void main (string[] args) throws Exception {Properties props = new Pro 
    Perties (); 
    Props.setproperty ("Mail.smtp.auth", "true"); 
    Props.setproperty ("Mail.transport.protocol", "SMTP"); 
    Props.setproperty ("Mail.host", "smtp.163.com"); Session sessions = Session.getinstance (props, New authenticator () {protected passwordauthentication get 
          Passwordauthentication () {return new Passwordauthentication ("* * *", "Hu Jintao"); 
     
    } 
    }); 
    Message message = new MimeMessage (session); 
    Message.setsubject ("Third JavaMail test Procedure"); 
    Message.setfrom (new internetaddress ("\" "+mimeutility.encodetext" ("Chen") + "<chenssy995812509@163.com>")); 
     
    Message.setrecipients (recipienttype.to, New address[]{new internetaddress ("995812509@qq.com")}); 
    Message body Mimemultipart multipart = new Mimemultipart ("mixed"); 
    Message.setcontent (multipart); * * Create the content of the message * including aMail body and two attachments */mimebodypart content = new MimeBodyPart ();   Message content MimeBodyPart Attch1 = new MimeBodyPart ();   Annex 1 MimeBodyPart ATTCH2 = new MimeBodyPart (); 
    Annex 2//Add the content of the message to the multipart Multipart.addbodypart. 
    Multipart.addbodypart (ATTCH1); 
     
    Multipart.addbodypart (ATTCH2); 
    Set up attachment 1 DataSource ds1 = new Filedatasource ("g:\\ ebook \\oracle password. txt"); 
    DataHandler DH1 = new DataHandler (DS1); 
    Attch1.setdatahandler (DH1); 
    Attch1.setfilename ("Oracle.txt"); 
    Set up attachment 2 DataSource ds2 = new Filedatasource ("g:\\ ebook \ Account. txt"); 
    DataHandler DH2 = new DataHandler (DS2); 
    Attch2.setdatahandler (DH2); 
    Attch2.setfilename (Mimeutility.encodetext ("account. txt")); * * Set content (body)---is a complex body * including HTML body and display a picture * * Mimemultipart Bodymultipart = new Mimemultipart ("relate 
    D "); 
    Content.setcontent (Bodymultipart); 
    Construct body MimeBodyPart htmlBody = new MimeBodyPart (); MimeBodyPart gifbody = new MimeBodyPart (); 
    Bodymultipart.addbodypart (HtmlBody); 
   
    Bodymultipart.addbodypart (Gifbody); 
    Set Picture DataSource Gifds = new Filedatasource ("f:\\ picture \ Picture \\4.jpg"); 
    DataHandler GIFDH = new DataHandler (GIFDS); 
    Gifbody.setdatahandler (GIFDH); 
    Gifbody.setheader ("Content-id", "<" +gifds.getname () + ">"); 
    Gifbody.setheader ("Content-location", "http://www.itcast.cn/logo.gif"); Set HTML body htmlbody.setcontent ("<span style= ' color:red;font-size:16px ' > This is my third JavaMail test!" included accessories and pictures, a little complicated. 
     
     
    ...</span><br> "+" shows the picture  ', "text/html;charset=utf-8");    Message.savechanges (); 
  Generate mail transport.send (message); 
 } 
 
}

Related Article

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.