Java Send mail example to explain _java

Source: Internet
Author: User
Tags auth base64 mail example

The example of this article explains the detailed process of Java email, for your reference, the specific content as follows

1. Mail Agreement

E-mail: SMTP (Simple Mail transport protocal)

protocol for receiving mail: POP3 (Post Office protocal 3)

IMAP new Protocol Send mail also can receive mail.

(Step-by-Step interaction with the server)

Smtp:

2, the process of sending and receiving mail:

Typically, SMTP and POP3 are two servers (hosts).

SMTP Mail has a port of 25.

The POP3 port is 110.

Example of sending a message

1), Installation Foxmail:

2), to send the mail, the user name and password to Base64 code

 The username and password are base64 encoded

 @Test public

 void base64 () {

  String name = "wj_leaf12345";

  String pwd = "1qaz2wsx";

  Base64encoder en = new Base64encoder ();

  Name = En.encode (Name.getbytes ());

  PWD = En.encode (Pwd.getbytes ());

  SYSTEM.ERR.PRINTLN (name);

  System.err.println (PWD);

 }

3, email via Java code

To send a message in Java, you must import a new package

mail.jar– the core package of outgoing mail

activation.jar– encrypts the user and password.

There are three core classes in Mail.jar:

Javax.mail.session– refers to a session with a mail server. Just one on the whole project.

Javax.mail.Message (interface)-Prepares to send data information.

MimeMessage-You can set the type of data information.

transport– it has a way to send messages.

First step: Import two jar packages

Step two: Send a simple message

public void SendMail () throws exception{//First step: Declare properties object put information properties prop = new properties ();

  Set up which server to connect Prop.setproperty ("Mail.host", "smtp.126.com");

  Sets whether to validate Prop.setproperty ("Mail.smtp.auth", "true"); Step two: Declare user name and password Authenticator auth = new Authenticator () {//This Shantiu returns the user and password object public passwordauthentication Getpass

    Wordauthentication () {passwordauthentication pa = new Passwordauthentication ("AAA", "SSS");

   Return PA;

  }

  };

  Step two: Get Session object Session session = Session.getdefaultinstance (Prop,auth);

  Sets the debug mode Session.setdebug (True) of the session;

  

  Step three: Declare information mimemessage MM1 = new MimeMessage (session);

  Fourth Step: Set Sender email address from = new InternetAddress ("wj@126.com");

  Mm1.setfrom (from);

  Fifth step: Set the recipient mm1.setrecipient (Recipienttype.to,new internetaddress ("wj@163.com"));

  Mm1.setrecipient (recipienttype.cc, New internetaddress ("554@qq.com")); Mm1.setrecipient (RECIPIENTTYPE.BCC, New InterNetaddress ("wj@ss.cn"));

  Step Sixth: Set the theme Mm1.setsubject ("This is the message 3 in Java");

  Mm1.setcontent ("Hello, this is sent in Java Mail, 3333 try again", "text/plain;charset=utf-8");

 Seventh Step: Transport.send (MM1);

 

 }

Step three: V send a message with a hyperlink

Mm1.setsubject ("This is a mail sfasdf3 with Java");

  Mm1.setcontent ("Hello, this is a mail sent in Java, <a href= ' http://www.baidu.com ' > Baidu </a>", "Text/html;charset=utf-8");

  Seventh Step:

  transport.send (MM1);

Step Fourth: Message for a character piece

public void SendFile () throws exception{Properties P = new properties ();

  P.setproperty ("Mail.host", "smtp.163.com");

  P.setproperty ("Mail.smtp.auth", "true"); Session s = session.getdefaultinstance (P,new authenticator () {@Override public passwordauthentication GetPassword

   Authentication () {return new passwordauthentication ("ww", "123");

  }

  });

 

  S.setdebug (TRUE);

  Statement MimeMessage mimemessage msg = new MimeMessage (s);

  Msg.setfrom (New InternetAddress ("ww@163.com"));

  Msg.setrecipient (recipienttype.to, New internetaddress ("ww@126.com"));

  

  Msg.setsubject ("Picture's");

  First step: Declare the part mimemultipart mm = new Mimemultipart ();

  Step two: Declare mimebodypart body1 = new MimeBodyPart ();

  Step three: Set DataSource ds = new Filedatasource (New File ("./img/a.jpg"));

  DataHandler dh = new DataHandler (DS);

  Body1.setdatahandler (DH);

  You must set the name Body1.setfilename (Mimeutility.encodetext ("Belle. jpg"));

  MimeBodyPart body2 = new MimeBodyPart (); //Step Three: Set DataSource ds2 = new Filedatasource (New File ("./img/b.jpg"));

  DataHandler DH2 = new DataHandler (DS2);

  Body2.setdatahandler (DH2);

  You must set the name Body2.setfilename (Mimeutility.encodetext ("Beauty 2.jpg"));

  MimeBodyPart body3 = new MimeBodyPart ();

  Step three: Set DataSource DS3 = new Filedatasource (New File ("./img/m.mp3"));

  DataHandler dh3 = new DataHandler (DS3);

  Body3.setdatahandler (DH3);

  You must set the name Body3.setfilename (Mimeutility.encodetext ("The end of the century. mp3"));

  Add body1 to mm Mm.addbodypart (body1);

  Mm.addbodypart (BODY2);

  Mm.addbodypart (BODY3);

  

  Msg.setcontent (mm);

 Send Transport.send (msg);
 }

The above is the entire content of this article, I hope to help you learn.

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.