mardi 26 mars 2019

this is my index.jsp code to send gmail to user input mail address but its not working

i wrote this index.jsp code for send gmail for a another person .but itsnot working and give this error to me . "type Exception report

messageInternal Server Error

descriptionThe server encountered an internal error that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: javax.mail.internet.AddressException: Illegal address in string ``''

root cause

javax.mail.internet.AddressException: Illegal address in string ``''

note The full stack traces of the exception and its root causes are available in the GlassFish Server Open Source Edition 4.1.2 logs. ".

 <%-- 
     Document   : index
     Created on : Mar 26, 2019, 7:48:36 PM
      Author     : ADITHYA
--%>

<%@page import="java.util.Properties"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.internet.*"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
 <html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Sending an email</title>
</head>
<body onLoad="displayResult()">
    <h1>Sending an email</h1>

    <%!
        public static class SMTPAuthenticator extends Authenticator{

        public PasswordAuthentication getPasswordAuthentication (){
        return new PasswordAuthentication("portaluniverses","portal123!");
}
}

        %>
        <%
          int result=0;
          if(request.getParameter("send")!=null){

              String d_uname="portaluniverses@gmail.com";
              String d_password="portal123!";
              String d_host ="smtp.gmail.com";
              int d_port=465;

              String m_to=new String();
              String m_from = "portaluniverses@gmail.com";
              String m_subject=new String();
              String m_text=new String();

              if(request.getParameter("to")!=null){
                  m_to = request.getParameter("to");
              }
               if(request.getParameter("subject")!=null){
                  m_subject = request.getParameter("subject");
              }
               if(request.getParameter("message")!=null){
                m_text="<h1>welcome to  sasts </h1>";
                m_text =m_text.concat(request.getParameter("message"));
                m_text = m_text.concat("<br/><h2>hope you got link</h2>");
               }

               //creat property object
               Properties props = new Properties();

               //creat SMTPAuthentication object
               SMTPAuthenticator auth =new SMTPAuthenticator(); 

               //creat a mail sesion object 
               Session ses = Session.getInstance(props,auth);
               //creat a MIME style email message  object
               MimeMessage msg =new MimeMessage(ses);
               msg.setContent(m_text, "text/html");
               msg.setSubject(m_subject);
               msg.setFrom(new InternetAddress(m_from));
               msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));


               try{
                   Transport transport= ses.getTransport("SMTPS");

                   transport.connect(d_host,d_port, d_uname,d_password);

                   transport.sendMessage(msg, msg.getAllRecipients());

                   transport.close();   

                   result=1;

               }catch(Exception e){
                   out.println(e);
               }
          }

        %>       


    <form name="myForm" action="index.jsp" method="POST">
        <table border="0">
            <tbody>
                <tr>
                   <tr>To:</tr>
                   <tr> <input type="text" name="To" value="" /></tr>
                </tr>
                <tr>
                    <td>Subject</td>
                    <td><input type="text" name="subject" value="" size="50" /></td>
                </tr>
                 <tr>
                    <td>Message</td>
                    <td><textarea name="message" rows="4" cols="50"></textarea>  </td>
                </tr>

            </tbody>
        </table>
        <input type="hidden" name="hidden" value="<%= result %>" />
        <input type="reset" value="Clear" name="Clear" />
        <input type="submit" value="send" name="send" />
    </form>
        <script language="JavaScript">
            function displayResult(){
                if(document.myForm.hidden.value === "1"){
                    alert("Mail was sent");
                }
            }

</body>




Aucun commentaire:

Enregistrer un commentaire