如果smtp服务器要求身份验证,怎样使用JSP页面发信?(50分)

  • 主题发起人 主题发起人 CrazyHunter
  • 开始时间 开始时间
C

CrazyHunter

Unregistered / Unconfirmed
GUEST, unregistred user!
如果smtp服务器要求身份验证,怎样使用JSP页面发信?
先在这里多谢各位高手!
 
利用一下代码可以在JSP页面中发信
<HTML>
<BODY>
<FORM action="sendmail.jsp" method="post">
<TABLE align="center">
<TR>
<TD width="50%">
To:<BR><INPUT name="to" size="25">
</TD>
<TD width="50%">
From:<BR><INPUT name="from" size="25">
</TD>
</TR>
<TR>
<TD colspan="2">
Subject:<BR><INPUT name="subject" size="50">
</TD>
</TR>
<TR>
<TD colspan="2">
<P>Message:<BR><TEXTAREA name="text" rows=25 cols=85></TEXTAREA></P>
</TD>
</TR>
</TABLE>
<INPUT type="submit" name="cb_submit" value=" Send ">
<INPUT type="reset" name="cb_reset" value=" Clear ">
</FORM>
</BODY>
</HTML>

///sendmail.jsp/////
<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;

sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "smtp.jspinsider.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
但是我在WIN2000 SERVER+RESIN的环境下进行调试,出现以下错误:
500 Servlet Exception
/class/sendmail.jsp:11: Package javax.mail not found in import.
import javax.mail.*;
^
/class/sendmail.jsp:12: Package javax.mail.internet not found in import.
import javax.mail.internet.*;
^
/class/sendmail.jsp:13: Package javax.activation not found in import.
import javax.activation.*;
^
3 errors
不知为什么?而且我也下载了JAVAMAIL和JAF,放到了JDK的LIB中。
请高手们给与指教!!!!!
 
u need to put the javamail and jaf package into web-inf/lib.
 
谢谢你,曹晓钢!!!可以知道你的email么?以后有问题向你请教!!!
 
不用谢。您看来对javamail有所熟悉了,看看能不能帮助解决这个帖子的问题呢?
 
好的,我尽力试试!!!
 
简单说一下
关键的有这么几步
1、prop.put("mail.smtp.auth", "true");
2、Transport smtp = s.getTransport("smtp");
smtp.connect(smtpServer, SMTPSERVERPORT, userName, password);
smtp.sendMessage(msg, InternetAddress.parse(mailTo,false));
 
代码如下:
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class sendMail
{
public static void main(String args[]) throws Exception
{
String host = "smtp.sina.com.cn";
String from = "javamail@sina.com";
String to = "javamail@china.com";
String username = "javamail";
String password = "password";
// Get system properties
// Properties props = System.getProperties();
很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
//这样才能通过验证
// Get session
Session session = Session.getDefaultInstance(props);
// watch the mail commands go by to the mail server
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
 
多人接受答案了。
 
后退
顶部