1

我想通过 servlet 触发我的电子邮件,但它没有触发。当我将代码作为独立的 Java 应用程序运行时,它运行良好。下面是我的代码。
Servlet 给了我问题,从服务器我无法触发代码

package model;

import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.InitialContext;
importjavax.naming.NamingException;

import com.sun.xml.internal.bind.CycleRecoverable.Context;

public class TestJavaMail   
{

    String userid ;
    String password;
    public TestJavaMail(String userid , String password)
    {
        this.userid = userid;
        this.password =password;
    }

        public void process()
        {

        String[] to = {"faiz.akhtar@agnitio-technologies.com","manish.kaushik@agnitio-           technologies.com",
                "sandeep.sharma@agnitio-technologies.com"};
        Properties props = new Properties();   
        props.put("mail.transport.protocol", "smtp");   
        props.put("mail.smtp.host", "smtp.exchangemails.com");   
        props.put("mail.smtp.port", 25);   

        props.put("mail.smtp.user", userid);   
        props.put("mail.smtp.pass", password);   
        props.put("mail.smtp.starttls.enable", "true");   
        props.put("mail.smtp.auth ", "true");   

                                Authenticator auth = new javax.mail.Authenticator()   
        {   
             protected PasswordAuthentication getPasswordAuthentication()   
            {   
                return new PasswordAuthentication(userid, password);   
            }   
        };   

                    Session session = Session.getDefaultInstance(props,auth);   

        try  
        {   
                                MimeMessage msg = new MimeMessage(session);   
            System.out.println("Mimemessage ceated");   
            InternetAddress[] iaFrom = { new InternetAddress(   
                    "moonis.raza@agnitio-technologies.com") };   
            msg.setFrom(iaFrom[0]);   
            InternetAddress[] iaTo =  new InternetAddress[to.length] ;  
            for(int i=0;i<to.length;i++)
            {
                iaTo[i] = new InternetAddress(to[i]);
                msg.addRecipient(Message.RecipientType.TO, iaTo[i]);
            }

            msg.setSubject("Test Java Mail");   
            msg.setSentDate(new Date());   
            msg.setText("Hello, Congrats - It is working\n pls send acknowledgement mail to senderof u get this" +
                    "\n as it is part of project");   
            Transport tran = session.getTransport("smtp");   
            System.out.println("Transport object created......");   
            //tran.setStarttls(true);   
            tran.connect("smtp.exchangemails.com", 25, "moonis.raza@agnitio-           technologies.com", "welcome");   
            //tran.connect();   
            msg.saveChanges();   
            System.out.println("Connect succeeded");   
            tran.sendMessage(msg, msg.getAllRecipients());   
            tran.close();   
            System.out.println("Mail Sent Successfully");   
        }   
        catch (MessagingException mex)   
        {   
            System.out.println("send failed, exception: " + mex);   
        }   
                        }   
}  

这是servlet代码:

package com.controller;

import java.io.*;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.TestJavaMail;
/**
 * Servlet implementation class emailservice
 */
   public class emailservice extends HttpServlet {


    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws     ServletException, IOException {
        res.setContentType("text/html");
        System.out.println("here");

            PrintWriter writer = res.getWriter();
            writer.println("HELLO WORLD");
            String h = "hello";

            writer.println("" +h);

            writer.println("welcome ");

            writer.println(req.getRemoteHost());




        String userid = getServletConfig().getInitParameter("userid");
        String password = getServletConfig().getInitParameter("password");
        writer.println(userid);     
        TestJavaMail t1 = new TestJavaMail(userid, password);

        t1.process();
        writer.println("\n bhej di ");


    }


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

}
4

1 回答 1

0

我试图为自己运行你的代码,看看会发生什么。首先,我确保mail.jar类路径上有可用的文件。其次,我做了一些必要的更改,以便能够运行代码而不会出现身份验证错误。即,而不是:

props.put("mail.smtp.host", "smtp.exchangemails.com"); 

我用了:

props.put("mail.smtp.host", "smtp.gmail.com");

显然,我必须为这个不同的主机提供我自己的用户名和密码。

你说的对; 该应用程序在独立执行时运行良好。但是,当我将其移至 a 时JBoss AS 7.0.0,我遇到了两个不同的问题。

最初,我遇到了这个错误:

at javax.mail.Session.getDefaultInstance(Session.java:320) [mail-1.4.4.jar:1.4.4]
at test.TestJavaMail.process(TestJavaMail.java:42) [classes:]

经过一番谷歌搜索并找到这里给出的解决方案后,我可以摆脱这个错误。即,我替换了这一行:

Session session = Session.getDefaultInstance(props, auth);

和:

Session session = Session.getInstance(props, auth);

然后,我尝试再次运行该应用程序。这一次,我收到以下错误:

java.lang.NoClassDefFoundError: javax/net/ssl/SSLPeerUnverifiedException
    com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
    com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    javax.mail.Service.connect(Service.java:295)
    test.TestJavaMail.process(TestJavaMail.java:63)

我用谷歌搜索了上面的错误,发现我使用的 JBoss 版本有一个错误。因此,我将应用程序移至更高JBoss版本,JBoss AS 7.1.1并且成功了。servlet 成功地向我为其提供的目的地发送了一封电子邮件。

我希望这项调查对您和任何遇到类似问题的访问者都有帮助。

于 2012-10-25T13:05:41.747 回答