Welcome, Guest. Please login or register.
Did you miss your activation email?
Pages: [1]   Go Down
  Print  
Author Topic: Send mail using JavaMail  (Read 480 times)
0 Members and 1 Guest are viewing this topic.
Arkie
Javaforums.net Admin
Senior Member
*

Reputation: 16
Developer @ Javaforums.net
Offline Offline
Posts: 2593
Referrals: 13

WWW Awards
« on: June 13, 2009, 06:49:34 PM »

Code
GeSHi (java):
  1. // This example is from _Java Examples in a Nutshell_. (http://www.oreilly.com)
  2. // Copyright (c) 1997 by David Flanagan
  3. // This example is provided WITHOUT ANY WARRANTY either expressed or implied.
  4. // You may study, use, modify, and distribute it for non-commercial purposes.
  5. // For any commercial use, see http://www.davidflanagan.com/javaexamples
  6.  
  7. import java.io.*;
  8. import java.net.*;
  9.  
  10. /**
  11.  * This program sends e-mail using a mailto: URL
  12.  **/
  13. public class SendMail {
  14.  public static void main(String[] args) {
  15.    try {
  16.      // If the user specified a mailhost, tell the system about it.
  17.      if (args.length >= 1) System.getProperties().put("mail.host", args[0]);
  18.  
  19.      // A Reader stream to read from the console
  20.  
  21.      // Ask the user for the from, to, and subject lines
  22.      System.out.print("From: ");
  23.      String from = in.readLine();
  24.      System.out.print("To: ");
  25.      String to = in.readLine();
  26.      System.out.print("Subject: ");
  27.      String subject = in.readLine();
  28.  
  29.      // Establish a network connection for sending mail
  30.      URL u = new URL("mailto:" + to);      // Create a mailto: URL
  31.      URLConnection c = u.openConnection(); // Create a URLConnection for it
  32.      c.setDoInput(false);                  // Specify no input from this URL
  33.      c.setDoOutput(true);                  // Specify we'll do output
  34.      System.out.println("Connecting...");  // Tell the user what's happening
  35.      System.out.flush();                   // Tell them right now
  36.      c.connect();                          // Connect to mail host
  37.      PrintWriter out =                     // Get output stream to mail host
  38.        new PrintWriter(new OutputStreamWriter(c.getOutputStream()));
  39.  
  40.      // Write out mail headers.  Don't let users fake the From address
  41.      out.println("From: \"" + from + "\" <" +
  42.                  System.getProperty("user.name") + "@" +
  43.                  InetAddress.getLocalHost().getHostName() + ">");
  44.      out.println("To: " + to);
  45.      out.println("Subject: " + subject);
  46.      out.println();  // blank line to end the list of headers
  47.  
  48.      // Now ask the user to enter the body of the message
  49.      System.out.println("Enter the message. " +
  50.                         "End with a '.' on a line by itself.");
  51.      // Read message line by line and send it out.
  52.      String line;
  53.      for(;;) {
  54.        line = in.readLine();
  55.        if ((line == null) || line.equals(".")) break;
  56.        out.println(line);
  57.      }
  58.  
  59.      // Close the stream to terminate the message
  60.      out.close();
  61.      // Tell the user it was successfully sent.
  62.      System.out.println("Message sent.");
  63.      System.out.flush();
  64.    }
  65.    catch (Exception e) {  // Handle any exceptions, print error message.
  66.      System.err.println(e);
  67.      System.err.println("Usage: java SendMail [<mailhost>]");
  68.    }
  69.  }
  70. }
  71.  
Created by GeSHI 1.0.7.20
Logged

Java and .NET developer

To students: It doesn't matter how hard you've studied; the material won't be on the exam anyway.

Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://img222.imageshack....707/arkietomatoesmall.jpg
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
two0426
Junior Member
*

Reputation: 0
Offline Offline
Posts: 6
Referrals: 0

Awards
« Reply #1 on: May 31, 2010, 01:42:57 AM »

spam
« Last Edit: May 31, 2010, 02:17:13 AM by Arkie » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Your Ad Here