Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

msg.addRecipient(Message.RecipientType.TO,to) - won't compile in program

843834Mar 11 2008 — edited Jul 21 2008
I have had success in one of my servlets using JavaMail. I was able to send an email to my inbox to test it out. I proceeded to create another servlet based on that 1st one, and for reasons I can't explain, the servlet won't compile.
I get this error.
cannot resolve symbol :
         variable : RecipientType
         location : java.lang.String
                       msg.setRecipient(Message.RecipientType.TO, to); 
I get the same error when I switch out setRecipient for addRecipient.
However, through further testing, I've found that not only does my 1st servlet still compile, but I'm also able to run the code from the 2nd servlet, successfully in a JSP page. This is driving me nuts...how could there be a problem compiling? There's no reason why one servlet compiles (with similar if not almost exactly the same code) and the other won't. Plus this runs fine in a JSP page...what's going on??? I've spent hours on this and I can't figure it out...please help, any input is appreciated.

Here is the JSP page that runs successfully :
<%@page import="java.io.*"%>
<%@page import="java.util.Properties"%>
<%@page import="javax.mail.*"%>
<%@page import="javax.mail.Message.RecipientType"%>
<%@page import="javax.mail.internet.*"%>
<%@page import="javax.servlet.*"%>
<%@page import="javax.servlet.http.*"%>
<%@page import="javax.naming.*"%>
<%@page import="javax.sql.*"%>
<%@page import="java.sql.*"%>

<%					
		 Connection conn = null;
		 Statement stmt = null;
		 ResultSet rs = null; 
		 		
		    try {						
			Context ctx = new InitialContext();
	      		if(ctx == null ) 
	            throw new Exception("Boom - No Context");
	
	   			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/myDB");      
		        conn = ds.getConnection(); 
		        stmt = conn.createStatement();  		        
		            		   		
        			
        		//get the POP3 and SMTP property values from db
				    rs = stmt.executeQuery("Select Tech_Support_Email, POP3, SMTP from sitewide_info");        					    		
		    		String hostemailaddress = "";
		    		String POP3 = "";  //mail.smtp.host, this one 1st
		    		String SMTP = ""; //smtp.stratos.net, this one 2nd
		    		
		    		if(rs.next()) {
		    		   hostemailaddress = rs.getString(1);
		    		   POP3 = rs.getString(2);
		    		   SMTP = rs.getString(3);
		    		  }		    
            		
		    // Specify the SMTP Host
    	    Properties props = new Properties();		    			    		
    		//POP3 = mail.smtp.host & SMTP = smtp.stratos.net - must be in this order
    		props.put(POP3, SMTP);
		
		    // Create a mail session
		    Session ssn = Session.getDefaultInstance(props, null);
		    ssn.setDebug(true);			    		                
            
		    String subject = "Testing out Email";
		    String body = "hello";
		          
		    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    		    
		    rs = stmt.executeQuery("Select Customer_Name, Email_Address from customer_profiles where "
											+"Customer_Number=1111");
					String fromName = "";
					String fromEmail = "";
					if(rs.next()) {
					   fromName = rs.getString(1);
					   fromEmail = rs.getString(2);
					   }																    
		    
		    InternetAddress from = new InternetAddress(fromEmail,fromName);			    		    
			
		    String toName = "Bob";		
		    InternetAddress to = new InternetAddress(hostemailaddress,toName);		    
		
		        // Create the message
		        Message msg = new MimeMessage(ssn);
		        msg.setFrom(from);
		        msg.addRecipient(Message.RecipientType.TO, to);
		        msg.setSubject(subject);
		        msg.setContent(body, "text/html");
		
		        Transport.send(msg);		    
		    
		    }//try
		     catch (MessagingException mex) {		            
		            mex.printStackTrace(); } 
		     catch(Exception e) {}
		     		           	        
        finally { 	    
	    
		  try { 
    		if (rs!=null) rs.close();
  			  }
  			catch(SQLException e){}  		  
  		  try { 
    		if (stmt!=null) stmt.close();
  			  }
  			catch(SQLException e){}
  		  try { 
    		if (conn!=null) conn.close();
  			  }
  			catch(SQLException e){}
        }//finally  	    
