In the new project, the function of automatically sending data collection results to the specified mailbox is required. Because Java is not very familiar with it, a simple email sending function is provided after some online searches and references, take it as a java entry-level study. For preliminary verification, emails can be sent normally.
After the code is added, because the selected JDK version (jdk-7u5-windows-i586) is installed and the accompanying JRE library does not contain mail. jar, the program runs wrong. After the mail. jar file is downloaded, click Add External JARs under project --> properties --> java Build Path in eclipse to specify the storage Path.
[Java]
Import java. util. Date;
Import java. util. Properties;
Import javax. mail. Address;
Import javax. mail. Authenticator;
Import javax. mail. Message;
Import javax. mail. PasswordAuthentication;
Import javax. mail. Session;
Import javax. mail. Transport;
Import javax. mail. internet. InternetAddress;
Import javax. mail. internet. MimeMessage;
Public class mail_test
{
// Mail server
Private String host = "smtp.126.com ";
// Mail account
Private String username = "xxxxxxx ";
// Password
Private String password = "xxxxxxx ";
Private String mail_head_name = "Head of test mail ";
Private String mail_head_value = "Head of test mail ";
Private String mail_to = www.2cto.com;
Private String mail_from = "www.2cto.com ";
Private String mail_subject = "subject of test mail ";
Private String mail_body = "content of test mail ";
Private String personalName = "test_mail! ";
Public mail_test ()
{
}
Public void send () throws Exception
{
Try
{
Properties props = new Properties (); // obtain the system environment
Authenticator auth = new Email_Autherticator (); // Authentication
Props. put ("mail. smtp. host", host );
Props. put ("mail. smtp. auth", "true ");
Session session = Session. getDefaultInstance (props, auth );
// Set the session to communicate with the email server.
MimeMessage message = new MimeMessage (session );
Message. setSubject (mail_subject );
Message. setText (mail_body); // you can specify the mail body.
Message. setHeader (mail_head_name, mail_head_value); // you can specify the mail title.
Message. setSentDate (new Date (); // you can specify the mail sending Date.
Address address = new InternetAddress (mail_from, personalName );
Message. setFrom (address); // you can specify the sender's address.
Address toAddress = new InternetAddress (mail_to); // you can specify the email receiver Address.
Message. addRecipient (Message. RecipientType. TO, toAddress );
Transport. send (message); // send an email
System. out. println ("send over! ");
} Catch (Exception ex)
{
Ex. printStackTrace ();
Throw new Exception (ex. getMessage ());
}
}
/*
* Used for server-to-user authentication
*/
Public class Email_Autherticator extends Authenticator
{
Public Email_Autherticator ()
{
Super ();
}
Public Email_Autherticator (String user, String pwd)
{
Super ();
Username = user;
Password = pwd;
}
Public PasswordAuthentication getPasswordAuthentication ()
{
Return new PasswordAuthentication (username, password );
}
}
Public static void main (String [] args)
{
Mail_test sendmail = new mail_test ();
Try
{
Sendmail. send ();
} Catch (Exception ex)
{
}
}
}
After the program is compiled and run, the "send over" command is displayed in the eclipse console! Indicates that the test email has been sent successfully.
Author: saloon_yuan