Summary of JavaMail Operations (1)

Source: Internet
Author: User
Tags date auth html form mail
In the collation of the user's article, found a javamail summary, hereby thank The_east_key, and announced to you, I hope to help you, the full text as follows:
This article is about:
Send regular mail, accept normal mail
Send a message with an attachment and receive a message with an attachment
Send HTML form of message, accept HTML form of message
[b[send a message with pictures [/b], etc. made a summary.
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Import com.borland.jbcl.layout.*;
Import javax.mail.*;
Import java.util.*;
Import javax.mail.internet.*;
Import java.io.*;
Import javax.activation.*;


Public String host= "smtp.163.com";
Public String username= "ABCDEFG";
Public String password= "ABCDEFG";
Public String mail_head_name= ' This is head ' this mail;
Public String mail_head_value= ' This is head ' this mail;
Public String mail_to= "xyz@163.com";
Public String mail_from= "abcdefg@163.com";
Public String mail_subject= ' This is the subject to this Test mail ';
Public String mail_body= ' This is the mail_body to this test mail ';
This piece of code is used to send regular e-mail messages
void Jbutton1_actionperformed (ActionEvent e) {
Try
{
Properties Props = new properties ()//Get system environment
Authenticator auth = new Email_autherticator ()//mail Server user authentication

Props.put ("Mail.smtp.host", host);
Props.put ("Mail.smtp.auth", "true");
Session session = Session.getdefaultinstance (Props,auth);
Sets the session and communicates with the mail server.
MimeMessage message = new MimeMessage (session);
Message.setcontent ("Hello", "Text/plain");//Set Message format
Message.setsubject (mail_subject);//Set Message subject
Message.settext (mail_body);//Set message body
Message.setheader (Mail_head_name,mail_head_value);//Set message headers
Message.setsentdate (new Date ());//set Mail send date

Address = new InternetAddress (Mail_from, "sunxiaoming");
Message.setfrom (address); Set the address of the sender of the message

If you want to set up multiple parameters for the sender of the message, you can use the following statement
Address address[] = {new InternetAddress ("sunxm@oaklet.co.jp", "Sunxmatoaklet"), New InternetAddress (" Firstsxm@hotmail.com "," Sunxmathotmail ")};
Message.addfrom (address);

Address toaddress = new internetaddress (mail_to);//Set addresses of message recipients
Message.addrecipient (message.recipienttype.to,toaddress);
Address ccaddress = new InternetAddress ("firstsxm@hotmail.com")//Set addresses of CC of the message
Message.addrecipient (message.recipienttype.cc,ccaddress);


Transport.send (message);//Send mail
///To get a specific instance out of session for your Protocol.pass along the username and password
(blank if unnecessary). Send the Message,and close the connection;
Message.savechanges ();
Transport transport = Session.gettransport ("SMTP");
Transport.connect (Host,username,password);
Transport.sendmessage (Message,message.getallrecipients ());
Transport.close ();
*/
System.out.println ("Send ok!");
}
catch (Exception ex)
{
System.out.println ("Faild" +ex);
}
}

 

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.