Summary of JavaMail Operations (4)

Source: Internet
Author: User
Tags auth
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");

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.