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
Code :
[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
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?
Quote:
u shoud use smtp as 25
Have you set:
Code :
[COLOR=#8b0000]props.put("mail.smtp.host", host);[/COLOR]
To the correct SMTP host and port? I think it should be:
Code :
[COLOR=#8b0000]props.put("mail.smtp.host", 25);[/COLOR]
Assuming that mail.smtp.host is your SMTP host.
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?
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.
Code :
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;
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