SimpleSendMessage.javamyNLinux联盟 myNLinux联盟 import java.util.*;myNLinux联盟 myNLinux联盟 import javax.mail.*;myNLinux联盟 import javax.mail.internet.*;myNLinux联盟 import javax.activation.*;myNLinux联盟 myNLinux联盟 public class SimpleSendMessage {myNLinux联盟 myNLinux联盟 public static void main(String[] args) {myNLinux联盟 myNLinux联盟 // Collect the necessary information to send a simple messagemyNLinux联盟 // Make sure to replace the values for host, to, and from withmyNLinux联盟 // valid information.myNLinux联盟 // host - must be a valid smtp server that you currently havemyNLinux联盟 // access to.myNLinux联盟 // to - whoever is going to get your emailmyNLinux联盟 // from - whoever you want to be. Just remember that many smtpmyNLinux联盟 // servers will validate the domain of the from addressmyNLinux联盟 // before allowing the mail to be sent.myNLinux联盟 String host = "server.myhost.com";myNLinux联盟 String to = "YourFriend@somewhere.com";myNLinux联盟 String from = "MeMeMe@myhost.com";myNLinux联盟 String subject = "JSP Rules!";myNLinux联盟 String messageText = "I am sending a message using the"myNLinux联盟 + " JavaMail API.\nI can include any text that I want.";myNLinux联盟 boolean sessionDebug = false;myNLinux联盟 myNLinux联盟 // Create some properties and get the default Session.myNLinux联盟 Properties props = System.getProperties();myNLinux联盟 props.put("mail.host", host);myNLinux联盟 props.put("mail.transport.protocol", "smtp");myNLinux联盟 myNLinux联盟 Session session = Session.getDefaultInstance(props, null);myNLinux联盟 myNLinux联盟 // Set debug on the Session so we can see what is going onmyNLinux联盟 // Passing false will not echo debug info, and passing truemyNLinux联盟 // will.myNLinux联盟 session.setDebug(sessionDebug);myNLinux联盟 myNLinux联盟 try {myNLinux联盟 myNLinux联盟 // Instantiate a new MimeMessage and fill it with themyNLinux联盟 // required information.myNLinux联盟 Message msg = new MimeMessage(session);myNLinux联盟 myNLinux联盟 msg.setFrom(new InternetAddress(from));myNLinux联盟 InternetAddress[] address = {new InternetAddress(to)};myNLinux联盟 msg.setRecipients(Message.RecipientType.TO, address);myNLinux联盟 msg.setSubject(subject);myNLinux联盟 msg.setSentDate(new Date());myNLinux联盟 msg.setText(messageText);myNLinux联盟 myNLinux联盟 // Hand the message to the default transport servicemyNLinux联盟 // for delivery.myNLinux联盟 Transport.send(msg);myNLinux联盟 }myNLinux联盟 catch (MessagingException mex) {myNLinux联盟 myNLinux联盟 mex.printStackTrace();myNLinux联盟 }myNLinux联盟 }myNLinux联盟 } myNLinux联盟