Talking about JavaEmail: sending the first mail

Talking about JavaEmail: sending the first mail

Preface

Yes, there is a demand to power, this time the boss let me look at how to use the Java code to send an email. Zhengchou nothing can engage with, today is the Qixi Festival, you engage in this demand, spent half a day to fix, so in this one of the fill hole process.

Talking about JavaEmail: sending the first mail

Mail protocol

When it comes to mail, you have to say under the mail protocol, which is divided into SMTP, POP3, IMAP three protocol, because here we do not charge to the mailbox, so we only need to use SMTP. Here we have the concept of a third protocol to make a summary.

SMTP (send mail protocol, the default port 25 (POP3) to receive mail protocol, the default port 110, IMAP (not online) receive mail protocol, the default port 143, running on the TCP/IP protocol, and POP3 the main difference: online operation, the user can not put all the mail all the download, can be directly through the client the messages from the server to operate)

JavaEmail send mail flow

The main process of sending JavaEmail mail is the four step, in which the third steps are optional. They are the connection server, edit mail, add attachments, and send mail. As shown in the following picture

Talking about JavaEmail: sending the first mail

JavaEmail development configuration

Since I created a Maven project, I added the following Maven dependency in pom.xml

< properties> < javamail.version> 1.4.1< /javamail.version> < /properties> < dependencies> < dependency> < groupId> javax.mail< /groupId> < artifactId> mail< /artifactId> < version> ${javamail.version}< /version> < /dependency> < /dependencies>

But heard in the development of JDK6.0, do not need to import the mail.jar, because the official JDK is already included in the jar package, but I did not find, so I built on. If both are present, there will be the following error.

Java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream

The solution is simple, as long as you delete the mail directory and the activation directory inside javaee.jar (if you want to go once and for all, go to the hard disk location where javaee.jar is located, and delete the two directories).

Send a plain text mail

No matter what the message is sent, here I suggest you use e-mail, because ordinary mail seems to be sending limit to the number of times. I use here is QQ enterprise mailbox account is provided by the company. Here we need to check the mailbox settings page two places. One is the address of the server and we receive information, two is to see if we have opened the corresponding sending and receiving service. As shown below.

Talking about JavaEmail: sending the first mail

Up up up see from the figure, we know that the Tencent enterprise mailbox is send address for smtp.exmail.qq.com, using SSL port 465, there is a hole, that is if you don’t use SSL, then port 25, and Sina, NetEase mailbox port is consistent.

view after we want, we need to connect to the server. First we need to set up the server parameters. Here we use Properties to store the server connection information. As shown below.

