Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Java Mailing program working in intranet but not in internet

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Java Mailing program working in intranet but not in internet

    Hi everyone,

    I have a little problem with my java mail coding.
    While i was using java mail coding in local netwok(intranet) properly executing.
    But i was used in internet, it is not working.
    I told my problem(java mail is not working prperly) to my server maintainer(service provider).
    They told "we loaded all mandatory jar files in Tomcat server please verify your coding"
    And told " please check your smtp protocal" and wrote code
    u shoud use smtp as 25

    other things asp and php coding are not supported in java server ..
    u should send mail coding through jsp
    Please check once & let us know if any problems !
    Please let us know the error URL / Path if you have any more problems, thanks

    please refer my coding and tell what is wrong with coding

    [COLOR=darkred]Properties props = System.getProperties();[/COLOR]
    [COLOR=darkred]props.put("mail.smtp.host", host);[/COLOR]
    [COLOR=darkred]//props.put("mail.debug", "true");[/COLOR]
    [COLOR=darkred]Session session = Session.getInstance(props, null);[/COLOR]
    [COLOR=darkred]//session.setDebug(debug);[/COLOR]
     
    [COLOR=darkred]try {[/COLOR]
    [COLOR=darkred]// create a message[/COLOR]
    [COLOR=darkred]MimeMessage msg = new MimeMessage(session);[/COLOR]
    [COLOR=darkred]msg.setFrom(new InternetAddress(from));[/COLOR]
    [COLOR=darkred]InternetAddress[] address = {new InternetAddress(to)};[/COLOR]
    [COLOR=darkred]msg.setRecipients(Message.RecipientType.TO, address);[/COLOR]
    [COLOR=darkred]msg.setSubject(subject);[/COLOR]
     
    [COLOR=darkred]// create and fill the first message part[/COLOR]
    [COLOR=darkred]MimeBodyPart mbp1 = new MimeBodyPart();[/COLOR]
    [COLOR=darkred]mbp1.setText(msgText1);[/COLOR]
     
    [COLOR=darkred]// create the second message part[/COLOR]
    [COLOR=darkred]MimeBodyPart mbp2 = new MimeBodyPart();[/COLOR]
     
    [COLOR=darkred]// attach the file to the message[/COLOR]
    [COLOR=darkred]FileDataSource fds = new FileDataSource(filename);[/COLOR]
    [COLOR=darkred]mbp2.setDataHandler(new DataHandler(fds));[/COLOR]
    [COLOR=darkred]mbp2.setFileName(fds.getName());[/COLOR]
     
    [COLOR=darkred]// create the Multipart and add its parts to it[/COLOR]
    [COLOR=darkred]Multipart mp = new MimeMultipart();[/COLOR]
    [COLOR=darkred]mp.addBodyPart(mbp1);[/COLOR]
    [COLOR=darkred]mp.addBodyPart(mbp2);[/COLOR]
     
    [COLOR=darkred]// add the Multipart to the message[/COLOR]
    [COLOR=darkred]msg.setContent(mp);[/COLOR]
     
    [COLOR=darkred]// set the Date: header[/COLOR]
    [COLOR=darkred]msg.setSentDate(new java.util.Date());[/COLOR]
     
    [COLOR=darkred]// send the message[/COLOR]
    [COLOR=darkred]Transport.send(msg);[/COLOR]
    [COLOR=darkred]flag=true;[/COLOR]
     
     
    [COLOR=darkred]}catch (MessagingException mex) {[/COLOR]
    [COLOR=darkred]// Prints all nested (chained) exceptions as well[/COLOR]
    [COLOR=darkred]mex.printStackTrace();[/COLOR]
    [COLOR=darkred]}[/COLOR]

    please reply immediately

    thanks
    sundar


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Java mail problem(working in intranet,but not working in iternet)

    I can't really see anything wrong with this code although I am unable to test it. Have you got any further with it?

    u shoud use smtp as 25
    Have you set:

    [COLOR=#8b0000]props.put("mail.smtp.host", host);[/COLOR]

    To the correct SMTP host and port? I think it should be:

    [COLOR=#8b0000]props.put("mail.smtp.host", 25);[/COLOR]

    Assuming that mail.smtp.host is your SMTP host.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    May 2008
    Location
    Canada
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java mail problem(working in intranet,but not working in iternet)

    How do you access the Internet? Any proxy involved? Is the SMTP server outside the Intranet?
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  4. #4
    Junior Member
    Join Date
    Nov 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java mail problem(working in intranet,but not working in iternet)

    I have this simple SMTP program working - both intranet and internet.

    See if this works for your needs.

     
                       public void sendMail()
    	{
    		try
    		{
        		Socket s = new Socket(this.getSmtpServer(), 25);
     
        		InputStream inStream = s.getInputStream();
        		OutputStream outStream = s.getOutputStream();
     
        		in = new Scanner(inStream);
        		out = new PrintWriter(outStream, true /* autoFlush */);
     
        		String hostname = InetAddress.getLocalHost().getHostName();
     
        		receive();
        		send("HELO " + hostname);
        		receive();
        		send("MAIL FROM: <" + this.getFrom() + ">");
        		receive();
     
        		// Parse the Test Plan names which are comma seperated
        		String patternStr = ",";
        		String[] fields = this.getTo().split(patternStr);
        		for (String value : fields)
            		send("RCPT TO: <" + value + ">");
     
        		receive();
        		send("DATA");
        		receive();
        		send("subject: " + this.getSubject());
        		send(this.getMessage());
        		send(".");
        		receive();
        		s.close();
    		}
    		catch (IOException e)
    		{
    			e.printStackTrace();
    		}
    	}
     
    	public static void receive() throws IOException
    	{
    		String line = in.nextLine();
     
    	}
     
    	public static void send (String s ) throws IOException
    	{
    		out.print(s.replaceAll("\n", "\r\n"));
    		out.print("\r\n");
    		out.flush();
    	}
     
    	private static Scanner in;
    	private static PrintWriter out;

  5. #5
    Junior Member
    Join Date
    Dec 2008
    Location
    london
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Java mail problem(working in intranet,but not working in iternet)

    please check ur smtp host and ur port number
    why don't u use gmail host
    imn my project i use gmail host
    host name is gmail.com port is 465
    if u are interested to see this code please click on the following link
    paruchuri.vigneswara - Sending Mails using java
    and put mail.jar file in ur lib directory or in the classpath
    Last edited by vigneswara; January 27th, 2009 at 12:04 AM. Reason: small spelling mistake

Similar Threads

  1. Sending and Receiving mail using J2ME without server
    By chals in forum Java ME (Mobile Edition)
    Replies: 5
    Last Post: June 2nd, 2009, 09:59 AM
  2. [SOLVED] Error "TitleChanger is abstract; cannot be instantiated"
    By Uzual in forum AWT / Java Swing
    Replies: 2
    Last Post: May 26th, 2009, 11:23 AM
  3. Java GUI problem in paintComponent?
    By Richard_ in forum AWT / Java Swing
    Replies: 2
    Last Post: May 1st, 2009, 08:19 AM
  4. [SOLVED] JAVA JList Problem in visual images
    By antitru5t in forum AWT / Java Swing
    Replies: 4
    Last Post: April 15th, 2009, 03:09 PM
  5. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 07:56 AM