Using Java to send mail in a small OA system __java

Source: Internet
Author: User

First you need to download a javax.mail package, import the package into the project, and then go directly to the code.

Package com.javaee.email;  
Import java.util.Properties;  
Import javax.mail.Address;  
Import Javax.mail.Message;  
Import javax.mail.Session;  
Import Javax.mail.Transport;  
Import javax.mail.internet.InternetAddress;  
Import Javax.mail.internet.MimeMessage; public class Sendmail_two {public static void main (string[] args) {String message = "This is the first email to be sent using Java
        "; try {//The first parameter is a QQ mailbox server (different mailboxes are different servers), the second parameter is the sender of the message, the third is the sender's password, the fourth parameter is the mail recipient, and the fifth parameter is the message message sent SendMail ("smtp.qq.com  
        "," xxxxx@qq.com "," ******* "," xxxxxxx@qq.com ", message);  
        catch (Exception e) {e.printstacktrace (); }/** * @param args Add by Zxx, Feb 5, 2009 */public static void SendMail (String smtphost  
        , string from,string Password, string to,string message) throws exception{//TODO auto-generated method stub  
        Properties Props = new properties ();  Props.setproperty ("Mail.smtp.auth", "true");
        Props.setproperty ("Mail.transport.protocol", "SMTP");  
        Session session = Session.getinstance (props);  
          
        Session.setdebug (TRUE);  
        msg = new MimeMessage (session);  
        Msg.settext (message);  
      
        Msg.setfrom (New InternetAddress (from));  
        Transport transport = Session.gettransport ();  
        Transport.connect (SMTPHost, From.split ("@") [0], password);  
        Transport.sendmessage (msg, new Address[]{new internetaddress (To)});  
    Transport.close ();    }  
}

Send mail is so simple, it should be noted that different mail is different mail server, Sina's mail server is smtp.sina.com.

Also noteworthy is that if 454 authentication failed appears during the program's Operation , please open SMTP flag first! Error Resolution:

Enter the sender's mailbox, find the settings, find the account, and select Open the POP3/SMTP service in POP3/IMAP/SMTP.


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.