e-mail email __java with Java implementation

Source: Internet
Author: User
Tags getmessage

Today the project has one such demand, the user forgets the password in order to retrieve the password needs to send to the user's mailbox automatically contains the authentication code the mail, therefore the Internet inquires the Java mail the realization. Words don't say much, specifically see realization. the jar package you need

The jar package used in this case is Commons-email-1.4.jar, from Apache.
Download Address:
http://commons.apache.org/proper/commons-email/download_email.cgi

We open the website click to download Commons-email-1.4.jar, it also provides the source code, need the classmate can download. Mail entity class

Package com.yc.wowo.entities;

    /** * Mail Attribute entity * * @author Eden */public class mail{public static final String encodeing = "UTF-8"; Private String host = "smtp.163.com"; Server address private String sender = "xuda27@163.com"; Sender's mailbox private String receiver = "xuda27@qq.com"; Recipient's mailbox Private String name = "Tatsu Software team"; Sender Nickname private String username = "xuda27@163.com"; Account Private String Password = "Client authorization Code"; Client authorization Code password private String subject = "[Tatsu software] Retrieve your account password"; Subject Private String message = "Your verification code is: 8988";
    Information (support HTML) public String GetHost () {return host;
    public void Sethost (String host) {this.host = host;
    Public String Getsender () {return sender;
    } public void Setsender (String sender) {this.sender = sender;
    Public String Getreceiver () {return receiver; } public void Setreceiver (String receiver) {this.receiver = receiveR
    Public String GetName () {return name;
    public void SetName (String name) {this.name = name;
    Public String GetUserName () {return username;
    } public void Setusername (String username) {this.username = username;
    Public String GetPassword () {return password;
    } public void SetPassword (String password) {this.password = password;
    Public String Getsubject () {return subject;
    } public void Setsubject (String subject) {this.subject = subject;
    Public String GetMessage () {return message;
    public void Setmessage (String message) {this.message = message; }

}
Mailutil class Implementation
Import org.apache.commons.mail.EmailException;

Import Org.apache.commons.mail.HtmlEmail;

Import Com.yc.wowo.entities.Mail;
        /** * Mail Delivery Tool Implementation class * * @author Eden/public class Mailutil {public boolean send (mail) {//email
        Htmlemail email = new Htmlemail ();
            try {//Here is the name of the SMTP send server: 163 is as follows: "Smtp.163.com" Email.sethostname (Mail.gethost ());
            The setting of the character encoding set Email.setcharset (mail.encodeing);
            The recipient's mailbox Email.addto (Mail.getreceiver ());
            Sender's Mailbox Email.setfrom (Mail.getsender (), Mail.getname ()); If you need authentication information, set authentication: username-password.
            Email.setauthentication (Mail.getusername (), Mail.getpassword ()) for the sender's registered name and password on the mail server respectively;
            The subject of the message to be sent Email.setsubject (Mail.getsubject ());
            The information to be sent, because of the use of Htmlemail, you can use HTML tags in the content of the message email.setmsg (Mail.getmessage ());
            Send Email.send (); return True;
            catch (Emailexception e) {e.printstacktrace ();
        return false; }
    }

}
Test

Test with the Main method

public static void Main (string[] args {  
        mail mail = new Mail ();   
        New Mailutil (). Send (mail);  

Results:

errors encountered during

Midway encountered a org.apache.commons.mail.EmailException:Sending the email to the following server failed:smtp.163.com:25 error.

The internet looked up the error. Reason: The port is wrong, sendemail.sethostname ("smtp.163.com"); is not open authentication, is the password (authorization code) wrong

I used the password to use the mailbox login password, so reported the wrong, should use authorization code.

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.