• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Sending mail

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I have tried to send mail using this code...
----------------------------------------------
import java.io.*;
import java.net.*;
/**
* This program sends e-mail using a mailto: URL
**/
public class SendMail {
public static void main(String[] args) {
try {
// If the user specified a mailhost, tell the system about it.
if (args.length >= 1)
System.getProperties().put("mail.host", args[0]);
// A Reader stream to read from the console
BufferedReader in =
new BufferedReader(new InputStreamReader(System.in));
// Ask the user for the from, to, and subject lines
System.out.print("From: ");
String from = in.readLine();
System.out.print("To: ");
String to = in.readLine();
System.out.print("Subject: ");
String subject = in.readLine();
// Establish a network connection for sending mail
URL u = new URL("mailto:" + to); // Create a mailto: URL
URLConnection c = u.openConnection(); // Create its URLConnection
c.setDoInput(false); // Specify no input from it
c.setDoOutput(true); // Specify we'll do output
System.out.println("Connecting..."); // Tell the user
System.out.flush(); // Tell them right now
c.connect(); // Connect to mail host
PrintWriter out = // Get output stream to host
new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
// Write out mail headers. Don't let users fake the From address
out.print("From: \"" + from + "\" <" +
System.getProperty("user.name") + "@" +
InetAddress.getLocalHost().getHostName() + ">\n");
out.print("To: " + to + "\n");
out.print("Subject: " + subject + "\n");
out.print("\n"); // blank line to end the list of headers
// Now ask the user to enter the body of the message
System.out.println("Enter the message. " +
"End with a '.' on a line by itself.");
// Read message line by line and send it out.
String line;
for(; {
line = in.readLine();
if ((line == null) || line.equals(".")) break;
out.print(line + "\n");
}
// Close (and flush) the stream to terminate the message
out.close();
// Tell the user it was successfully sent.
System.out.println("Message sent.");
}
catch (Exception e) { // Handle any exceptions, print error message.
System.err.println(e);
System.err.println("Usage: java SendMail [<mailhost>]");
}
}
}
-----------------------------------------------------------
The program hangs on 'c.connect()' trying to find a connection i guess.
I submitted my mail host and my own e-mail address but it just hangs and somtimes it states that the connection is not found what am i doing wrong.

confused person.
 
Ranch Hand
Posts: 906
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,
At first glance, this question is not related to the webservices forum.
Maybe you should better post it elsewhere (java forum for instance)
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
John, J-L is correct. This post should be moved to some other forum (I already asked the moderator to do just that).
In the meanwhile, you might want to Google for "JavaMail tutorial" or something like that.
 
author and deputy
Posts: 3150
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It can be moved here https://coderanch.com/forums/f-45/java
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's gone there now.
 
I'm still in control here. LOOK at this tiny ad!
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic