Send an email with JavaMail

xiaoxiao2021-03-11  186

Send an email with JavaMail (including accessories)

Author: ◆ ◆ Backstreet Boys Date: 2000-12-14 17:21:16 // Title: Your Product Name // Version: // Copyright: Copyright (c) 1999 // Author: Your Name // Company: Your Company // Description: Your description package mail; import java.awt. *; Import java.awt.event. *; Import java.util. *; Import java.io. *; import javax. mail *;. import javax.mail.internet *;. import javax.activation *;. public class SendMail extends Frame {Label label1 = new Label (); TextField textField1 = new TextField (); Label label2 = new Label (); TextField textField2 = new TextField (); Label label3 = new Label (); TextArea textArea1 = new TextArea (); Label label4 = new Label (); TextField textField3 = new TextField (); Button button1 = new Button (); Button button2 = new Button (); public SendMail () {enableEvents (AWTEvent.WINDOW_EVENT_MASK); try {jbInit ();} catch (Exception e) {e.printStackTrace ();}} protected void processWindowEvent (WindowEvent e) {super.processWindowEvent (e); if (E.GetId () == windowevent.window_closing) {SYST Em.exit (0);}} public static void main (String [] args) {sendmail sendmail1 = new sendmail (); sendmail1.setsize (400, 400); sendmail1.show (true);} private void jbinit () THROWS EXCEPTION {Label1.setbounds (New Rectangle (41, 38, 45, 23)); Label1.Settext ("Received Man"); this.setLayout (NULL); this.setsize (400, 400); TextField1.setBounds (New Rectangle (110 , 36, 174, 23); Label2.SetBounds (New Rectangle (42, 75, 38, 23)); Label2.Settext ("Topic); TextField2.SetBounds (New Rectangle (110, 76, 173, 23) Label3.SetBounds (New Rectangle (43, 148, 38, 23)); Label3.Settext ("Content"); TextArea1.setBounds (New Rectangle (110, 155, 256, 170));

Label4.SetBounds (New Rectangle (45, 111, 44, 23); label4.settext ("attachment"); TextField3.SetBounds (New Rectangle (110, 115, 173, 23)); Button1.SetBounds (New Rectangle 70, 348, 88, 24); Button1.SetLabel ("Send"); Button1.AddActionListener (New Java.awt.Event.ActionListener () {public void ActionPerformed (ActionEvent E) {Button1_ActionPerformed (e);}}) Button2.setBounds (New Rectangle (244, 348, 88, 24); Button2.SetLabel ("Refill"); Button2.AddActionListener (New Java.awt.Event.ActionListener () {Public Void ActionPerformed (ActionEvent E) {Button2_ActionPerformed (e);}}); this.add (label1, null); this.add (Textfield1, Null); this.add (Textfield2, null); this.add (Textfield3, null); this.add Textarea1, null; this.add (label2, null); this.Add (label4, null); this.Add (label3, null); this.add (Button2, null); this.Add (Button1, null); } void button2_actionperformed (ActionEvent E) {textfield1.settext (""); textfield2.settext (""); textfield3.settext (""); textarea1.setText ("");} void button1_actionperformed ( ActionEvent E) {String to, from, Subject, Message, attachment; from = "toone@mail.com"; to = textfield1.getText (); if (to.trim () .Equals (")) {JOTIONPANE. ShowMessageDialog (this, "Received", "wrong", joptionpane.error_message); return;} message = textarea1.getText (); attachment = textfield3.getText (); if (message.trim () .Equals ("") && attachment.trim () .Equals (")) {JOPANE.SHOWMESSAGEDIALOG (this," content and attachments can not be empty! "," Error ", JOPANE.ERROR_MESSAGE);} if (to. IndexOf ("@") == - 1) {JOTIONPANE.SHOWMESSAGEDIALOG (this, "Invalid Record Address!", "Error", JOPANE.Error_Message);

Return;} Subject = textfield2.gettext () .trim (); if (Subject.Equals (")) IF (this," Do you not need to set the topic? ", System Tips, 0) ! = 0) Return; file file = new file (attachment); if (! Attachment.equals (")) {if (! File.isfile ()) {joptionpane.showMessageDialog (this," invalid attachment name! " , "Error", JOPANE.ERROR_MESSAGE); RETURN;}} // The above program is the validity of the input // Create Some Properties and get the default session property props = system.getproperties (); Props.Put ("Mail. SMTP.HOST "," 192.168.0.1 "); session session = session.getDefaultInstance (props, null); session.setdebug (false); tryler msg = new mimeMessage (session); msg.setFrom (new InternetAddress (from)); InternetAddress [] address = {new InternetAddress (to)}; msg.setRecipients (Message.RecipientType.TO, address); msg.setSubject (subject); msg.addHeader ( "toone", " Fangjianhua "); if (attachment.equals (")) {system.out.println ("this is plain mail"); msg.settext (message);} else {system.out.println ("this is a multipart "); // c reate and fill the first message part MimeBodyPart mbp1 = new MimeBodyPart (); mbp1.setText (message); // create the second message part MimeBodyPart mbp2 = new MimeBodyPart (); // attach the file to the message FileDataSource fds = new FileDataSource (file); mbp2.setDataHandler (new DataHandler (fds)); mbp2.setFileName (fds.getName ()); // create the Multipart and its parts to it Multipart mp = new MimeMultipart (); mp.addBodyPart (mbp1) MP.AddBodypart (MBP2); // add the multipart to the message msg.setcontent (mp);} msg.setSentDate (new date ()); // send the message // for (int i = 0; i < 10; i

) Transport.send (MSG); //system.out.println ("Send a mail success"); JOPTIONPANE.SHOWMESSAGEDIALOG (THIS, "Mail Send Success", "System Tips", JOPANE.INFORMATION_MESSAGE);} catch Exception ex) {JOPANE.SHOWMESSAGEDIALOG (this, "Send Mail Failure", "Error", JOPANE.EROR_MESSAGE);}}}

9CBS certified blog expert

Blog expert

Huawei old employees

Big Data

More than ten years, in Huawei, Internet Corporation's experience, profound understanding and practical experience in CRM, big data, mainly sharing various project experience, including architecture, Java, big data and other articles

转载请注明原文地址:https://www.9cbs.com/read-128949.html

New Post(0)