%>         	 	  	
Here's the servlet that won't compile :
package testing.servlets.email;

import java.io.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.Message.RecipientType;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.naming.*;
import javax.sql.*;
import java.sql.*;


	public class MessageCenterServlet extends HttpServlet {
				
		public Connection conn = null;
		public Statement stmt = null;
		public Statement stmt2 = null;
		public ResultSet rs = null;
		public ResultSet rss = null;
		public PrintWriter out = null;
		
		public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
		{
			doPost(req,res);
		}
		
		public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException
		{				
		
		    try {			
				out = res.getWriter();
				Context ctx = new InitialContext();
	      		if(ctx == null ) 
	            throw new Exception("Boom - No Context");
	
	   			DataSource ds = 
	            (DataSource)ctx.lookup(
	               "java:comp/env/jdbc/myDB");      
		        conn = ds.getConnection(); 
		        stmt = conn.createStatement();  
		        stmt2 = conn.createStatement();		        
		        
		        HttpSession session = req.getSession();		        		        		                
		        
		           String Subject = (String)session.getAttribute("Subject");
        		   String Message = (String)session.getAttribute("Message");
		           sendAnEmail(rs,stmt,Subject,Message,res);  		        		        
		        
				}//try
		           
		        catch (Exception e) { }
		        
		        finally { cleanup(rs,rss,stmt,stmt2,conn); }
	        
	     }//post        
	    
	    public void cleanup(ResultSet r, ResultSet rs, Statement s, Statement s2, Connection c){
		  try { 
    		if (r!=null) r.close();
  			  }
  			catch(SQLException e){}
  		  try { 
    		if (rs!=null) rs.close();
  			  }
  			catch(SQLException e){}
  		  try { 
    		if (s!=null) s.close();
  			  }
  			catch(SQLException e){}
  		  try { 
    		if (s2!=null) s2.close();
  			  }
  			catch(SQLException e){}
  		  try { 
    		if (c!=null) c.close();
  			  }
  			catch(SQLException e){}
        }//cleanUp  	    
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  	    	
        public void sendAnEmail(ResultSet rs, Statement stmt, String Subject,String Message, HttpServletResponse res) {  	    
        	try {			
				
        		//get the POP3 and SMTP property values from db
				    rs = stmt.executeQuery("Select Tech_Support_Email, POP3, SMTP from sitewide_info");        					    		
		    		String hostemailaddress = "";
		    		String POP3 = "";
		    		String SMTP = "";
		    		
		    		if(rs.next()) {
		    		   hostemailaddress = rs.getString(1);
		    		   POP3 = rs.getString(2);
		    		   SMTP = rs.getString(3);
		    		  }		           
            		
		    // Specify the SMTP Host
    	    Properties props = new Properties();		    			    		    		
    		props.put(POP3, SMTP);
		
		    // Create a mail session
		    Session ssn = Session.getDefaultInstance(props, null);
		    ssn.setDebug(true);			    		                
            
		    String subject = "Testing out Email";
		    String body = "hello";
		          
		    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		    		    
		    rs = stmt.executeQuery("Select Customer_Name, Email_Address from customer_profiles where "
											+"Customer_Number=1111");
					String fromName = "";
					String fromEmail = "";
					
					if(rs.next()) {
					   fromName = rs.getString(1);
					   fromEmail = rs.getString(2);
					   }															
		    
		    InternetAddress from = new InternetAddress(fromDealerEmail,fromDealerName);				
            String toName = "Bob";					    
            InternetAddress to = new InternetAddress(hostemailaddress,toName);
		
		        // Create the message
		        Message msg = new MimeMessage(ssn);
		        msg.setFrom(from);
		        msg.setRecipient(Message.RecipientType.TO, to);
		        msg.setSubject(subject);
		        msg.setContent(body, "text/html");
		
		        Transport.send(msg);		    
		    
		    }//try
		     catch (MessagingException mex) {		            
		            mex.printStackTrace(); } 
		     catch(Exception e) {}
		     
        	}//end
}//class  	  	
-Love2Java

Edited by: Love2Java on Mar 11, 2008 9:15 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 18 2008
Added on Mar 11 2008
5 comments
621 views