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

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » Network Protocol » URLScreenshots 
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. URL Constructor Test
2. URL Encode Test
3. Get URL Content
4. Get URL Parts
5. Read from a URL
6. URL Equality
7. URL Request
8. URL Get
9. A URL Retrieval Example
10. URL Reader
11. URL Connection ReaderURL Connection Reader
12. Using URLConnection
13. Parse URLParse URL
w__w_w_._j_a__v__a_2___s__.co__m | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.