Results 1 to 1 of 1
  1. #1
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    6

    Default Email send problem

    I have created a servlet which sends the mail to my mail serjantik@gmail.com
    All the libs mail.jar and activation.jar are in apche-tomcat libs folder,and also added to the Web Dynamic Project in Eclipse.I got such excpetion:
    Java Code:
    SEVERE: Servlet.service() for servlet SendEmail threw exception
    java.lang.SecurityException: Access to default session denied
    	at javax.mail.Session.getDefaultInstance(Session.java:292)
    	at org.eclipse.http.SamSite.SendEmail.doPost(SendEmail.java:65)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
    	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    	at java.lang.Thread.run(Thread.java:619)

    here is the sendmail.html file:

    Java Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <form                    method=post              action="SendEmail">
    
                        
    
                         Reciever        <input type=text name=text2><br>
    
                         Subject          <input type=text name=text3><br>
    
                        Message        <textarea name='area1' rows=5 cols=30>                  </textarea>
    
                                                 <input     type=submit>
    
    </form>
    </body>
    </html>
    And SendMail servlet file code
    Java Code:
    package org.eclipse.http.SamSite;
    
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.*;
    
    import javax.servlet.http.*;
    
    import java.io.*;
    
    import javax.mail.*;
    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
    import javax.mail.internet.*;   // important
    import javax.mail.event.*;      // important
    import java.net.*;
    
    import java.util.*;
    /**
     * Servlet implementation class for Servlet: SendEmail
     *
     */
     public class SendEmail extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
       static final long serialVersionUID = 1L;
       private static final String SMTP_HOST_NAME = "gmail-smtp.l.google.com";
       private static final String SMTP_AUTH_USER = "serjantik";
       private static final String SMTP_AUTH_PWD = "";
       private static final String fromEmailAddress="serjantik@gmail.com";
    	
        /* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#HttpServlet()
    	 */
    	public SendEmail() {
    		super();
    	}   	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		
    	}  	
    	
    	/* (non-Java-doc)
    	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
    	 */
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		// TODO Auto-generated method stub
    		PrintWriter  out=response.getWriter();
            response.setContentType("text/html");
            boolean debug = false;
            
    		
            //properties
            Properties props = new Properties();
    		props.put("mail.transport.protocol", "smtp");
    		props.put("mail.smtp.starttls.enable","true");
    		props.put("mail.smtp.host", SMTP_HOST_NAME);
    		props.put("mail.smtp.auth", "true");
     
    		Authenticator auth = new SMTPAuthenticator();
    		Session session = Session.getDefaultInstance(props, auth);
           
            String s2 = request.getParameter("text2");
            String s3 = request.getParameter("text3");
            String s4 = request.getParameter("area1");
            
         
            session.setDebug(debug);
            
    		// create a message
    		Message msg = new MimeMessage(session);
    		try{
    			// set the from and to address
    			InternetAddress addressFrom = new InternetAddress(fromEmailAddress);
    			msg.setFrom(addressFrom);
     
    			InternetAddress[] addressTo = new InternetAddress[]{new InternetAddress(s2)};
    		
    			msg.setRecipients(Message.RecipientType.TO, addressTo);
     
    			// Setting the Subject and Content Type
    			msg.setSubject(s3);
    			msg.setContent(s4, "text/plain");
    			Transport.send(msg);
    		}catch(MessagingException err){
    			out.println("The message wasn't sent,some errors occured");
    		}
    		
         
    	}
    	
    	private class SMTPAuthenticator extends javax.mail.Authenticator {
    		 
    		public PasswordAuthentication getPasswordAuthentication() {
    			String username = SMTP_AUTH_USER;
    			String password = SMTP_AUTH_PWD;
    			return new PasswordAuthentication(username, password);
    		}
    	}
    }
    What is wrong with SSL?
    Last edited by serjant; 09-19-2008 at 02:22 AM.

Similar Threads

  1. send email using JMS
    By Heather in forum Advanced Java
    Replies: 9
    Last Post: 01-07-2009, 03:04 PM
  2. Replies: 1
    Last Post: 09-27-2008, 01:42 AM
  3. help in email developing
    By reached in forum New To Java
    Replies: 0
    Last Post: 12-09-2007, 07:55 PM
  4. Email Sigs.
    By tomrayne in forum Advanced Java
    Replies: 1
    Last Post: 07-13-2007, 09:14 AM
  5. To send email of page present Web from a Jsp
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-09-2007, 04:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •