sends e-mail using a mailto: URL : URL « Network Protocol « Java






sends e-mail using a mailto: URL

sends e-mail using a mailto: URL
      

/*
 * Copyright (c) 2004 David Flanagan.  All rights reserved.
 * This code is from the book Java Examples in a Nutshell, 3nd Edition.
 * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
 * You may study, use, and modify it for any non-commercial purpose,
 * including teaching and use in open-source projects.
 * You may distribute it non-commercially as long as you retain this notice.
 * For a commercial use license, or to purchase the book, 
 * please visit http://www.davidflanagan.com/javaexamples3.
 */
//package je3.net;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.URL;
import java.net.URLConnection;

/**
 * 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()));

      // We're talking to the SMTP server now.
      // Write out mail headers. Don't let users fake the From address
      out.print("From: \"" + from + "\" <" + System.getProperty("user.name") + "@"
          + InetAddress.getLocalHost().getHostName() + ">\r\n");
      out.print("To: " + to + "\r\n");
      out.print("Subject: " + subject + "\r\n");
      out.print("\r\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 + "\r\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>]");
    }
  }
}

   
    
    
    
    
    
  








Related examples in the same category

1.Creating a URL with a single string.
2.Creating a URL With components
3.Converting Between a Filename Path and a URL
4.URL Constructor Test
5.URL Encode Test
6.Get URL Content
7.Get URL Parts
8.Read from a URL
9.Convert a URL to a URI
10.Converting Between a URL and a URI
11.Convert an absolute URI to a URL
12.URL Equality
13.Parsing a URL
14.URL Request
15.URL Get
16.A URL Retrieval Example
17.URL Reader
18.URL Connection ReaderURL Connection Reader
19.Using URLConnection
20.Parse URLParse URL
21.Resolve a relative URL
22.Convert the absolute URI to a URL object
23.Convert URI to URL
24.Get parts of a url
25.Checks, whether the URL uses a file based protocol.
26.Add Parameter to URL
27.Returns the anchor value of the given URL
28.Extracts the file name from the URL.
29.Creates a relative url by stripping the common parts of the the url.
30.Checks, whether the URL points to the same service. A service is equal if the protocol, host and port are equal.
31.Extracts the base URL from the given URL by stripping the query and anchor part.
32.Returns true if the URL represents a path, and false otherwise.
33.Parse Port
34.Parse Host
35.Given a URL check if its a jar url(jar:!/archive) and if it is, extract the archive entry into the given dest directory and return a file URL to its location
36.check the validity of url pattern according to the spec.
37.A collection of File, URL and filename utility methods
38.Build Relative URL Path
39.Checks that the protocol://host:port part of two URLs are equal
40.Create valid URL from a system id
41.Extract URL File Name
42.Extract the URL page name from the given path
43.Get Domain Name
44.Get Locale From String
45.Get URL Last Modified
46.Get the name of the parent of the given URL path
47.Get the parent of the given URL path
48.Has URLContent Changed
49.Is URL a local file
50.Normalize an URL
51.Normalizes an URL
52.Resolve a relative URL string against an absolute URL string
53.ResourceBundle String manager
54.Save URL contents to a file
55.URL Path: standardize the creation of mutation of path-like structures
56.Utility class for building URLs
57.Add Default Port to a URL If Missing
58.Get Relative Path To URL
59.Download from a URL and save to a file