⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0184.htm

📁 JspServlet教程专栏 对javaservlet讲述的非常详细
💻 HTM
字号:
<html>

<head>
<title>新时代软件教程:操作系统 主页制作 服务器 设计软件 网络技术 编程语言 文字编辑</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style>
<!--
body, table {font-size: 9pt; font-family: 宋体}
a {text-decoration:none}
a:hover {color: red;text-decoration:underline}
.1  {background-color: rgb(245,245,245)}
-->
</style>
</head>
<p align="center"><script src="../../1.js"></script></a>
<p align="center"><big><strong>邮件发送简单例子-bean文件</strong></big></p>
<div align="right">---摘自互联网</div>

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

  </table>
<p align="center"><script src="../../2.js"></script></a>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -