Chapter One Introduction to Java Network programming _mailclient.java

Source: Internet
Author: User

Import java.net.*;
Import java.io.*;

public class mailsender{
Private String smtpserver= "smtp.mydomain.com"; The host name of the SMTP mail server
Private String smtpserver= "localhost";
private int port=25;

public static void main (string[] Args) {
Message msg=new message ("[email protected]",//sender's email address
"[email Protected]",//recipient's Email Address
"hello",//mail Header
"hi,i Miss you very much."); Message body
New MailSender (). SendMail (msg);
}

public void SendMail (Message Msg) {
Socket socket=null;
try{
Socket = new Socket (smtpserver,port); Connect to a mail server
BufferedReader BR =getreader (socket);
PrintWriter pw = Getwriter (socket);
String localhost= inetaddress.getlocalhost (). gethostname (); The name of the Client's host

SendAndReceive (null,br,pw); Just to receive the Server's response data
SendAndReceive ("HELO" + localhost,br,pw);
SendAndReceive ("MAIL from: <" + msg.from+ ">", br,pw);
SendAndReceive ("RCPT to: <" + msg.to+ ">", br,pw);
SendAndReceive ("DATA", br,pw); Next start sending message content
Pw.println (msg.data); Send Message Content
System.out.println ("client>" +msg.data);
SendAndReceive (".", br,pw); Message sent
SendAndReceive ("QUIT", br,pw); End Communication
}catch (ioexception E) {
E.printstacktrace ();
}finally{
try{
If (socket!=null) socket.close ();
}catch (ioexception E) {e.printstacktrace ();}
}
}

/** sends a row of strings and receives response data for a row of servers */
private void SendAndReceive (String Str,bufferedreader br,printwriter Pw) throws ioexception{
If (str! = Null) {
System.out.println ("client>" +str);
Pw.println (str); After sending the STR string, "\ r \ n" is also sent.
}
String response;
If ((response = br.readline ()) = Null)
System.out.println ("server>" +response);
}

Private PrintWriter getwriter (socket Socket) throws ioexception{
OutputStream socketout = Socket.getoutputstream ();
return new PrintWriter (socketout,true);
}
Private BufferedReader Getreader (socket Socket) throws ioexception{
InputStream Socketin = Socket.getinputstream ();
return new BufferedReader (new InputStreamReader (socketin));
}
}

Class Message{//indicates Mail
String from; Sender's E-mail Address
String to; Recipient's Email Address
String subject; Message header
String content; Message body
String data; Message content, including message headers and body text
Public Message (string from,string to, string subject, string Content) {
this.from=from;
this.to=to;
this.subject=subject;
this.content=content;
Data= "Subject:" +subject+ "\ r \ n" +content;
}
}

Authmailclient.java



Import java.net.*;
Import java.io.*;

public class mailsenderwithauth{
Private String smtpserver= "smtp.163.com"; The host name of the SMTP mail server
private int port=25;

public static void main (string[] Args) {
Message msg=new message ("[email protected]",//sender's email address
"[email Protected]",//recipient's Email Address
"hello",//mail Header
"hi,i Miss you very much."); Message body
New Mailsenderwithauth (). SendMail (msg);
}

public void SendMail (Message Msg) {
Socket socket=null;
try{
Socket = new Socket (smtpserver,port); Connect to a mail server
BufferedReader BR =getreader (socket);
PrintWriter pw = Getwriter (socket);
String localhost= inetaddress.getlocalhost (). gethostname (); The name of the Client's host

String username= "[email protected]";
String password= "38633638sa";
Base64 encoding of user names and passwords
Username = new Sun.misc.BASE64Encoder (). encode (username.getbytes ());
Password = new Sun.misc.BASE64Encoder (). encode (password.getbytes ());
SendAndReceive (null,br,pw); Just to receive the Server's response data
SendAndReceive ("EHLO" + localhost,br,pw);
SendAndReceive ("AUTH LOGIN", br,pw); Authentication command
SendAndReceive (username,br,pw); User name
SendAndReceive (password,br,pw); Password
SendAndReceive ("MAIL from:" + msg.from+ "", br,pw);

SendAndReceive ("RCPT to:" + msg.to+ "", br,pw);
SendAndReceive ("DATA", br,pw); Next start sending message content
Pw.println (msg.data); Send Message Content
System.out.println ("client>" +msg.data);
SendAndReceive (".", br,pw); Message sent
SendAndReceive ("QUIT", br,pw); End Communication
}catch (ioexception E) {
E.printstacktrace ();
}finally{
try{
If (socket!=null) socket.close ();
}catch (ioexception E) {e.printstacktrace ();}
}
}

/** sends a row of strings and receives response data for a row of servers */
private void SendAndReceive (String Str,bufferedreader br,printwriter Pw) throws ioexception{
If (str! = Null) {
System.out.println ("client>" +str);
Pw.println (str); After sending the STR string, "\ r \ n" is also sent.
}
String response;
If ((response = br.readline ()) = Null)
System.out.println ("server>" +response);
}

Private PrintWriter getwriter (socket Socket) throws ioexception{
OutputStream socketout = Socket.getoutputstream ();
return new PrintWriter (socketout,true);
}
Private BufferedReader Getreader (socket Socket) throws ioexception{
InputStream Socketin = Socket.getinputstream ();
return new BufferedReader (new InputStreamReader (socketin));
}
}

Class Message{//indicates Mail
String from; Sender's E-mail Address
String to; Recipient's Email Address
String subject; Message header
String content; Message body
String data; Message content, including message headers and body text
Public Message (string from,string to, string subject, string Content) {
this.from=from;
this.to=to;
this.subject=subject;
this.content=content;
Data= "Subject:" +subject+ "\ r \ n" +content;
}
}


Chapter One Introduction to Java Network programming _mailclient.java

Related Article

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.