Private static String smtpHost = "smtp.exmail.qq.com"; private static; String port = "25" private static Properties props = System.getProperties (public); static void main (String[] args) throws AddressException, MessagingException UnsupportedEncodingException{, props.put (mail.smtp.host, smtpHost); props.put ("mail.smtp.auth", "true"); props.put ("mail.smtp.port, port); props.setProperty (" mail.transport.protocol "," SMTP ");}

Then, we connect the server with the above server information

Session session = Session.getInstance (props); / / session.setDebug session connection with the mail server (true); / / open Debug mode

When the connection is complete, we’ll need to edit our mail. Here you’ll need the MimeMessage class to implement the code as follows

MimeMessage message = new MimeMessage (session); / / on behalf of an email (New message.setFrom InternetAddress ("email address"); message.setRecipients (Message.RecipientType.TO), "receive mail address"); message.setSubject ("Sao, send you a surprise"); message.setText ("text");

After the preparation of the address, we need to send this email, here we need to use the Transport class to realize the function of sending mail. Of course, we need to verify the account and password to send us during the sending process. As shown below.

Transport transport = session.getTransport (transport.connect); / / identity verification email before ("send mail account", "password"); transport.sendMessage (message) (message.getAllRecipients);

Up up up note: mail account and email address may be different at ~

In this way, we successfully sent an email. The effect is as follows. (full Demo at the bottom of the article)

Talking about JavaEmail: sending the first mail

Send a formatted message (HTML form)

Assume that a text has been unable to meet our needs, we need to have the format of the mail, then how should we do? We need to use the MimeMultipart and MimeBodyPart of the two classes to achieve.

The connection server in the front does not need to change the delivery of mail, similar to plain text, and the change is to edit the contents of the mail. The code is as follows

MimeBodyPart content = new, MimeBodyPart (); content.setContent ("<"; "h1>"; attachment is a catchy song! < /h1> text/html; "," charset=utf-8 "); MimeMultipart mimeMultipart = new (MimeMultipart); mimeMultipart.addBodyPart (content); mimeMultipart.setSubType (" mixed "); message.setContent (mimeMultipart); / / add theme content

After normal execution, inbox gets the following mail, and we find the attachment is a nice song! It’s really the form of the H1 tag

Talking about JavaEmail: sending the first mail

Send an email with attachments

It says there are accessories, then how can not work on how to send a e-mail attachments? As above and add format, we need to use the MimeMultipart and MimeBodyPart of the two classes to achieve.

The specific part of the code is shown below

MimeBodyPart attachment = new (MimeBodyPart); attachment.setDataHandler (New DataHandler (New FileDataSource ("attachment to the local address)); attachment.setFileName (MimeUtility.encodeText) (" music.Mp3 ")); MimeBodyPart content = new (MimeBodyPart); content.setContent (" < h1> attachment is a nice song! < /h1> text/html; "," charset=utf-8 "); MimeMultipart mimeMultipart = new (MimeMultipart); mimeMultipart.addBodyPart (attachment); mimeMultipart.addBodyPart (content); mimeMultipart.setSubType (" mixed "); message.setContent (mimeMultipart);

At this point, when we send the mail, our mail will appear in the corresponding mailbox. As shown in the following picture

Talking about JavaEmail: sending the first mail

The article’s overall code is as follows

Package com.infosports.emaildemo; import java.io.UnsupportedEncodingException; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class App private static String {smtpHost = "smtp.exmail.qq.com"; private static String = "formPerson email address"; private static String toPerson = "post A "private static String accept account; port = 25; private = System.getProperties (static Properties props); public static void main (String[] args) throws AddressException, MessagingException UnsupportedEncodingException{, props.put (mail.smtp.host, smtpHost); props.put (" mail.smtp.auth "," true "); props.put (" mail.smtp.port port ".); props.setProperty (" mail.transport.protocol "," SMTP "); Session session = Session.getInstance (props); / / session.setDebug session connection with the mail server (true); MimeMessage message = new MimeMessage (session); / / represents an e-mail message.setFrom (New InternetAddress (formPerson) message.setRecipients (Message.); RecipientType.TO, toPerson); message.setSubject ("Sao, send you a surprise"); / / message.setText ("mail text"); / / MimeBodyPart = new MimeBodyPart attachment accessories (attachment.setDataHandler (New); DataHandler (New FileDataSource ("accessory path")); attachment.setFileName (MimeUtility.encodeText) ("music.Mp3") MimeBodyPart; content) = new (MimeBodyPart); content.setContent ("< h1> attachment is a beautiful song! < /h1> text/html; "," charset=utf-8 "); MimeMultipart mimeMultipart = new (MimeMultipart); mimeMultipart.addBodyPart (attachment); mimeMultipart.addBodyPart (content); mimeMultipart.setSubType (" mixed "); message.setContent (mimeMultipart); Transport transport = session.getTransport (transport.connect); / / identity verification email before (formPerson," send account password "); transport.sendMessage (message), (message.getAllRecipients);}}

summary

Use JavaEmail to send mail to here, what is the lack of welcome to criticize Sao building, here thank you. I hope you love this article. Not much to say. Today is the Qixi Festival, I wish you all happy ~ the Qixi Festival

Talking about JavaEmail: sending the first mail


Talking about JavaEmail: sending the first mail