| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 948 人关注过本帖
标题:JSP 发邮件出问题!
只看楼主 加入收藏
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
 问题点数:0 回复次数:5 
JSP 发邮件出问题!

代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'Login.jsp' starting page</title>
</head>

<body>
<form action="sendmail.jsp" method=post>
<table>
<tr><td>收件人:<input type=text name=to></td></tr>
<tr><td>发件人:<input type=text name=from></td></tr>
<tr><td>主题:<input type=text name=subject></td></tr>
<tr><td>内容;<textarea name=content></textarea>
<tr><td><input type=submit value=send ><input type=reset value=aaa></td></tr>
</table>
</form>
</body>
</html>

处理页面:
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="javax.mail.*, javax.mail.internet.*, javax.activation.*, java.util.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">

<title>My JSP 'sendmail.jsp' starting page</title>
</head>
<% request.setCharacterEncoding("gb2312"); %>
<body>
<%
try{
Properties props=System.getProperties();
Session sendmailSession;
Transport transport;

sendmailSession=Session.getDefaultInstance(props,null);
props.put("mail.smtp.host","smtp.sina.com.cn");
//props.put("mail.smtp.prot","25");
Message newMessage=new MimeMessage(sendmailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("content"));
transport =sendmailSession.getTransport("smtp");
transport.send(newMessage);

%>
<p>你的邮件已发送,请返回。</p>

<%
}
catch(MessagingException e){
out.print("错误原因是:"+e);
}
%>
</body>
</html>

错误原因是:javax.mail.SendFailedException: Sending failed; nested exception is: class javax.mail.SendFailedException: Invalid Addresses; nested exception is: class javax.mail.SendFailedException: 554 : Client host rejected: Access denied

是不是我没装SMTP服务器的问题啊?要装这个服务器吗?哪位能帮我改改吗?


搜索更多相关主题的帖子: JSP 邮件 
2007-11-16 16:57
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 
收件人我写的是:xing@sina.com
2007-11-16 16:58
黄袖标
Rank: 4
等 级:贵宾
威 望:13
帖 子:676
专家分:0
注 册:2007-3-22
收藏
得分:0 

Properties props = new Properties();
SendMailAuthenticator au = new SendMailAuthenticator();
String user = "xxx";
String pwd = "xxx";
au.check(user, pwd);
//假设你用这个email
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props, au);

String smtp="smtp.163.com";
String username=user;
String userEmail=user+"@"+smtp.substring(5, smtp.length());
System.out.println("userEmail="+userEmail);
MimeMessage message = new MimeMessage(session);
//处理中文乱码
String form="xxx";
form=new String(form.getBytes(),"8859_1");
message.setFrom(new InternetAddress("("+form+")"+userEmail));

InternetAddress[] address = { new InternetAddress(
"xx@xxx.com") };

message.setRecipients(Message.RecipientType.TO, address);

message.setSubject("测试邮件");

message.setSentDate(new Date());

message.setText("这是一个测试邮件: " + getRandKeys(6));

Transport.send(message);


大致就这样


我胡汉三又回来啦!物是人非啊,只有静夜思大大还在。
2007-11-16 19:08
HB515560244
Rank: 1
等 级:新手上路
帖 子:48
专家分:0
注 册:2007-10-9
收藏
得分:0 

那 user和pwd怎么得到啊,

props.put("mail.smtp.host", "smtp.163.com");//这个有什么用啊?应该怎么设置啊,如果我是发给新浪的,或其他的呢???

谢谢啊,在线..

2007-11-16 22:16
黄袖标
Rank: 4
等 级:贵宾
威 望:13
帖 子:676
专家分:0
注 册:2007-3-22
收藏
得分:0 
user pwd 你不是问我吧,你的email啊,ld

props.put("mail.smtp.host", "smtp.163.com");//这个有什么用啊?应该怎么设置啊,如果我是发给新浪的,或其他的呢???

发给谁就写谁咯。

我胡汉三又回来啦!物是人非啊,只有静夜思大大还在。
2007-11-19 08:55
yanwu1515
Rank: 1
等 级:新手上路
帖 子:29
专家分:0
注 册:2007-11-3
收藏
得分:0 

我这里有个类,你可以试一下,是我们做项目时用到的:
package eshop.bean;
import java.util.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class MailFacade {

public static boolean sendMail(String strSMTP, String strTitle,
String strContent, String strUserName, String strPassword,
String strAddress, String strTarddress){
boolean bolReturnValue = false;
try{
Properties mailPros = new Properties();
mailPros.put("mail.smtp.host",strSMTP);
mailPros.put("mail.smtp.auth", "true");

Session mailSession = Session.getDefaultInstance(mailPros);

mailSession.setDebug(true);

MimeMessage message = new MimeMessage(mailSession);

//设置发信人
message.setFrom(new InternetAddress(strAddress));

//收信人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(strTarddress));

// 邮件标题
message.setSubject(strTitle);

// 邮件内容
message.setText(strContent);

// 保存
message.saveChanges();

Transport transport = mailSession.getTransport("smtp");
transport.connect(strSMTP,strUserName,strPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

bolReturnValue=true;
}catch(Exception e){
bolReturnValue=false;
}
finally{
return bolReturnValue;
}
}


public static void main(String args[]) throws Exception {
Properties mailPros = new Properties();
mailPros.put("mail.smtp.host", "smtp.163.com");
mailPros.put("mail.smtp.auth", "true");

Session mailSession = Session.getDefaultInstance(mailPros);

mailSession.setDebug(true);

MimeMessage message = new MimeMessage(mailSession);

// 设置发信人
message.setFrom(new InternetAddress("yanwu401909@163.com"));

// 收信人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
"yanwu401909@163.com"));

// 邮件标题
message.setSubject("你好");

// 邮件内容
message.setText("你好,TestTestTestTestTestTestTest");

// 保存
message.saveChanges();

Transport transport = mailSession.getTransport("smtp");
transport.connect("smtp.163.com", "yanwu401909", "(这里是自己的密码)");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
System.out.println("发送ok!");
}
}

2007-11-19 09:07
快速回复:JSP 发邮件出问题!
数据加载中...
 
   



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

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