JavaMail Fine Extraction __java

Source: Internet
Author: User
Tags auth java se

-- Introduction to Start JavaMail

JavaMail is a package that is used to send e-mail, and Java EE (J2ee.jar) contains the package, but if you are using Java SE, you must first download the JavaMail package.

Download JavaMail

You can search for JavaMail download in Google and then go to the official website to download the latest JavaMail package. Then add Mail.jar to your classpath. Notably, if you are using the version below Java SE 6, you will also need to download the JavaBeans Activation Framework package and add Activation.jar to your classpath.

Mail Protocol

Typically, the mail server uses one or more of the following protocols to send and receive e-mail. SMTP (Simple Mail Transfer Protocol) POPs (Post Office Protocol) IMAP (Internet message Access Protocol) MIME (Multipurpose Int Ernet Mail Extensions) NNTP (Network News transport Protocol)

In fact, it is precisely because JavaMail encapsulates these protocols that we only have to face APIs when sending and receiving emails, without caring about the details of the protocol.

one of the simplest examples

Import Java.util.Date;

Import java.util.Properties;
Import Javax.mail.Authenticator;
Import Javax.mail.Message;
Import javax.mail.MessagingException;
Import javax.mail.PasswordAuthentication;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.InternetAddress;

Import Javax.mail.internet.MimeMessage;
	public class Test {//modify here to use your own mail server to send mail public static final String HOST = "smtp.163.com";
	public static final String USER = "wave0409";
	
	public static final String PASSWORD = "*******************"; public static final String address = "wave0409@163.com";
	Note Modify here, don't harm me public static final String SUBJECT = "Test";

	public static final String TEXT = ' This is a test mail. ' public static void Main (string[] args) throws Exception {try {//Set JavaMail Property Properties props = new properties (
			); Props.put ("Mail.smtp.host", host); Mail server Props.put ("Mail.transport.protocol", "SMTP"); Protocol Props.put ("Mail.smtp.auth", "true"); Whether to require authentication(username, password)//authentication Authenticator auth = new Authenticator () {@Override protected passwordauthentication ge
				Tpasswordauthentication () {return new passwordauthentication (USER, PASSWORD);

			}
				
			};

			Gets the session instance session Sendmailsession = Session.getinstance (props, auth);
			Construct mail Message newmessage = new MimeMessage (sendmailsession);
			Newmessage.setfrom (address) (new internetaddress);
			Newmessage.setrecipient (Message.RecipientType.TO, new internetaddress (address));
			Newmessage.setsubject (SUBJECT);
			Newmessage.setsentdate (New Date ());

			Newmessage.settext (TEXT);
			Send mail Transport.send (newmessage); SYSTEM.OUT.PRINTLN ("Send success, please check.")
		");
		catch (messagingexception m) {m.printstacktrace ();
 }

	}
}

more examples

In fact, there are many things you can do with JavaMail, such as sending an HTML message, sending an attachment, reading a message, and so on. More examples can be found in the demo folder of the downloaded JavaMail package.

-- more see: Java EE Extract
-- statement: Reprint please indicate the source
--Last Updated on 2012-08-16
--written by Shangbo on 2012-07-28
--End

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.