Mail sending program based on JavaMail package ---- Kangkang

xiaoxiao2021-03-05  24

JavaMail API

It is an optional package (standard extension) for reading, writing, and sending electronic messages, which can be used to establish a standard-based email client, which is equipped with a variety of Internet mail associations, including

SMTP

,

POP

,

IMAP

with

Mime

Also included

NNTP

,

S / MIME

And other agreements. Usually develop

JavaMail

The program also needs

Sun

of

JavaBeans Activation Framework

(

Jaf

).

JavaBeans Activation Framework

The operation is very complicated, and it is simple to say.

JavaMail

The operation must depend on its support, such as

JavaMail

use

JavaBeans Activation Framework

Handle

Mime

Encoded email attachment.

JavaMail

Interface is included

Javax.mail

In their sub-bags,

JavaBeans Activation Framework

Interface is included

Javax.actiVation

in the bag,

Jaf

Specification is

"Glasgow" JavaBeans

Part of the specification, about

Jaf

For more details, please refer to

Jaf

guide:

http://java.sun.com/beans/glasgow/jaf.html

The core JavaMail API consists of seven classes: Session, Message, Address, Authenticator, Transport, Store and Folder, all from Javax.mail, JavaMail API top package. You can use these classes to complete a large number of common email tasks, including send messages, retrieve messages, delete messages, authentication, reply messages, forward messages, management attachments, and handle messages based on HTML file formats and lists. For more information on JavaMail, please refer to the JavaMail Guide:

http://java.sun.com/products/javamail/

Similar to the SENDMAIL program implemented with Javamail, there are many online. But the author discovered a little problem. My Code is below. Please determine if you have already installed JDK1.4 Javamial 1.3.2 and JAF. If you don't have installation, please install as follows:

1 Get the latest JDK from www.java.sun.com website. Then set the Java environment variable.

If you are Win95 / 98, add the following 3-line statement to the last side of /autoexec.bat:

Java_Home = C: /J2SDK1.4.2

Path =% java_home% / bin;% PATH%

Classpath =.;% Java_home% / lib

Optimistic in classpath ".", This represents the current directory, many people HelloWorld has not running most of this reason.

If it is Win2000 or XP, use the mouse to right click "My Computer" -> Properties -> Advanced -> Environment Variable

System Variables -> New -> Variable Name: Java_Home Variable: C: /J2SDK1.4.2

System Variables -> Edit -> Variable Name: Path in the foresia of variable value:% java_home% / bin;

System Variables -> New -> Variable Name: ClassPath Variable:.;% Java_Home% / LIB / Tools.jar;% Java_Home% / lib / dt.jar;

The one in front of ClassPath "." Is the same.

Please regulate a HelloWorld to verify that the installation is successful? 2 http://java.sun.com/products/javamail/ Download the latest JavaMail.

Set classpath =% classpath%; c: /download/javamail-1.3.2/mail.jar ;.

3 http://java.sun.com/products/javabeans/glasgow/jaf.html Download JAF.

Set classpath =% classpath%; c: /download/activation/activation.jar

4 ok. It can finally breathe first. Next start the program part. Change the IF ("########") == false) Return; in the ######################################## Reverse IF ("#######") == false) Return; in ##### to write to a local file path. It can also be empty. For more people learn, please don't modify the sendmail@163.com password: Sendcode This mailbox is only used in program testing! !

Then, in DOS

Javac Sendmail.java

Java Sendmail

Oh, it is successful! ! ! My host DOS display

D: /> Java Sendmail

Set system properties: mail.smtp.host = SMTP.163.com

Ready to get the mail session object!

Ready to create a MIME mail object!

Set SMTP identity authentication: mail.smtp.auth = TRUE

Set the email topic!

Set the mail format

Settler

Set the sender!

Add Mail Accessories: D: /sendmail.java

Program gets username and password

Sending e-mail....

Send mail success!

*********************************************************** **********************

/ **

* @Author Kangkang

* http://corncc.ikdiy.com

* 2005.4.14

* /

Import java.util. *;

Import javax.mail. *;

Import javax.mail.internet. *;

Import java.util.date;

Import javax.activation. *;

Import java.io. *;

Public Class Sendmail

{

// 21-30 Rows define the variables used in this program. They are constructed in main.

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 (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 ("Preparing 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 (); // MP a multipart object

// Multipart is a container That Holds Multiple Body PARTS.

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)

{

System.out.println ("Program Get User Name and Password");

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

{

System.out.println ("Set Mail Format");

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)

{

System.out.println ("Settlement");

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) {

System.out.println ("Send Accessories");

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 (MIMEMSG, MIMEMSG.GETRECIPIENTS (Message.RecipientType.to);

//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 = "Hello. This is a testing."

Sendmail themail = new sendmail ("SMTP.163.com");

THEMAIL.SETNEEDAUTH (TRUE);

IF ("DKJJF") == false) return;

IF (amail.setbody == false) return;

IF ("########") == false) return;

IF ("SENFMAIL@163.com") == false) return;

IF ("#######") == false) Return; // Absolute path on the local machine

Themail.setNamePass ("Sendmail", "JavaCode"); // Username and Password

IF (themail.sendout () == false) return;

}

}

*********************************************************** ********************** TIPS:

The front of the A program is just an abstract definition. A specific parameter is given in the main function.

B Use the system.out.Println observation process.

C Read JavaMaill / DOCS / Javadocs / INDEX.html

转载请注明原文地址:https://www.9cbs.com/read-35765.html

New Post(0)