当前位置: 首页 > 图文教程 > 网络编程 > JSP > 邮件发送简单例子-bean文件

JSP
java/jsp中 中文问题详解
Java代码编写的一般性指导
如何成为一个优秀的jsp程序员
JSP开发入门(二)----JSP语法的基本原理
JSP开发入门(三)--JSP与JavaBean
JSP开发入门(四)--JSP的内部对象
JSP开发入门(五)--JSP其他相关资源
JSP开发入门(一)--安装好你的机器来使用JSP
页面自动刷新,不用按回车键来提交数据!
用连接池提高Servlet访问数据库的效率(2)
JDBCTM 指南:入门7-CallableStatement
JDBCTM 指南:入门3 - DriverManager
JDBCTM 指南:入门6-PreparedStatement
JDBCTM 指南:入门5 - ResultSet
JDBCTM 指南:入门4 - Statement
JDBCTM 指南:入门
JDBCTM 指南:入门2 - 连接
用连接池提高Servlet访问数据库的效率(1)
使用JavaBean创建您的网上日历本(1)
使用JavaBean创建您的网上日历本(2)

JSP 中的 邮件发送简单例子-bean文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 54 ::
收藏到网摘: n/a

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 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 = "[email protected]";
String from = "[email protected]";
String subject = "JSP Rules!";
String messageText = "I am sending a message using the"
+ " 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 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();
}
}
}