Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to send mail

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use java to send e-mail related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe you will get something after reading this article on how to use java to send e-mail, let's take a look.

First take a look at the implementation steps, and then talk about the problems that may be encountered

1. To introduce javax.mail dependency, I use springboot, so the dependency is introduced like this

Org.springframework.boot spring-boot-starter-mail

If you don't use the springboot framework, go and find it yourself.

two。 Construct the basic information class of mail

Package com.example.demo.comment.sendemail;import java.util.Properties;/** * basic information needed to send mail * * @ author 860118060 * / public class MailSenderInfo {/ * IP and port of the server that sent the mail * / private String mailServerHost; private String mailServerPort = "25"; / * * address of the sender * / private String fromAddress / * email recipient's address * / private String toAddress; / * username and password of login mail sending server * / private String userName; private String password; / * whether authentication is required * / private boolean validate = false; / * email subject * / private String subject / * text content of the message * / private String content; / * File name of the message attachment * / private String [] attachFileNames; / * get the message session attribute * / public Properties getProperties () {Properties p = new Properties (); p.put ("mail.smtp.host", this.mailServerHost) P.put ("mail.smtp.port", this.mailServerPort); p.put ("mail.smtp.auth", validate? "true": "false"); return p;} public String getMailServerHost () {return mailServerHost;} public void setMailServerHost (String mailServerHost) {this.mailServerHost = mailServerHost;} public String getMailServerPort () {return mailServerPort;} public void setMailServerPort (String mailServerPort) {this.mailServerPort = mailServerPort;} public boolean isValidate () {return validate } public void setValidate (boolean validate) {this.validate = validate;} public String [] getAttachFileNames () {return attachFileNames;} public void setAttachFileNames (String [] fileNames) {this.attachFileNames = fileNames;} public String getFromAddress () {return fromAddress;} public void setFromAddress (String fromAddress) {this.fromAddress = fromAddress } public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} public String getToAddress () {return toAddress;} public void setToAddress (String toAddress) {this.toAddress = toAddress;} public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName } public String getSubject () {return subject;} public void setSubject (String subject) {this.subject = subject;} public String getContent () {return content;} public void setContent (String textContent) {this.content = textContent;}}

3. Build a mail sender

Package com.example.demo.comment.sendemail;import java.util.Date;import java.util.Properties;import javax.mail.Address;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeBodyPart;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart / * simple mail (mail without attachments) sender * / public class SimpleMailSender {/ * send messages in text format * @ param mailInfo messages to be sent * / public static boolean sendTextMail (MailSenderInfo mailInfo) {/ / determine whether authentication is required MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties () If (mailInfo.isValidate ()) {/ / if authentication is required, create a password verifier authenticator = new MyAuthenticator (mailInfo.getUserName (), mailInfo.getPassword ());} / / construct a session Session sendMailSession = Session.getDefaultInstance (pro,authenticator) to send mail based on mail session properties and password verifier Try {/ / create an email message based on session Message mailMessage = new MimeMessage (sendMailSession); / / create an email sender address Address from = new InternetAddress (mailInfo.getFromAddress ()); / / set the sender mailMessage.setFrom (from) of the email message / / create the address of the recipient of the message and set it to the email message Address to = new InternetAddress (mailInfo.getToAddress ()); mailMessage.setRecipient (Message.RecipientType.TO,to); / / set the subject mailMessage.setSubject of the email message (mailInfo.getSubject ()) / / set the time for sending mail messages mailMessage.setSentDate (new Date ()); / / set the main contents of mail messages String mailContent = mailInfo.getContent (); mailMessage.setText (mailContent); / / send email Transport.send (mailMessage); return true } catch (MessagingException ex) {ex.printStackTrace ();} return false;} / * * send email in HTML format * @ param mailInfo message to be sent * / public static boolean sendHtmlMail (MailSenderInfo mailInfo) {/ / determine whether authentication is required MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties () / / if authentication is required, create a password verifier if (mailInfo.isValidate ()) {authenticator = new MyAuthenticator (mailInfo.getUserName (), mailInfo.getPassword ());} / / construct a session Session sendMailSession = Session.getDefaultInstance (pro,authenticator) to send mail based on mail session attributes and password verifier Try {/ / create an email message based on session Message mailMessage = new MimeMessage (sendMailSession); / / create an email sender address Address from = new InternetAddress (mailInfo.getFromAddress ()); / / set the sender mailMessage.setFrom (from) of the email message / / create the address of the recipient of the message and set it to the message Address to = new InternetAddress (mailInfo.getToAddress ()); / / the Message.RecipientType.TO attribute indicates that the type of the recipient is TO mailMessage.setRecipient (Message.RecipientType.TO,to); / / sets the subject of the message mailMessage.setSubject (mailInfo.getSubject ()) / / set the time when the mail message is sent mailMessage.setSentDate (new Date ()); / / the MiniMultipart class is a container class that contains an object of type MimeBodyPart Multipart mainPart = new MimeMultipart (); / / create a MimeBodyPart BodyPart html = new MimeBodyPart () that contains HTML content / / set HTML content html.setContent (mailInfo.getContent (), "text/html; charset=utf-8"); mainPart.addBodyPart (html); / / set MiniMultipart object to email content mailMessage.setContent (mainPart); / / send email Transport.send (mailMessage); return true } catch (MessagingException ex) {ex.printStackTrace ();} return false;}}

4. Build a password verifier

Package com.example.demo.comment.sendemail;import javax.mail.*;/** * @ author 860118060 * / public class MyAuthenticator extends Authenticator {String userName=null; String password=null; public MyAuthenticator () {} public MyAuthenticator (String username, String password) {this.userName = username; this.password = password;} @ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (userName, password);}}

Now that the preparatory work is complete, let's take a look at how to call

5. Call Demo

Package com.example.demo.comment.sendemail;public class SendEmailDemo {public static void main (String [] args) {/ / this class mainly sets email MailSenderInfo mailInfo = new MailSenderInfo (); mailInfo.setMailServerHost ("smtp.163.com"); mailInfo.setMailServerPort ("25"); mailInfo.setValidate (true); / / sender mailbox mailInfo.setUserName ("xxxxxxxx@163.com") / / sender mailbox password mailInfo.setPassword ("xxxxxxxx"); / / sender mailbox mailInfo.setFromAddress ("xxxxxxxx@163.com"); / / receiver mailbox mailInfo.setToAddress ("xxxxxxxx@qq.com"); / / message title mailInfo.setSubject ("test mailbox to send mail") / / email content mailInfo.setContent ("email content is very rich"); / / send stylistic format SimpleMailSender.sendTextMail (mailInfo); / / send html format SimpleMailSender.sendHtmlMail (mailInfo);}}

There are two formats for sending mail.

Problems and summary

If nothing happens, you should have successfully sent two emails, but just in case, let's analyze which problems will lead to failure.

1.mailInfo.setMailServerHost ("smtp.163.com"); and mailInfo.setFromAddress ("xxxxxxxx@163.com"); these two sentences. That is, if you use a 163smtp server, then the email address must be 163. if not, it will not be sent successfully.

two。 Do not use the email you just registered to send email in the program. If your email is newly registered, do not use "smtp.163.com". Because you can't send it. The newly registered mailbox will not give you this permission, that is, you cannot pass the verification. Use the mailbox you often use, and it takes a long time.

As the sender, the 3.qq mailbox may require authorization verification.

The authorization is as follows:

Find the account in the settings, drop down and follow the prompts to open the authorization, and then use the obtained authorization code as the mailbox password to send the email successfully.

Finally, a common mailbox is attached by the way:

Commonly used mailbox server (SMTP, POP3) address, port

Sina.com:

POP3 server address: pop3.sina.com.cn (port: 110) SMTP server address: smtp.sina.com.cn (port: 25)

SinaVIP:

POP3 server: pop3.vip.sina.com (port: 110) SMTP server: smtp.vip.sina.com (port: 25)

Sohu.com:

POP3 server address: pop3.sohu.com (port: 110) SMTP server address: smtp.sohu.com (port: 25)

126 mailbox:

POP3 server address: pop.126.com (port: 110) SMTP server address: smtp.126.com (port: 25)

139 mailbox:

POP3 server address: POP.139.com (port: 110) SMTP server address: SMTP.139.com (port: 25)

163.com:

POP3 server address: pop.163.com (port: 110) SMTP server address: smtp.163.com (port: 25)

QQ Mail

POP3 server address: pop.qq.com (port: 110)

SMTP server address: smtp.qq.com (port: 25)

QQ enterprise mailbox

POP3 server address: pop.exmail.qq.com (SSL enabled port: 995) SMTP server address: smtp.exmail.qq.com (SSL enabled port: 587Comp465)

Yahoo.com:

POP3 server address: pop.mail.yahoo.com SMTP server address: smtp.mail.yahoo.com

Yahoo.com.cn:

POP3 server address: pop.mail.yahoo.com.cn (port: 995) SMTP server address: smtp.mail.yahoo.com.cn (port: 587)

HotMail

POP3 server address: pop3.live.com (port: 995) SMTP server address: smtp.live.com (port: 587)

Gmail (google.com)

POP3 server address: pop.gmail.com (SSL enabled port: 995) SMTP server address: smtp.gmail.com (SSL enabled port: 587)

263.net:

POP3 server address: pop3.263.net (port: 110) SMTP server address: smtp.263.net (port: 25)

263.net.cn:

POP3 server address: pop.263.net.cn (port: 110) SMTP server address: smtp.263.net.cn (port: 25)

X263.net:

POP3 server address: pop.x263.net (port: 110) SMTP server address: smtp.x263.net (port: 25)

21cn.com:

POP3 server address: pop.21cn.com (port: 110) SMTP server address: smtp.21cn.com (port: 25)

Foxmail:

POP3 server address: POP.foxmail.com (port: 110) SMTP server address: SMTP.foxmail.com (port: 25)

China.com:

POP3 server address: pop.china.com (port: 110) SMTP server address: smtp.china.com (port: 25)

Tom.com:

POP3 server address: pop.tom.com (port: 110) SMTP server address: smtp.tom.com (port: 25)

Etang.com:

POP3 server address: pop.etang.com SMTP server address: smtp.etang.com

This is the end of the article on "how to use java to send email". Thank you for reading! I believe you all have some knowledge about "how to use java to send email". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 229

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report