Summary of JavaMail Operations (4)

Source: Internet
Author: User
Tags date auth mail readline
This piece of code is used to send messages with HTML information
void Jbutton8_actionperformed (ActionEvent e) {
Try
{
Properties Props = new properties ();
Authenticator auth = new Email_autherticator ();
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth);
MimeMessage message = new MimeMessage (session);
Set the message body
String htmltext= "< h1>helloMessage.setcontent (HTMLText, "text/html");//text/plain set the format of the message because it has an HTML document, the text/html format

Message.setsubject (Mail_subject);
Message.settext (mail_body); When using message.setcontent (), it seems impossible to use Message.settext ()
Message.setheader (Mail_head_name,mail_head_value);
Message.setsentdate (New Date ());
Address = new InternetAddress (Mail_from, "sunxiaoming");
Message.setfrom (address);
Address toaddress = new internetaddress (mail_to);
Message.addrecipient (message.recipienttype.to,toaddress);
Transport.send (message);
System.out.println ("Send ok!");
}
catch (Exception ex)
{
System.out.println ("Faild" +ex);
}

}

This piece of code is used to send messages with pictures
void Jbutton9_actionperformed (ActionEvent e) {
Try
{
String file = "./20020423/test.gif";
Properties Props = new properties ();
Authenticator auth = new Email_autherticator ();

Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth);
MimeMessage message = new MimeMessage (session);

Message.setsubject ("Embedded Image");
Address = new InternetAddress (Mail_from, "sunxiaoming");
Message.setfrom (address);
Address toaddress = new internetaddress (mail_to);
Message.addrecipient (message.recipienttype.to,toaddress);
Message.setheader (Mail_head_name,mail_head_value);
Message.setsentdate (New Date ());

BodyPart Messagebodypart = new MimeBodyPart ();
String HTMLText = "< h1>Messagebodypart.setcontent (HTMLText, "text/html");
The principle is the same as sending attachments, the message is considered to be composed of several parts, first set each part, and then all of them together.
Create a related multi-part to combine the parts
Mimemultipart multipart = new Mimemultipart ("related");
Multipart.addbodypart (Messagebodypart);

Create part for the image
Messagebodypart = new MimeBodyPart ();

Fetch the image and associate to part
DataSource FDS = new Filedatasource (file);
Messagebodypart.setdatahandler (New DataHandler (FDS));
Messagebodypart.setheader ("Content-id", "Memememe");

ADD part to Multi-part
Multipart.addbodypart (Messagebodypart);

Associate multi-part with Message
Message.setcontent (multipart);

Transport.send (message);
System.out.println ("Send ok!");
}
catch (Exception ex)
{
System.out.println ("Faild" +ex);
}

}

This program is used to accept messages with HTML information
void Jbutton7_actionperformed (ActionEvent e) {
Try
{
Properties props = System.getproperties ();
Authenticator auth = new Email_autherticator ();
Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth);
Store store = Session.getstore ("POP3");
Store.connect (Host,username,password);

After connecting to the store,you can get a Folder,which must is opened before you can read messages from it:
Folder folder = Store.getfolder ("INBOX");
Folder.open (Folder.read_write);
Message message[] = Folder.getmessages ();

for (int i=0,n=message.length;i//for (int i=0,n=1;i {
String Out_from_person = ((internetaddress) message[i].getfrom () [0]). getpersonal ();
String out_from_address = ((internetaddress) message[i].getfrom () [0]). getaddress ();
System.out.println ("From:" +out_from_person+ ")";
SYSTEM.OUT.PRINTLN ("Address:" +out_from_address+ ");

String out_subject = Message[i].getsubject ();
System.out.println ("Subject:" +out_subject+ ")";

Get information about a message and manipulate each part of it
Part messagepart = Message[i];
Object out_content = Messagepart.getcontent ();
if (out_content instanceof Multipart)
{
MessagePart = ((Multipart) out_content). Getbodypart (0);
System.out.println ("[Multipart message]");
}
String Out_content_type = Messagepart.getcontenttype ();
System.out.println ("CONTENT:" +out_content_type);

Jeditorpane1.settext ("");//show it on Jeditorpanel
Jeditorpane1.setcontenttype ("text/html");
Jeditorpane1.settext ((String) out_content);
Jeditorpane1.seteditable (FALSE);

/*
if (Out_content_type.startswith ("Text/plain") | | | out_content_type.startswith ("text/html")
{
InputStream ipstm = Messagepart.getinputstream ();
BufferedReader bufreader = new BufferedReader (new InputStreamReader (ipstm));
String thisline = Bufreader.readline ();
while (Thisline!= null)
{
System.out.println ("Thisline:" +thisline);
Thisline = Bufreader.readline ();
}
}
*/
System.out.println ("------------------------------------------------------------");
Message[i].setflag (flags.flag.deleted,true);
}
Folder.close (TRUE);
Store.close ();
}
catch (Exception ej2)
{
System.out.println (EJ2);
}

}
 

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.