JavaMail Operation Guide (1)

zhaozj2021-02-16  34

Bromon original, please respect the copyright

How to compare a complete JavaMail operation guide? I think it should include most basic Email operations to cope with general applications. In this guide, it is intended to cover the following:

● Send email: including text messages, HTML messages, messages with accessories, SMTP authentication ● Receive email: POP3 remote connection, charge different MIME messages, process attachments

I want to have the above function, you should cope with a lot of Email's related applications. So please allow me to give this article a more arrogant name, so that the ratings can be guaranteed. Or the reason, the reason for writing this POST is not to see more comprehensive online, if you have seen, please remember to tell me.

All of the following examples have been actually tested, you can say that it is not OO, not Plugable, but it is indeed referred to. Since I have JavaMail, it is convenient to send spam. This document is much less, this is not my lazy, but a lot of things involving POP3 and other agreements. If you don't understand these norms, I really don't know how to explain it with you; if you know, then I basically No need to explain it. Therefore, in the utmost principle, it will be omitted, and if you are interested, you will turn over the protocol specification.

Tony is less nonsense, first need to configure the environment. The needed package is mail.jar and activation.jar. High version of J2SDK EE comes with. Address, then search on java.sun.com, it is easy to find. Put it in ClassPath.

First, email transmission

Let's get an email Hello World, hot and warm:

/ ************* Name: TextMail Shender.java Author: bromon version: 1.0 date: 2004-4-26 Note: Send email to bromon@163.com, you need to install SMTP server *** ********* / pack org.bromon.mail; import javax.mail. *; Import javax.mail.internet. *; Import java.util. *; Public class textmailsender {public static void main String args []) {Try {Properties Prop = New Properties (); // Specify the SMTP server to be used for Bromon2k prop.put ("mail.smtp.host", "bromon2k"); session mailsession = session.getDefaultInstance PROP);

// sender address InternetAddress from = new InternetAddress ( "bromon @ bromon2k"); // recipient address InternetAddress to = new InternetAddress ( "bromon@163.com"); MimeMessage msg = new MimeMessage (mailSession); msg .SG.AddRecipient (javax.mail.Message.RecipientType.to, to); // Send a date msg.setsentdate (new java.util.date ()); // title msg.setSubject (" Hello "); // message body msg.Settext (" Hello, bromon "); transport.send (msg);} catch (exception e) {system.out.println (e);}}} The program is simple, But it can't run (inverted). Unless you have an SMTP server installed on your machine, and your machine is also called bromon2k. Write such a process that cannot be executed is not to find it, but let everyone have a basic impression on JavaMail, I am too lazy to change. The following demonstrates how to send an email, basic operations and above, just more SMTP authentication through 163, sohu and other email service providers, just one SMTP verification:

/ * * CREATED ON 2004-4-26 * / package org.bromon.mail; import javax.mail. *; Import java.util. *; Import javax.mail.internet. *;

/ ** * @Author bromon * / public class senderwithsmtpver {string host = "; string user =" "; string password ="

Public void sethost (string host) {this.host = host;}

Public void setaccount (string user, string password) {this.user = user; this.password = password;}

Public void send (String from, String to, String Subject, String Content) {Properties PROPS = New Properties (); Props.Put ("mail.smtp.host", host); // Specify SMTP Server Props.put (" Mail.smtp.auth "," true "); // Specify if SMTP verification try {session mailsession = session.getDefaultInstance (props); mailsession.setdebug (true); // Whether to display Debug information in the console = Messis new MimeMessage (mailSession); message.setFrom (new InternetAddress (from)); // sender message.addRecipient (Message.RecipientType.TO, new InternetAddress (to)); // recipient message.setSubject (subject) ; // message subject message.setText (content); // message content message.saveChanges (); Transport transport = mailSession.getTransport ( "smtp"); transport.connect (host, user, password); transport.sendMessage (message Message.GetallRecipients ()); transport.close ();} catch (exception e) {system.out.println (e);}} public static void main (String args []) {senderwithsmtpver sm = new senderwithsmtpver () ;

Sm.sethost ("smtp.163.com"); / / Specify the mail server sm.setAccount ("ABC", "123") to use; // Specify an account and password

/ * @ @ @Param string sender's address * @Param String Recipient address * @Param string mail title * @Param string mail body * / sm.send ("abc@163.com", "bromon @ 163. COM "," Title "," Content ");

}

This program doesn't seem to explain, write SMTP addresses, accounts, passwords and other configuration information inside, and many APIs in Java need to do this, such as adding support for proxy servers, etc.

The above program modifies the server address, account, and password can be used, very simple.

How to send an HTML format Email? It is also very simple, then write the HTML code in the body, then specify the contentType of the message is OK, only the key code is given below:

........ mimeMessage msg = new mimeMessage (mailsession); msg.setcontent (content, "text / html"); msg.settext ("

below, how are you? "); .......... The following is the email with attachments, a little more complicated, and there are some different procedures, please carefully, while needing a little IO knowledge. The same code is not listed, write only the key part, who wants to be lazy?

Import javax.mail. *; import javax.mail.internet. *; import javax.activation. *; import java.util. *; ......... MimeMessage msg = new mimeMessage (MSG.SetSentDate); msg.setsentdate (New Date) )); Msg.setsubject ("hello");

MIMEBODYPART TEXTBODYPART = new mimebodypart (); TextBodypart.Settext ("Mail Text");

MimeBodyPart fileBodyPart = new MimeBodyPart (); FileDataSource fds = new FileDataSource ( "GIS.rar"); Annex fileBodyPart.setDataHandler // to send (new DataHandler (fds)); fileBodyPart.setFileName (fds.getName ()); Multipart Container = new mimemultipart (); container.addbodypart (TextBodypart); Container.Addbody (FileBodypart); msg.setcontent (container); Transport.send (MSG); ............

The MSG here is composed of two mimebodypart. This thing is basically difficult. If you don't understand the relevant specifications, you will not explain. If you understand, I don't have to explain, this ..........

转载请注明原文地址:https://www.9cbs.com/read-21733.html

New Post(0)