邮件发送简单例子-bean文件
点击次数:29 次 发布日期:2008-11-26 15:52:00 作者:源代码网
|
源代码网推荐 源代码网推荐 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 with 源代码网推荐 // valid information. 源代码网推荐 // host - must be a valid smtp server that you currently have 源代码网推荐 // access to. 源代码网推荐 // to - whoever is going to get your email 源代码网推荐 // from - whoever you want to be. Just remember that many 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 the" 源代码网推荐 + " JavaMail API. I 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 the 源代码网推荐 // required information. 源代码网推荐 Message msg = new MimeMessage(session); 源代码网推荐 源代码网推荐 msg.setFrom(new InternetAddress(from)); 源代码网推荐 InternetAddress[] address = {new InternetAddress(to)}; 源代码网推荐 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(); 源代码网推荐 } 源代码网推荐 } 源代码网推荐 } 源代码网推荐 源代码网供稿. |
