Java code for sending emails

Source: Internet
Author: User

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

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.