JavaMail Usage Experience

Source: Internet
Author: User

Want to do a Java Send mail small function.

Use of Maven-built projects.
<!--Https://mvnrepository.com/artifact/javax.mail/mail--
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
If you want to introduce a jar package manually, you need to introduce two jar packages; here's My Code:
public class Mail {
Sender Information
public static String Myemailaccount = ".... @163.com "; Mailbox
public static String Myemailpassword = "..... "; Password

Send mailbox information
The SMTP server address of the sender's mailbox must be accurate, different mailbox server addresses are generally different,
public static String Toemailsmtphost = "smtp.163.com"; NetEase Email
public static String Toemailaccount = "..... @163.com "; Recipient mailbox


Main function
public static void Main (string[] args) throws Exception {

1. Create a parameter configuration to connect to the mail server
Properties Props = new properties ();
Props.setproperty ("Mail.transport.protocol", "SMTP");//Declaration of Use Protocol
Props.setproperty ("Mail.smtp.host", toemailsmtphost);//Determine the server address of the mailbox
Props.setproperty ("Mail.smtp.auth", "true");//request validation required

/**
* PS Some mailbox servers require SSL security authentication for SMTP connections, and for increased security, mailbox support
* SSL connection, can also open itself, if the server is unable to connect to the right-click Server, carefully check the console print log
* **/

/**
* The following code is an SMTP service that opens an SSL connection.
* The port of the SMTP server (the port of non-SSL connection generally defaults to 25, can not be added, if SSL connection is turned on,
* Need to change to the corresponding mailbox of the SMTP server port, specifically to see the corresponding mailbox service Help, QQ mailbox SMTP (SLL) port is 465 or 587, other mailboxes to see for themselves)
*

Final String smtpport = "465";
Props.setproperty ("Mail.smtp.port", Smtpport);
Props.setproperty ("Mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
Props.setproperty ("Mail.smtp.socketFactory.fallback", "false");
Props.setproperty ("Mail.smtp.socketFactory.port", Smtpport);
**/

2. Create a Session object based on the configuration to interact with the mail server
Session session = Session.getdefaultinstance (props);
Session.setdebug (TRUE);

3. Create a message
MimeMessage message = Createmimemessage (Session,myemailaccount,toemailaccount);

4. Get mail Transfer objects according to session
Transport Transport = Session.gettransport ();

/**
* 5. Use the email account and password to connect to the right-click Server, where the certified mailbox must be the same as the sender mailbox in the message, or error
* PS--01: The key to success or failure in this sentence, if the connection server fails, it will output the log of the corresponding failure reason in the console
* Carefully review the cause of the failure, some mailbox servers will return an error code or view the wrong type of connection, according to the given error type of the corresponding mail server
* See specific failure reasons on the Help website
*
* PS--02: The reason for the connection failure usually has a few points, check the code carefully:
* 1. Mailbox does not have SMTP service turned on
* 2. Email password error, for example, some mailboxes have a separate password;
* 3. The mailbox server requires an SSL secure connection to be used;
* 4. The request is too frequent or other reasons, the mail server refused service;
*
*
* ps--03: Look at log carefully, read log carefully, read the log, the cause of the error is described in log.
*
*/
Transport.connect (Myemailaccount,myemailpassword);

6. Send a message, send all the pickup addresses, message.getallrecipients () Get all the recipients that were added in the creation of the mail object, cc person, secret sender
Transport.sendmessage (Message,message.getallrecipients ());

7. Close connection
Transport.close ();
}

/**
* Create a simple message that contains only text
* Sessions @param session and server interaction
* @param sendMail Sender Mailbox
* @param toemailaccount Recipient mailbox
*
* **/

public static MimeMessage Createmimemessage (Session session, String SendMail,
String Toemailaccount) throws Exception {
1. Create a message
MimeMessage message = new MimeMessage (session);

2. From Sender
Message.setfrom (New InternetAddress (SendMail, "haha", "UTF-8"));

3 to recipients (can add multiple recipients, CC, BCC)
Message.setrecipient (mimemessage.recipienttype.to,new internetaddress (Toemailaccount,
"Beauty", "UTF-8"));

4.Subject: Message subject
Message.setsubject ("Come, chat", "UTF-8");

5. Content: Message body (HTML tags can be used)
Message.setcontent ("He Bingling beauty, no rain Tonight Ha", "text/html;charset=utf-8");

6. Set the sender time
Message.setsentdate (New Date ());

7. Save Settings
Message.savechanges ();

return message;
}
}
Errors generated during: 1. Error code 550:

Steps

1. Log in to your own 163 mailbox, and note that you want to use the email address set up in your app to log in.

2. On the main menu bar, in the Settings submenu, select the Pop3/smtp/iamp menu.

3. If the "Client authorization password" is not turned on, choose to turn it on. When turned on, it will generate an app authorization password for you. To use this generated password as the Java Mail API to send mail authentication password, so that the 500 password can be lifted, if the mailbox login password to set this authentication password, you will encounter 500 errors. After you turn on the "Client Licensing password" feature:

Setting the above will result in a 535 error code: For the following reasons:
Sender Information
public static String Myemailaccount = "...; Mailbox
public static String Myemailpassword = "...; Password
The password here is not the email login password you set, but the client authorization password.

JavaMail Usage Experience

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.