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);
}
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.