Java to write a simple e-mail sender program _java

Source: Internet
Author: User
Tags gettext nslookup command

This article introduces the simple e-mail Sender program Java Implementation Code, shared for everyone to reference, the specific contents are as follows

In this code, there are a few points of attention to emphasize :
1, use the Socket with the SMTP mail server to obtain a connection, note the host name of the SMTP server;
2, the use of the data command, if the written subject (subject), the body of the message must have a blank line between the subject, that is, "carriage return + line", in the code is \ r \ n;
3, the same need for the sender's mailbox user name, password BASE64 encoded and then passed to the SMTP server;
4. The program still has a warning at compile time because the Sun.misc.BASE64Encoder class exists in the Rt.jar package, and the compiler warns because the JDK updates the upgrade and may cause some classes in the package to change and become unavailable.
In addition, the code has been written and some problems have been identified :
1, smtp.qq.com and smtp.sina.com mail server do not know why can not use, that is, when the sender's e-mail address is QQ or SINA, this program regardless of the use of the status of the answer code can not read. In my test, only smtp.163.com can use, obviously are in the official website to find these several SMTP servers, how can not use it? It's strange. Any friend who knows the hope can tell me, thank you!
2, in the following Simplemailsender class in the SendEmail () method, some duplicate code is confusing, but there is no way, I still do not understand ...
3, a major discovery: QQ mailbox to receive mail faster than 163 mailbox, Sina mailbox may be dozens of times times faster, really let me surprised. In addition, the use of the nslookup command to query smtp.qq.com hostname, found that it has a lot of SMTP server, at least 163 3 units more than 5 units, Tencent is strong enough;
4, although said that the program can be malicious to keep a mailbox to send mail, but I found that when I use a Sina mailbox sent dozens of consecutive mail to a fixed another mailbox, the Sina mailbox again want to send mail will be rejected, careful yo.
The code is as follows:

Message class E_Mail {String from;
 String to;
 String subject;
 String content;
 String UserName;

 String pwd;
 Public E_Mail (string from, string to, string subject, string content, String userName, string pwd) {this.from = from;
 This.to = to;
 This.subject = subject;
 this.content = content;
 This.username = This.tobase64 (userName);
 This.pwd = This.tobase64 (pwd); /** * In the E_Mail class for user name, password transcoding work */private string toBASE64 (String str) {return (new Sun.misc.BASE64Encoder (). Enco 
 De (Str.getbytes ()));
 }//Simple mail delivery End class, Implement Send function public class Simplemailsender {private String smtpserver;

 private int port = 25;
 private socket socket;
 BufferedReader BR;
 
 PrintWriter PW; /** * According to the sender's mailbox address to determine the SMTP mail server */private void Initserver (String from) {if (From.contains ("@163")) {This.smtpserver =
 "Smtp.163.com";
 }else if (from.contains ("@126")) {this.smtpserver = "smtp.126.com";
 }else if (from.contains ("@sina")) {this.smtpserver = "smtp.sina.com"; }else if (From.contains ("@qq ")) {this.smtpserver =" smtp.qq.com ";
 
 } public void SendEmail (E_Mail email) {try {this.initserver (email.from);
 This.socket = new Socket (smtpserver, port);
 this.br = This.getreader (socket);
 
 THIS.PW = This.getwriter (socket); Start assembling the command sequence send_receive (NULL) for sending messages;
 Receive the successful information send_receive ("Ehlo hao") to connect to the SMTP server;
 Send_receive ("auth login");
 Send_receive (Email.username);
 Send_receive (EMAIL.PWD);
 Send_receive ("Mail from:<" + email.from + ">");
 Send_receive ("rcpt to:<" + email.to + ">");
 
 Send_receive ("Data");
 Message content Pw.println ("from:" + email.from);
 Pw.println ("to:" + email.to);
 
 Between the theme and the text must be empty line, that is, plus "\ r \ n" pw.println ("Subject:" + Email.subject + "\ r \ n");
 Print the contents of the message in the console System.out.println ("From:" + email.from); 
 System.out.println ("to:" + email.to);
 System.out.println ("Subject:" + Email.subject + "\ r \ n");
 
 System.out.println (email.content);
 
 Message body pw.println (email.content); Be sure to remember the text with "."
 End Send_receive (".");
 Send_receive ("Quit"); } CAtch (IOException e) {e.printstacktrace ();
 Finally {try {if (socket!= null) socket.close ();
 catch (IOException e) {e.printstacktrace (); }/** * Each send a command, you must add "\ r \ n" after the command, * then print out the corresponding status code for the SMTP mail server * @param command/private void send_receive (Strin
 g command) throws ioexception{if (command!= null) {//Send commands to SMTP mail server, make sure to remember to add "\ r \ n" pw.print (Command + "\ r \ n");
 Pw.flush ();
 System.out.println ("User >>" + command);
 } Char [] response = new char[1024];
 Br.read (response);
 SYSTEM.OUT.PRINTLN (response);  /** * Gets the output stream of the socket/private printwriter getwriter (socket socket) throws IOException {outputstream =
 Socket.getoutputstream ();
 return new PrintWriter (Socketout, true); /** * Get SOCKET INPUT stream * * Private BufferedReader Getreader (socket socket) throws IOException {InputStream Socketin
 = Socket.getinputstream ();
 return new BufferedReader (new InputStreamReader (Socketin));
}//test public static void main (string[] args) { New Mailsendergui ();
 }//Mail Sender Interface Class Mailsendergui extends JFrame implements ActionListener {private JLabel Usernamelabel;
 Private JTextField Usernamefield;
 Private JLabel Pwdlabel;
 Private JPasswordField Pwdfield;
 Private JLabel Fromlabel;
 Private JTextField Fromfield;
 Private JLabel Tolabel;
 Private JTextField Tofield;
 Private JLabel Subjectlabel;
 Private JTextField Subjectfield;
 Private JLabel Contentlabel;

 Private JTextArea Contentarea;
 Private JButton sendbtn;

 Private JButton cancelbtn;

 private E_Mail Email;

 Private Simplemailsender MailSender;
 Public Mailsendergui () {this.init ();
 This.mailsender = new Simplemailsender ();
 private void Init () {This.fromlabel = new JLabel ("Sender's mailbox Address:");
 This.fromfield = new JTextField (25);
 This.usernamelabel = new JLabel ("username:");
 This.usernamefield = new JTextField (25);
 This.pwdlabel = new JLabel ("Password:");
 This.pwdfield = new JPasswordField (25);
 This.tolabel = new JLabel ("Recipient mailbox Address:"); This.tofield = new JtexTfield (25);
 This.subjectlabel = new JLabel ("Mail Subject:");
 This.subjectfield = new JTextField (20);
 This.contentlabel = new JLabel ("message body:");

 This.contentarea = new JTextArea (15, 20);
 This.settitle ("Ant--> Simple Mail Transmitter");
 This.setbounds (200, 30, 500, 500);
 This.setlayout (New BorderLayout ());
 This.setdefaultcloseoperation (Jframe.exit_on_close);

 This.setvisible (TRUE);
 THIS.SENDBTN = new JButton ("send");

 THIS.CANCELBTN = new JButton ("reset");
 This.sendBtn.addActionListener (this);
 
 This.cancelBtn.addActionListener (this);
 JPanel Uppanel = new JPanel (New GridLayout (6, 2, 5, 5));
 Uppanel.add (Fromlabel);
 Uppanel.add (Fromfield);
 Uppanel.add (Usernamelabel);
 Uppanel.add (Usernamefield);
 Uppanel.add (Pwdlabel);
 Uppanel.add (Pwdfield);
 Uppanel.add (Tolabel);
 Uppanel.add (Tofield);
 Uppanel.add (Subjectlabel);
 Uppanel.add (Subjectfield);
 
 Uppanel.add (Contentlabel); 
 This.add (Uppanel, Borderlayout.north);
 
 This.add (Contentarea, Borderlayout.center); JPanel Downpanel = new JPanel (New FlowlayoUT (flowlayout.center));
 Downpanel.add (SENDBTN, Borderlayout.south);
 
 Downpanel.add (CANCELBTN, Borderlayout.south);
 This.add (Downpanel, Borderlayout.south); @Override public void actionperformed (ActionEvent e) {if (e.getsource () = = this.sendbtn) {this.email = new E_Mail (This.fromField.getText (), This.toField.getText (), This.subjectField.getText (), This.contentArea.getText (), th

 Is.userNameField.getText (), New String (This.pwdField.getPassword ()));

 This.mailSender.sendEmail (This.email);
 else if (e.getsource () = = this.cancelbtn) {this.fromField.setText (null);
 This.toField.setText (NULL);
 This.subjectField.setText (NULL);
 This.contentArea.setText (NULL);
 }

 }
}

The above is Java to write a simple e-mail sender of all the code, I hope to help you learn.

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.