老大要我在 "部門負責人審批 "模塊裡加一個郵件提醒功能,方便將打回的申請通知申請人。已經通過request獲得了from,to,message,date,subject等,也知道公司的郵件服務器host,請教怎麼發送?
哪位高手有沒有利用javamail寫的發送郵件的程序,可以帖出來共享一下嗎?我在網上找了一個,修改成jsp運行,tomcat老提醒我:2007-011611:03:09 /pursue/email.jsp(1,16) equal symbol expected
這是什麼問題啊?
晚上加班在線等......
------解决方案--------------------
package mail;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.Properties;
public class Servlet1
extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK ";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String server = request.getParameter( "txtServer ");
String [] emailTo = new String[4];
for(int i=0;i <4;i++)
{
emailTo[i]=request.getParameter( "txtMailTo "+i);
}
String emailFrom = request.getParameter( "txtMailFrom ");
String topic = request.getParameter( "txtTopic ");
String body = request.getParameter( "txaBody ");
try
{
Properties p = new Properties();
p.put( "mail.transport.protocol ", "smtp ");
p.put( "mail.smtp.host ",server);
p.put( "mail.smtp.port ", "25 ");
Session s = Session.getInstance(p);
Message m = new MimeMessage(s);
for(int i=0;i <4;i++)
{
m.setFrom(new InternetAddress(emailFrom));
m.setRecipients(Message.RecipientType.TO,InternetAddress.parse(emailTo[i]));
m.setSentDate(new Date());
m.setSubject(topic);
m.setText(body);
Transport.send(m);
m.writeTo(System.out);
out.println( "邮件已成功发送到 "+emailTo[i]);
}
}
catch(Exception e)
{
System.out.println(e);
}
out.close();
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
doGet(request, response);
}
//Clean up resources
public void destroy() {
}
}