当前位置:编程学习 > JAVA >>

javaweb中使用JavaMail报错

第一次用报错,百度找不到,第一次发帖,求求各位指教!
DEBUG: setDebug: JavaMail version 1.4.5
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
我写了一个testMail.html提交相关数据到sendMail.jsp页面,其中在sendMail中调用java方法。具体代码如下:
testMail.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>发送文本型文件</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  
  <body>
  <h2>发送文本型邮件</h2>
  <form action="sendMail.jsp" method="post" name="form1">
  SMTP服务器:<input type="text" id="STMPHost" name="STMPHost"><br>
  登陆账号:<input type="text" id="user" name="user"><br>
  登陆密码:<input type="text" id="password" name="password"><br>
  发件人邮箱:<input type="text" id="form" name="from"><br>
  收件人邮箱:<input type="text" id="to" name="to"><br>
  邮箱标题:<input type="text" id="subject" name="subject"><br>
  邮箱内容:<input type="text" id="content" name="content"><br>
  <input type="submit" value="发送" name="submit">
  <input type="reset" value="重填" name="reset">
  
  </form>
   
  </body>
</html>

sendMail.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="send.SendTextMail;"%>
<jsp:useBean id="mySend" class="send.SendTextMail"></jsp:useBean>
<<jsp:setProperty property="*" name="mySend"/>
<html>
  <head>
    <title>My JSP 'sendMail.jsp' starting page</title>
    
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">    
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">


  </head>
  
  <body>
    <%boolean status = mySend.send();
    if (status){
    out.print("恭喜我!发到了!");
    } else{
    out.print("I'm so sorry");
    }
    %>
  </body>
</html>

SendTextMail.java
package send;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendTextMail {
String SMTPHost = "";
String user = "";
String password = "";
String from = "";
String to = "";
String subject = "";
String content = "";

public SendTextMail() {
}

public String getSMTPHost() {
return SMTPHost;
}

public void setSMTPHost(String host) {
SMTPHost = host;
}

public String getUser() {
return user;
}

public void setUser(String user) {
this.user = user;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
try {
subject = new String(content.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.subject = subject;
}

public String getContent() {
return content;
}

public void setContent(String content) {
try {
content = new String(content.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
this.content = content;
}

public boolean send() {
// 创建一个属性对象
Properties props = new Properties();
// 指定SMTP服务器
props.put("mail.stmp.host", SMTPHost);
// 指定是否需要SMTP验证
props.put("mail.smtp.host", true);
try {
// 创建一个授权验证对象
SmtpAuth auth = new SmtpAuth();
auth.setAccount(user, password);
// 创建一个session对象
Session mailSession = Session.getDefaultInstance(props, auth);
mailSession.setDebug(true);
// 创建一个message对象
Message message = new MimeMessage(mailSession);
// 指定发件人邮箱
message.setFrom(new InternetAddress(from));
// 指定收件人邮箱
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
// 指定邮件主题
message.setSubject(subject);
// 指定邮件内容
message.setText(content);
// 指定邮件发送日期
message.setSentDate(new Date());
// 指定邮件优先级
message.setHeader("X_Priority", "1");
message.saveChanges();
// 创建一个Transport对象
Transport transport = mailSession.getTransport("smtp");
// 连接stmp服务器
transport.connect(SMTPHost, user, password);
// 发送邮件
transport.sendMessage(message, message.getAllRecipients());
transport.close();
return true;
} catch (Exception e) {
return false;
}
}

// 定义一个SMTP授权验证类
static class SmtpAuth extends Authenticator {
String user, password;

// 设置账号信息
void setAccount(String user, String password) {
this.user = user;
this.password = password;
}

// 取得PasswordAuthenticationPasswordAuthentication对象
protected PasswordAuthentication getPasswordAuthenticaiton() {
return new PasswordAuthentication(user, password);
}

}

}
javamail --------------------编程问答-------------------- --------------------编程问答-------------------- 你的SMTPHost接到的值是什么?以前做的qq邮箱的,需要接收方开启协议 --------------------编程问答-------------------- 你本地有邮件收发服务器????
DEBUG SMTP: trying to connect to host "localhost", port 25, isSSL false
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,