用JAVA发邮件问题,急!

jonkin1206 2010-01-05 05:35:12
我用的是SSH框架开发的,在里面写了个发邮件公司,报以下错误,查了好久都没解决,请各位高手帮忙解决一下
java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
...全文
471 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
a312983516 2011-03-03
  • 打赏
  • 举报
回复
NativeMethodAccessorImpl 这个类第39行发出来看看
jonkin1206 2010-01-06
  • 打赏
  • 举报
回复
我直接在类里面写了个main测试,是可以发邮件的,但通过ACTION调用发邮件,就在页面上报上面的错误。
jonkin1206 2010-01-06
  • 打赏
  • 举报
回复
package com.lmail;

import com.opensymphony.xwork2.ActionSupport;

public class UsersAction extends ActionSupport {
private String email;

public String execute(){
SendMail.sendMail(email,"WWW.SINA.COM.CN");
return SUCCESS;
}
public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}
}


package com.lmail;


public class SendMail {
public static void sendMail(String toEmail, String uri) {
CommonMail themail = new CommonMail("mail.holpe.com.cn");
themail.setNeedAuth(false);
String title = "ASDFASDFASF
if(themail.setSubject(title) == false) return;
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.append("<html><head>");
contentBuilder
.append("<meta http-equiv=\"Content-Language\" content=\"zh_CN\"/>");
contentBuilder
.append("<meta http-equiv=\"Content-Type\" content=\"text/html\" charset=\"GB2312\"/>");
contentBuilder.append("<title></title><head><body>");
contentBuilder.append(toEmail + ",");
contentBuilder.append("<br />这封信是由 恒波乐园为你 发送的。你已经成功在我们网站上进行了注册,");
contentBuilder.append("<br />现在你需要激活码来激活你的账户。您只需点击下面的链接即可激活您的帐号:");
contentBuilder.append("<br /><br />");
contentBuilder.append("<a href='" + uri + "' target=\"_blank\">" + uri
+ "</a>");
contentBuilder.append("(如果上面不是链接形式,请将地址手工粘贴到浏览器地址栏再访问) ");
contentBuilder.append("</body></html>");
if(themail.setBody(contentBuilder.toString()) == false) return;
System.out.println();
if(themail.setTo(toEmail) == false) return;
if(themail.setFrom("jonkin@163.com") == false) return;
themail.setNamePass("jonkin@163.com","123456");
if(themail.sendout() == false) return;

}
}



package com.lmail;

import java.util.*;

import javax.mail.*;
import javax.mail.Message;

import javax.mail.internet.*;

import java.util.Date;

import javax.activation.*;

import java.io.*;

public class CommonMail {
// public class TestMail {

private MimeMessage mimeMsg; // MIME邮件对象

private Session session; // 邮件会话对象

private Properties props; // 系统属性

private boolean needAuth = false; // smtp是否需要认证

private String username = "jonkin@163.com"; // smtp认证用户名和密码

private String password = "123456";

private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象

public CommonMail() {
// setSmtpHost(getConfig.mailHost);//如果没有指定邮件服务器,就从getConfig类中获取
setSmtpHost("mail.holpe.com.cn");// 如果没有指定邮件服务器,就从getConfig类中获取
createMimeMessage();
}

public CommonMail(String smtp) {
setSmtpHost(smtp);
createMimeMessage();

}

public void setSmtpHost(String hostName) {

System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机

}

public boolean createMimeMessage()
{
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
}
catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}

System.out.println("准备创建MIME邮件对象!");
try {
System.out.println("准备创建MIME邮件对象!22211111111111");
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
System.out.println("准备创建MIME邮件对象!00000000000");
mp = new MimeMultipart();
System.out.println("准备创建MIME邮件对象!22222222222");
return true;
}
catch (Exception e) {
System.out.println("准备创建MIME邮件对象!333333333333");
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}

public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}

public void setNamePass(String name, String pass) {
username = name;
password = pass;
}


public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
}
catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}

public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent("" + mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
}
catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}

public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
}
catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
}

public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
}
catch (Exception e)
{
return false;
}
}

public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
}
catch (Exception e)
{
return false;
}
}

public boolean setCopyTo(String copyto)
{
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
}
catch (Exception e)
{
return false;
}
}

public boolean sendout()
{
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
//transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
}
catch (Exception e)
{
System.err.println("邮件发送失败!" + e);
return false;
}
}

}

WEB.XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!--
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
-->
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!-- 配置spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<!-- 开启监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Spring 刷新缓存清除监听器Introspector防止内存泄露 -->
<listener>
<listener-class>
org.springframework.web.util.IntrospectorCleanupListener
</listener-class>
</listener>



<servlet>
<servlet-name>dwr</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>Debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>dwr</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

<!-- 配置OpenSessionInViewFilter,必须在struts2监听之前 -->
<filter>
<filter-name>lazyLoadingFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<param-name>singleSession </param-name>
<param-value>false</param-value>
</init-param>
</filter>



<!--中文过滤-->
<filter>
<filter-name>encoding</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>

<filter>
<description>
</description>
<display-name>struts2</display-name>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
<!--
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
-->
</filter>


<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 设置监听加载上下文 -->

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
<!--首页-->
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
deng1234 2010-01-05
  • 打赏
  • 举报
回复
不会是内存溢出吧,改一下tomcat的默认内存。
Dazzlingwinter 2010-01-05
  • 打赏
  • 举报
回复
太长了,帖代码呀
发帖
Java

4.9w+

社区成员

Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
帖子事件
创建了帖子
2010-01-05 05:35
社区公告
暂无公告