1. Download the supported jar packages from my resource library,
2. Test code:
[Java]
Package com. email. test;
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 TestEmail {
Public void SendEmailTest (){
Properties props = new Properties ();
Props. put ("mail. smtp. host", "smtp.qq.com ");
Props. put ("mail. smtp. auth", "true"); // allow smtp Verification
Session sendMailSession = Session. getInstance (props, null );
Try {
Transport transport = sendMailSession. getTransport ("smtp ");
// Connect to your QQ account. Note that the user name and password must be correct; otherwise, the permission is not obtained.
Transport. connect ("smtp.qq.com", "3693352@qq.com ","****");
Message newMessage = new MimeMessage (sendMailSession );
// Set the mail subject
String mail_subject = "Change email sender test ";
NewMessage. setSubject (mail_subject );
// Set the sender address
String strFrom = "517122199@qq.com ";
StrFrom = new String (strFrom. getBytes (), "iso-8859-1 ");
NewMessage. setFrom (new InternetAddress (strFrom ));
// Address addressFrom [] = {new
// InternetAddress ("617334015@qq.com"), new
// InternetAddress ("goodnight0002@163.com ")};
// Change the sender address
// NewMessage. addFrom (addressFrom );
// Set the recipient address
Address addressTo [] = {new InternetAddress ("306220165@qq.com ")};
NewMessage. setRecipients (Message. RecipientType. TO, addressTo );
// Set the mail body
NewMessage. setSentDate (new java. util. Date ());
String mail_text = "java implements mail sending! ";
NewMessage. setText (mail_text );
NewMessage. saveChanges (); // Save the sending information
Transport. sendMessage (newMessage, newMessage
. GetRecipients (Message. RecipientType. TO); // send an email
Transport. close ();
// Transport. send (newMessage );
System. out. println ("sent successfully! ");
} Catch (Exception e ){
System. out. println ("failed to send! ");
System. out. println (e );
}
}
Public static void main (String args []) throws Exception {
TestEmail SEmail = new TestEmail ();
SEmail. SendEmailTest (); www.2cto.com
}
}
3. Note:
1) when sending the email for the first time, you must set your mailbox to allow smtp verification. Otherwise, the email cannot be sent.
2) enter the correct email permission, email name, and password. Otherwise, an exception is thrown.
Author: HarderXin