| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 579 人关注过本帖
标题:JavaMail问题
只看楼主 加入收藏
zhanglinkai
Rank: 1
等 级:新手上路
帖 子:87
专家分:0
注 册:2006-11-25
收藏
 问题点数:0 回复次数:3 
JavaMail问题

index.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java Mail</title>
</head>
<body>
<div align="center">
<br><br>
<font color="blue" style="楷书"><h1>利用JavaMail来发送电子邮件</h1></font>
<br>
<br>
<form action="JavaMail.jsp" name="form1">
<p>寄信人: <input type="text" name="From" value="" size="30" /></p>
<p>收件人: <input type="text" name="To" value="" size="30" /></p>
<p>主 &nbsp;&nbsp;题: <input type="text" name="Subject" value="" size="30" /></p>
<p>内 &nbsp;容:</p>
<p><textarea name="Message" rows="8" cols="40">
</textarea></p>
<p><input type="submit" value="发送"/> &nbsp;&nbsp;<input type="reset" value="清除" /></p>
</form>
</div>
</body>
</html>
JavaMail.jsp
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.activation.*" %>
<%@ page import="java.io.*,java.util.*" %>
<%--
The taglib directive below imports the JSTL library. If you uncomment it,
you must also add the JSTL library to the project. The Add Library... action
on Libraries node in Projects view can be used to add the JSTL 1.1 library.
--%>
<%--
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
--%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Java Mail</title>
</head>
<body>
<center>
<h2>利用JavaMail来传送电子邮件</h2>
<%
request.setCharacterEncoding("GB2312");
InternetAddress[] address=null;
String mailserver="smtp.126.com";
String From=request.getParameter("From");
String To=request.getParameter("To");
String Subject=request.getParameter("Subject");
String message=request.getParameter("Message");
boolean sessionDebug=false;
try{
//设定服务器和传输协议
java.util.Properties props=System.getProperties();
props.put("mail.smtp.host",mailserver);
props.put("mail.transport.protocol","smtp");

//产生新的Session
javax.mail.Session mailSession=javax.mail.Session.getInstance(props,null);
mailSession.setDebug(sessionDebug);
javax.mail.Message msg=new MimeMessage(mailSession);

//设定传送邮件的发件人
msg.setFrom(new InternetAddress(From));
address=InternetAddress.parse(To,false);
msg.setRecipients(javax.mail.Message.RecipientType.TO,address);
msg.setSubject(Subject);

msg.setSentDate(new Date());
msg.setText(message);
Transport transport =mailSession.getTransport("smtp");
transport.send(msg);
out.println("邮件已顺利传送");}
catch(MessagingException mex){
mex.printStackTrace();}
%>
</center>


<%--
This example uses JSTL, uncomment the taglib directive above.
To test, display the page like this: index.jsp?sayHello=true&name=Murphy
--%>
<%--
<c:if test="${param.sayHello}">
<!-- Let's welcome the user ${param.name} -->
Hello ${param.name}!
</c:if>
--%>

</body>
</html>
用JAVAMAIL作了个邮件传送的例子却怎么也实现不了,填写了信息后到JavaMail.jsp就什么也不显示了也没有错误提示。我用的126的油箱。是不是写的有问题请高手指教以下。 本人万分感激!!!谢谢

搜索更多相关主题的帖子: JavaMail 
2007-04-29 22:26
phb1353
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2007-5-4
收藏
得分:0 

这里有一个文件,具体的JSP你自已写好了。这个程序可以运行,是我用过的,OK,还有问题可以联系我QQ183413050

2007-05-04 19:21
phb1353
Rank: 1
等 级:新手上路
威 望:1
帖 子:11
专家分:0
注 册:2007-5-4
收藏
得分:0 

try {
java.util.Properties p = new Properties();
p.put("mail.smtp.host", "smtp.163.com");
// p.put("mail.smtp.port", "25");
// p.put("mail.transport.protocol", "smtp");
myauth mytu = new myauth();
p.put("mail.smtp.auth", "true");
Session session = Session.getInstance(p, mytu);
session.setDebug(true);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("邮箱地址"));
message.setSubject("subject");
// message.setText("什么也没有");
message.setRecipients(Message.RecipientType.TO,
javax.mail.internet.
InternetAddress.parse(
"邮箱地址")
);
// 附件1
MimeBodyPart mimebodypart = new MimeBodyPart();
mimebodypart.setText("这是附件消息");//显示主题

MimeBodyPart mimebodypart1 = new MimeBodyPart();
mimebodypart1.setText("这是第二个附件");
DataSource datasource = new FileDataSource("E:\\爱好.jpg");
mimebodypart1.setDataHandler(new DataHandler(datasource));
mimebodypart1.setFileName("E:\\爱好.jpg");//显示附件1的名字

MimeBodyPart mimebodypart2 = new MimeBodyPart();
mimebodypart2.setText("这是第三个附件");
DataSource datasource1 = new FileDataSource("E:\\a.txt");
mimebodypart2.setDataHandler(new DataHandler(datasource1));
mimebodypart2.setFileName("文本");//显示附件2的名字

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(mimebodypart);
multipart.addBodyPart(mimebodypart1);
multipart.addBodyPart(mimebodypart2);

message.setContent(multipart);//
Transport.send(message);
------------------------------------------------------------------------
package mymail;


import javax.mail.PasswordAuthentication;
import javax.mail.Authenticator;

public class myauth extends Authenticator {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication a = new PasswordAuthentication(
"用户名", "密码");
return a;
}
}

--------------------------------------------------------HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<form name="form1" method="post" action="http://localhost:8080/WebModule1/servlet1">
<table width="381" border="1" align="center">
<tr>
<td colspan="2" align="center">请输入所有信息以发送邮件</td>
</tr>
<tr>
<td>邮件服务器:</td>
<td>
<input type="text" name="text1">
</td>
</tr>
<tr>
<td>收件人</td>
<td>
<input type="text" name="text2" maxlength="200" >
</td>
</tr>
<tr>
<td>发件人</td>
<td>
<input type="text" name="text3" maxlength="200">
</td>
</tr>
<tr>
<td>主题:</td>
<td>
<input type="text" name="text4">
</td>
</tr>
<tr>
<td>消息:</td>
<td>
<textarea name="text5" cols="38" rows="5" id="text5"> </textarea>
</td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="submit" name="Submit" value="提交">
<input type="reset" name="reset" value="reset">
</td>
</tr>
</table>
</form>
</body>
</html>

2007-05-04 19:22
zplove
Rank: 5Rank: 5
等 级:贵宾
威 望:15
帖 子:783
专家分:0
注 册:2006-7-30
收藏
得分:0 

相信自己的没错了
2007-05-12 16:21
快速回复:JavaMail问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017519 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved