[JAVA100 example]045, sending a message using the SMTP protocol

Source: Internet
Author: User
Tags flush mail socket

Import java.net.*;


import java.io.*;


/**


* <p>title: Send mail using SMTP </p>


* <p>description: This example sends mail based on the SMTP protocol </p>
by using the socket method

* <p>copyright:copyright (c) 2003</p>


* <p>Filename:sendSMTPMail.java</p>


* @version 1.0


*/


public class Sendsmtpmail {


/**


*<br> Method Description: Main method


*<br> Input Parameters: 1. Server ip;2. Email address


*<br> return type:


*/


public static void Main (string[] arges) {


if (arges.length!=2) {


System.out.println ("Use Java sendsmtpmail hostname | mail to ");


return;


  }


Sendsmtpmail t = new Sendsmtpmail ();


T.sendmail (Arges[0], arges[1]);


  }


/**


*<br> Method Description: Send mail


*<br> input parameter: String mailserver mail receiving server


*<br> input parameter: String recipient receive mail address


*<br> return type:


*/


public void SendMail (string mailserver, String recipient) {


try {


//With socket Open 25 port


socket s = new socket (mailserver, 25);


//cache input and output


BufferedReader in = new BufferedReader


(New InputStreamReader (S.getinputstream (), "8859_1"));


BufferedWriter out = new BufferedWriter


(New OutputStreamWriter (S.getoutputstream (), "8859_1"));


//issue "HELO" command to express greetings to the server


send (in, out, "HELO theworld");


//Tell the server my email address, some servers have to check this address


send (in, out, "MAIL from: <>");


//Use the "RCPT to" command to tell the server to explain the e-mail address of the message


send (in, out, "RCPT to:" + recipient);


Sending a "DATA" means that the following will be the message body


send (in, out, "DATA");


//Use subject command to annotate message subject


Send (out, "Subject: This is a test program!") ");


//Use "from" to mark the source of the message


Send (out, "From:riverwind <>");


Send (out, "\ n");


//Message body


Send (out, "This is a message sent using the SMTP protocol!") If disturb please delete! ");


Send (out, "\n.\n");


//Send "QUIT" Port Mail


send (in, out, "QUIT");


S.close ();


   }


catch (Exception e) {


E.printstacktrace ();


   }


  }


/**


*<br> Method Description: Send a message and receive a reply


*<br> input Parameters:


*<br> return type:


*/


public void Send (BufferedReader in, BufferedWriter out, String s) {


try {


Out.write (s + "\ n");


Out.flush ();


System.out.println (s);


s = in.readline ();


System.out.println (s);


   }


catch (Exception e) {


E.printstacktrace ();


   }


  }


/**


*<br> Method Description: Overloaded method. Write information to the socket


*<br> input parameter: BufferedWriter out output buffer


*<br> input parameter: Information written by String s


*<br> return type:


*/


public void Send (BufferedWriter out, String s) {


try {


Out.write (s + "\ n");


Out.flush ();


System.out.println (s);


   }


catch (Exception e) {


E.printstacktrace ();


   }


  }


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.