Sending Mail : Email « Network Protocol « Java






Sending Mail

 
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;

public class MailExample {
  public static void main(String args[]) throws Exception {
    if (args.length != 3) {
      System.err.println("Usage: java MailExample host from to");
      System.exit(-1);
    }

    String host = args[0];
    String from = args[1];
    String to = args[2];

    // Get system properties
    Properties props = System.getProperties();

    // Setup mail server
    props.put("mail.smtp.host", host);

    // Get session
    Session session = Session.getDefaultInstance(props, null);

    // Define message
    MimeMessage message = new MimeMessage(session);
    message.setFrom(new InternetAddress(from));
    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
    message.setSubject("The Subject");
    message.setText("The Message");

    // Send message
    Transport.send(message);
  }
}

           
         
  








Related examples in the same category

1.Pure Java Email client
2.Sending Mail Using Sockets
3.Get Email Message Example
4.A Client to Send SMTP MailA Client to Send SMTP Mail
5.Mailer: Sends an email message
6.TestOpenMailRelay -- send self-returning SPAM to check for relay sitesTestOpenMailRelay -- send self-returning SPAM to check for relay sites
7.Sender -- send an email message
8.Sender -- send an email message with attachment
9.SendMime -- send a multi-part MIME email message
10.Read a file return mail headers one at a time
11.Send mail using GMAIL
12.Send Mail Implementation using simple SMTP
13.Validator for Zip code, Email, Phone number
14.Send email out