Recently I want to write an automatic mailbox verification function. I read a lot on the Internet and wrote a lot of questions about myself. I recorded them for reference and wrote them online.

Source: Internet
Author: User

Recently I want to write an automatic mailbox verification function. I read a lot on the Internet and wrote a lot of questions about myself. I recorded them for reference and wrote them online.

Package com. app. tools; 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 SendEmail {// public static final String HOST = "mx3.qq.com"; public static final String HOST = "smtp.163.com"; public static final String PROTOCOL = "smtp "; public static final int PORT = 25;/* 465 this is the qq port */public static final String FROM = "xxxxx@163.com "; // sender's email public static final String PWD = "xxxxxxx"; // sender's password private static Session getSession () {Properties props = new Properties (); props. put ("mail. smtp. host ", HOST); // set the server address props. put ("mail. store. protocol ", PROTOCOL); // sets the protocol props. put ("mail. smtp. port ", PORT); // set the port props. put ("mail. smtp. auth "," true "); Authenticator authenticator = new Authenticator () {@ Override protected PasswordAuthentication getPasswordAuthentication () {return new PasswordAuthentication (FROM, PWD );}}; session session = Session. getDefaultInstance (props, authenticator); return session;} public static void send (String toEmail, String content) {Session session = getSession (); try {System. out. println ("-- send --" + content); // Instantiate a message Message msg = new MimeMessage (session); // Set message attributes msg. setFrom (new InternetAddress (FROM); InternetAddress [] address = {new InternetAddress (toEmail)}; msg. setRecipients (Message. recipientType. TO, address); msg. setSubject ("Account Activation email"); msg. setSentDate (new Date (); msg. setContent (content, "text/html; charset = UTF-8"); // Send the message Transport. send (msg);} catch (MessagingException mex) {mex. printStackTrace () ;}} public static void main (String [] args) {new SendEmail (). send ("xxxxx@qq.com", "dsfsdf"); System. out. println ("success ");}}
Note the following:

1. Whether authentication is enabled. You must enable authentication for both QQ and 163 mailboxes.

2. javax. mail. MessagingException: cocould not connect to SMTP host: smtp.163.com, port: 25; // connection timeout

Solution reference: Add quotation marks ("true") to this attribute. An exception occurred! Note: If this attribute is not available, the Required Authentication exception may be thrown. This exception is generally caused by 1 or 2.

3. When setting attributes, be sure to find out the address and port of the mailbox. Javax. mail. AuthenticationFailedException

Qq mail port: mx3.qq.com port: 465 163 mailbox: smtp.163.com port: 25 This is used for smtp protocol when sending mail. The pop3 protocol is used for receiving emails, which is different. Please search for this by yourself

4,

This exception occurs when you use QQ mail to send emails. Check it. The official saying is that the message is sent too frequently, so it is locked! It has not been solved yet. Try again tomorrow to see if it will succeed (unsuccessful)

 

The above are several common exceptions. After the exception is solved, other exceptions can be solved based on the specific information thrown!

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.