当前位置: 代码迷 >> Java Web开发 >> 发送E-mail
  详细解决方案

发送E-mail

热度:942   发布时间:2005-11-25 22:29:00.0
发送E-mail

用户注册成功之后,通过邮件方式给用户自动发送一封邮件.
照到书上编了一个代码,运行总是不得行.
请问,是不是一定要自己建一个邮件服务器?
以及这其中还有哪些值得注意(或步骤)的啊?
谢谢!

搜索更多相关主题的帖子: 邮件服务器  成功  用户  

----------------解决方案--------------------------------------------------------
是不需要自己建立邮件服务器的!用已经现有的就好了,不过要注意设置端口,pop3和smtp的!如果代码不会的话,自己去google上搜!
----------------解决方案--------------------------------------------------------
把你的程序贴出来啊,我已搞JavaMail,大家共同探讨啊
----------------解决方案--------------------------------------------------------

谢了!
弄了一个简单的E-mail代码
<%@page import="javax.mail.*,javax.mail.internet.*,javax.activation.*,java.util.*"%>
<html>
<head>
<title>JavaMail Example</title>
</head>
<body>
<%
try{
Properties props=new Properties();
Session sendMailSession;
Transport transport;
sendMailSession=Session.getInstance(props,null);
props.put("mail.smtp.host","localhost");
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>
/////////////
其中props.put("mail.smtp.host","localhost")该怎样设置啊?


----------------解决方案--------------------------------------------------------


import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;
import java.util.Properties;

class EmailAuthenticator extends Authenticator
{
String m_username=null;
String m_userpass=null;
void setUsername(String username)
{m_username=username;
}
void setUserpass(String userpass)
{m_userpass=userpass;
}
EmailAuthenticator(String username,String userpass)
{super();
setUsername(username);
setUserpass(userpass);
}
public PasswordAuthentication getPasswordAuthentication()
{return new PasswordAuthentication(m_username,m_userpass);
}
}

public class newSendMail
{
public static void main(String args[])
{String host="smtp.163.com";
String from="...@163.com";
String to="....@...";
String username="...@163.com"; //同from相同
String password=""; //输入密码
EmailAuthenticator mailAutherticator=new EmailAuthenticator(username,password);
try
{Properties props=System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");

Session session=Session.getInstance(props,(Authenticator)mailAutherticator);
MimeMessage message=new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("hello,shiming");
message.setText("welcome here");
message.saveChanges();
Transport transport=session.getTransport("smtp");
transport.connect(host,username,password);
transport.sendMessage(message,message.getAllRecipients());
transport.close();
System.out.println("send successful");
}
catch(Exception e){System.out.println("no "+e);}
}
}


----------------解决方案--------------------------------------------------------
老兄,有没搞过Struts,我想学习一下,可不知道怎么配置,我是在Tomcat下开发的
----------------解决方案--------------------------------------------------------

做的网站小得很,用不上Struts,也没有学,该天向你请教哈.
你给出的代码不用自己整个html嘛,我编译通过了,直接运行出现了如下错误,
想必你运行成功了的.能帮忙分析一下原因?再次谢了!
HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Error instantiating servlet class newSendMail
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:865)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:621)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)


root cause

java.lang.NoClassDefFoundError: EmailAuthenticator
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:1618)
at java.lang.Class.getConstructor0(Class.java:1930)
at java.lang.Class.newInstance0(Class.java:278)
at java.lang.Class.newInstance(Class.java:261)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:856)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:621)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:163)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:144)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardContext.invoke(StandardContext.java:2358)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:133)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:118)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:116)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:594)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:127)
at org.apache.catalina.core.StandardPipeline$StandardPipelineValveContext.invokeNext(StandardPipeline.java:596)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:433)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:948)
at org.apache.coyote.tomcat4.CoyoteAdapter.service(CoyoteAdapter.java:152)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:683)
at java.lang.Thread.run(Thread.java:534)


----------------解决方案--------------------------------------------------------
呵呵,这位仁兄,不好意思,再问一下,你这是servlet,还是javabean?
还有其它代码吗?我是边做边学,见笑了.
----------------解决方案--------------------------------------------------------
明白代码是怎么回事了,但是运行会出现如下问题:
请问这个问题如何解决??

D:\apache-tomcat-5.5.12\webapps\E-mail\WEB-INF\classes>java newSendMail
no javax.mail.MessagingException: Could not connect to SMTP host: smtp.163.com,
port: 25, response: -1
----------------解决方案--------------------------------------------------------
String host="smtp.163.com";
String from="...@163.com";
String to="....@...";
String username="...@163.com"; //同from相同
String password=""; //输入密码
上面这几项你设置完全正确吗?
比如说用户名与密码是否正确?
我那个是写的Java程序,你直接用JC就可以编译运行啊。
不过作相应的转变后用在Jsp里也应该一样了,不过我到没试过。
----------------解决方案--------------------------------------------------------
  相关解决方案