e-mail with javamail (including accessories), with JBUILDER3 implementation

Source: Internet
Author: User
Tags empty gettext mail return trim
Send mail//title:your Product Name
Version:
Copyright:copyright (c) 1999
Author:your Name
Company:your Company
Description:your Description

Package Mail;

Import java.awt.*;
Import javax.swing.*;
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) {
System.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 ("addressee");
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 ("subject");
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 ("annex");
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 ("Re-filling");
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 ("")) {
Joptionpane.showmessagedialog (This, "the addressee cannot be empty!", "error", joptionpane.error_message);
Return
}
Message=textarea1.gettext ();
Attachment=textfield3.gettext ();
if (Message.trim (). Equals ("") &&attachment.trim (). Equals ("")) {
Joptionpane.showmessagedialog (This, "contents and attachments cannot all be empty!", "error", joptionpane.error_message);
Return
}
if (To.indexof ("@") ==-1) {
Joptionpane.showmessagedialog (this, "Invalid addressee address!", "error", joptionpane.error_message);
Return
}
Subject=textfield2.gettext (). Trim ();
if (Subject.equals (""))
If Joptionpane.showconfirmdialog (this, "You don't need to set a theme?") "," system hint ", 0)!=0)
Return
File File=new file (attachment);
if (!attachment.equals ("")) {
if (!file.isfile ()) {
Joptionpane.showmessagedialog (this, "Invalid attachment name!", "error", joptionpane.error_message);
Return
}
}
The above procedure is to verify the validity of the input

Create some properties and get the default session
Properties props = System.getproperties ();
Props.put ("Mail.smtp.host", "192.168.0.1");
Session session = Session.getdefaultinstance (props, null);
Session.setdebug (FALSE);

try{
Create a message
MimeMessage 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 mail");
Create and fill the
MimeBodyPart MBP1 = new MimeBodyPart ();
Mbp1.settext (message);

Create the second message part
MimeBodyPart MBP2 = new MimeBodyPart ();

Attach the "file to" 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, "Send mail Successfully", "system hint", joptionpane.information_message);
}
catch (Exception ex) {
Joptionpane.showmessagedialog (This, "Send Mail Failed", "error", joptionpane.error_message);
}

}
}


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.