/ *
In Java, I often see someone asking how to send mail with JavaMail? How to receive mail? How to access multiple folders and so on. The problem is scattered, and the history of history has already submerged in the ocean of the problem.
I have done a Java project before, which contains a webmail feature. At the beginning of the Java implementation, I have a bit gain for JavaMail. Seeing the regular problems in the forum, therefore put some of my experience, I hope to have some help.
This article only introduces the use of JavaMail to send mail features, which involve SMTP authentication, mail attachments, and HTML content mail.
Other content related to multi-mail box, receive POP3 mail and IMAP, will be introduced in subsequent articles.
The following procedures are required: JavaMail, JAF package, J2EE.jar contains the above two packages, it is recommended that you install J2SDKee or copy J2EE.jar directly, add it to the 2nd of JBuilder, or system ClassPath
* /
Package com.me.util.mail;
/ **
* @author zhangkun aistill@msn.com
* @version 1.0
* /
Import java.util. *;
Import javax.mail. *;
Import javax.mail.internet. *;
Import java.util.date;
Import javax.activation. *;
Import java.io. *;
Import com.me.util. *;
Public class sendmail {
Private mimeMessage mimemsg; // mime mail object
Private session session; // mail session object
Private Properties ProPs; // System Attributes
Private Boolean Needauth = false; // SMTP needs to be authenticated
Private string username = ""; // SMTP authentication user name and password
Private string password = ""
Private Multipart MP; // Multipart object, email content, title, attachment, etc. are added to the MIMEMESSAGE object.
/ **
*
* /
Public sendmail () {
Setsmtphost (getConfig.mailhost); // If you do not specify a mail server, get from the getConfig class
CreateMimeMessage ();
}
Public sendmail (string smtp) {
Setsmtphost (SMTP);
CreateMimeMessage ();
}
/ **
* @Param Hostname String
* /
Public void setsmtphost (string hostname) {
System.out.println ("Setting System Properties: mail.smtp.host =" Hostname);
IF (PrOPS == NULL) PROPS = system.getproperties (); // Get system properties object
Props.put ("mail.smtp.host", hostname); // Set SMTP host
}
/ **
* @Return Boolean
* /
Public Boolean CreatemimeMessage ()
{
Try {
System.out.println ("Prepare for Meeting Session Objects!"); Session = session.getDefaultInstance (props, null); // Get mail session object
}
Catch (Exception E) {
System.err.println ("Errors when getting a mail session object!" E);
Return False;
}
System.out.println ("Preparing to create MIME mail objects!");
Try {
MIMEMSG = new mimeMessage (session); // Create a MIME mail object
MP = new mimemultipart ();
Return True;
}
Catch (Exception E) {
System.err.println ("Create MIME E-mail Object Failed!" E);
Return False;
}
}
/ **
* @Param Need Boolean
* /
Public void setneedAuth (Boolean NEED) {
System.out.println ("Sets SMTP Identity Certification: mail.smtp.auth =" NEED);
IF (PrOPS == NULL) PROPS = system.getproperties ();
IF (NEED) {
Props.Put ("mail.smtp.auth", "true");
} else {
Props.Put ("mail.smtp.auth", "false");
}
}
/ **
* @Param Name String
* @Param Pass String
* /
Public void setNamePass (String name, string pass) {
Username = name;
Password = pass;
}
/ **
* @Param Mailsubject String
* @Return Boolean
* /
Public Boolean Setsubject (String Mailsubject) {
System.out.println ("Set Mail Topics!");
Try {
MIMEMSG.SETSUBJECT (MAILSUBJECT);
Return True;
}
Catch (Exception E) {
System.err.println ("Setting the Mail Topic An error!");
Return False;
}
}
/ **
* @Param Mailbody String
* /
Public Boolean SetBody (String Mailbody) {
Try {
Bodypart bp = new mimebodypart ();
BP.SETCONTENT ("" MailBody, "text / html; charSet = GB2312");
mp.addbodypart (bp);
Return True;
}
Catch (Exception E) {
System.err.println ("Errors when setting the message body!" E);
Return False;
}
}
/ **
* @Param Name string * @Param pass string
* /
Public Boolean AddFileAffix (String FileName) {
System.out.println ("Add Mail Accessories:" FileName);
Try {
Bodypart bp = new mimebodypart ();
FileDataSource FileDs = New FileDataSource (filename);
BP.SetDataHandler (New DataHandler (Fileds));
bp.setfilename (fileds.getname ());
mp.addbodypart (bp);
Return True;
}
Catch (Exception E) {
System.err.Println ("Add Mail Accessories:" FileName "error!" E);
Return False;
}
}
/ **
* @Param Name String
* @Param Pass String
* /
Public Boolean SetFrom (String from) {
System.out.println ("Set the sender!");
Try {
MimemSg.SetFrom (New Internet ADRESS (FROM)); // Setting sender
Return True;
}
Catch (Exception E)
{Return False;}
}
/ **
* @Param Name String
* @Param Pass String
* /
Public boolean setto (string to) {
IF (to == null) Return False;
Try {
MimeMsg.seTrecipients (Message.RecipientType.to, Internetdress.Parse (to));
Return True;
}
Catch (Exception E)
{Return False;}
}
/ **
* @Param Name String
* @Param Pass String
* /
Public Boolean SetCopyTo (String Copyto)
{
IF (COPYTO == NULL) RETURN FALSE;
Try {
MIMEMSG.SetRecipients (Message.RecipientType.cc, (address []) InternetdRESS.PARSE (COPYTO);
Return True;
}
Catch (Exception E)
{Return False;}
}
/ **
* @Param Name String
* @Param Pass String
* /
Public Boolean Sendout ()
{
Try {
MIMEMSG.SETCONTENT (MP);
Mimemsg.savechanges ();
System.out.println ("Sending Mail ....");
Session MailSession = session.getInstance (Props, NULL);
Transport Transport = Mailsession.getTransport ("SMTP");
Transport.connect ((String) Props.get ("mail.smtp.host"), username, password); transport.sendMessage (mimsg, mimemsg.getrecipients);
//Transport.send(mimemsg);
System.out.println ("Send Mail Success!");
Transport.close ();
Return True;
}
Catch (Exception E)
{
System.err.println ("Mail Send Failed!" E);
Return False;
}
}
/ **
* Just do it as this
* /
Public static void main (String [] args) {
String mailbody = ""
"
Sendmail themail = new sendmail ("smtp.msn.com");
THEMAIL.SETNEEDAUTH (TRUE);
IF ("THEMAIL.SETSUBJECT (" Title ") == false) return;
IF (amail.setbody == false) return;
IF ("Gates@msn.com") == false) Return;
IF ("Bill@msn.com") == false) return;
IF ("C: //Boot.ini") == false) Return;
Themail.setNamePass ("User", "Password");
IF (themail.sendout () == false) return;
}
}