Simple Example of email sending-bean File

Source: Internet
Author: User

SimpleSendMessage. java

Import java. util .*;

Import javax. mail .*;
Import javax. mail. internet .*;
Import javax. activation .*;

Public class SimpleSendMessage {

Public static void main (String [] args ){

// Collect the necessary information to send a simple message
// Make sure to replace the values for host, to, and from
// Valid information.
// Host-must be a valid smtp server that you currently have
// Access.
// To-whoever is going to get your email
// From-whoever you want to be. Just remember that allow smtp
// Servers will validate the domain of the from address
// Before allowing the mail to be sent.
String host = "server.myhost.com ";
String to = "YourFriend@somewhere.com ";
String from = "MeMeMe@myhost.com ";
String subject = "JSP Rules! ";
String messageText = "I am sending a message using"
+ "JavaMail API. \ nI can include any text that I want .";
Boolean sessionDebug = false;

// Create some properties and get the default Session.
Properties props = System. getProperties ();
Props. put ("mail. host", host );
Props. put ("mail. transport. protocol", "smtp ");

Session session = Session. getDefaultInstance (props, null );

// Set debug on the Session so we can see what is going on
// Passing false will not echo debug info, and passing true
// Will.
Session. setDebug (sessionDebug );

Try {

// Instantiate a new MimeMessage and fill it with
// Required information.
Message msg = new MimeMessage (session );

Msg. setFrom (new InternetAddress (from ));
InternetAddress [] address = {new InternetAddress ()};
Msg. setRecipients (Message. RecipientType. TO, address );
Msg. setSubject (subject );
Msg. setSentDate (new Date ());
Msg. setText (messageText );

// Hand the message to the default transport service
// For delivery.
Transport. send (msg );
}
Catch (MessagingException mex ){

Mex. printStackTrace ();
}
}
}

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.