Send Mail/*
In the Java version, you often see people asking how to send messages with JavaMail. How do I receive mail? How to access multiple folders, and so on. Problems are scattered, and the response to history has long been submerged in the ocean of problems.
I have done a Java project, which contains a webmail function, the original for the use of Java to javamail groping for some time, finally a little harvest. See the Forum often have this aspect of the problem, so put some of my experience to post out, I hope that some help.
This article only describes the use of JavaMail to implement the Send mail function, which involves SMTP authentication, mail attachment sent, and HTML content mail.
Other information about the implementation of multiple mailboxes, receive POP3 mail and IMAP and other content, will be introduced in subsequent articles.
The following program needs: JAVAMAIL,JAF package, J2ee.jar contains the above two packages, we recommend that you install J2sdkee or direct copy J2ee.jar, add it to the JBuilder library, or system classpath
Private session session; Mail Session Object
Private Properties props; System Properties
Private Boolean Needauth = false; Does SMTP require authentication
Private String username = ""; SMTP authenticated user name and password
Private String Password = "";
Private Multipart MP; Multipart objects, message content, headers, attachments, and so on are added to it before the MimeMessage object is generated
/**
*
*/
Public SendMail () {
Setsmtphost (getconfig.mailhost)//If no mail server is specified, obtain from the GetConfig class
Createmimemessage ();
}
Public SendMail (String SMTP) {
Setsmtphost (SMTP);
Createmimemessage ();
}
/**
* @param hostName String
*/
public void Setsmtphost (String hostName) {
SYSTEM.OUT.PRINTLN ("Set system properties: Mail.smtp.host =" +hostname);
if (props = = null) props = System.getproperties (); Get System Properties Object
Props.put ("Mail.smtp.host", hostName); Setting up an SMTP host
}
/**
* @return Boolean
*/
public boolean createmimemessage ()
{
try{
System.out.println ("Ready to get mail Session Object!") ");
Session = Session.getdefaultinstance (props,null); Get mail Session Object
}
catch (Exception e) {
SYSTEM.ERR.PRINTLN ("An error occurred while getting the mail session Object!") "+e);
return false;
}
SYSTEM.OUT.PRINTLN ("Prepare to create MIME mail Object!") ");
try{
mimemsg = new MimeMessage (session); To create a MIME mail object
MP = new Mimemultipart ();
/**
* @param name String
* @param pass String
*/
public boolean addfileaffix (String filename) {
System.out.println ("Add Mail attachment:" +filename);
try{
BodyPart bp = new MimeBodyPart ();
Filedatasource fileds = new Filedatasource (filename);
Bp.setdatahandler (New DataHandler (fileds));
Bp.setfilename (Fileds.getname ());
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.