Import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
/**
* <p>title: Using JavaMail to send mail </p>
* <p>description: Demonstrates how to use the JavaMail package to send e-mail. This example can send multiple attachments </p>
* <p>copyright:copyright (c) 2003</p>
* <p>Filename:Mail.java</p>
* @version 1.0
*/
public class Mail {
String to = "";//Recipient
String from = "";/sender
String host = "";//smtp host
String username = "";
String password = "";
String filename = "";//attachment file name
String subject = "";//Message subject
String content = "";//Message body
vector file = new vector ()//Attachment file Collection
/**
*<br> method Description: Default Builder
*<br> Input Parameters:
*<br> return type:
*/
public Mail () {
}
/**
*<br> Method Description: constructor, provide direct parameter incoming
*<br> input Parameters:
*<br> return type:
*/
public Mail (String to,string from,string smtpserver,string username,string password,string content) {
this.to = to;
this.from = from;
this.host = SmtpServer;
this.username = Username;
This.password = password;
this.subject = subject;
this.content = content;
}
/**
*<br> Method Description: Set mail server address
*<br> input parameter: String host Mail server address name
*<br> return type:
*/
public void Sethost (String host) {
this.host = host;
}
/**
*<br> method Description: Set login server checksum password
*<br> input Parameters:
*<br> return type:
*/
public void SetPassword (String pwd) {
This.password = pwd;
}
/**
*<br> Method Description: Set login server verify user
*<br> input Parameters:
*<br> return type:
*/
public void Setusername (String USN) {
this.username = USN;
}
/**
*<br> Method Description: Set mail send destination mailbox
*<br> input Parameters:
*<br> return type:
*/
public void Setto (String to) {
this.to = to;
}
/**
*<br> Method Description: Set the message to send the source mailbox
*<br> input Parameters:
*<br> return type:
*/
public void Setfrom (String from) {
this.from = from;
}
/**
*<br> Method Description: Set the message subject
*<br> input Parameters:
*<br> return type:
*/
public void Setsubject (String subject) {
this.subject = subject;
}
/**
*<br> Method Description: Set the message content
*<br> input Parameters:
*<br> return type:
*/
public void SetContent (String content) {
this.content = content;
}
/**
*<br> Method Description: Convert the theme to Chinese
*<br> input parameter: String strText
*<br> return type:
*/
public string Transferchinese (string strText) {
try{
StrText = Mimeutility.encodetext (New String (Strtext.getbytes (), "GB2312"), "GB2312", "B");
}catch (Exception e) {
E.printstacktrace ();
}
return strText;
}
/**
*<br> Method Description: Add attachments to the attachment
*<br> input Parameters:
*<br> return type:
*/
public void Attachfile (String fname) {
file.addelement (fname);
}
/**
*<br> Method Description: Send mail
*<br> input Parameters:
*<br> return Type: Boolean success is true and vice versa false
*/
public boolean SendMail () {
//Construction mail session
Properties props = System.getproperties ();
props.put ("Mail.smtp.host", host);
props.put ("Mail.smtp.auth", "true");
session session=session.getdefaultinstance (props, New Authenticator () {
public passwordauthentication getpasswordauthentication () {
return new Passwordauthentication (Username,password);
}
});
try {
Constructs the mimemessage and sets the basic value
mimemessage msg = new MimeMessage (session);
Msg.setfrom (New InternetAddress (from));
internetaddress[] address={new internetaddress (To)};
msg.setrecipients (message.recipienttype.to,address);
subject = Transferchinese (subject);
Msg.setsubject (subject);
//Construction multipart
Multipart MP = new Mimemultipart ();
//Add body to multipart
MimeBodyPart mbpcontent = new MimeBodyPart ();
Mbpcontent.settext (content);
//Add to MimeMessage (multipart representative body)
Mp.addbodypart (mbpcontent);
//Add attachments to multipart
enumeration efile=file.elements ();
while (efile.hasmoreelements ()) {
MimeBodyPart mbpfile = new MimeBodyPart ();
Filename=efile.nextelement (). toString ();
Filedatasource FDS = new Filedatasource (filename);
Mbpfile.setdatahandler (New DataHandler (FDS));
Mbpfile.setfilename (Fds.getname ());
//Add to MimeMessage (multipart representative attachment)
Mp.addbodypart (Mbpfile);
}
file.removeallelements ();
//Add MimeMessage to multipart
Msg.setcontent (MP);
msg.setsentdate (New Date ());
//Send mail
transport.send (msg);
} catch (Messagingexception Mex) {
Mex.printstacktrace ();
Exception ex = null;
if ((Ex=mex.getnextexception ())!=null) {
Ex.printstacktrace ();
}
return false;
}
return true;
}
/**
*<br> Method Description: Main method, for testing
*<br> input Parameters:
*<br> return type:
*/
public static void Main (string[] args) {
Mail sendmail = new mail ();
sendmail.sethost ("smtp.sohu.com");
sendmail.setusername ("Du_jiang");
Sendmail.setpassword ("31415926");
Sendmail.setto ("dujiang@sricnet.com");
sendmail.setfrom ("du_jiang@sohu.com");
sendmail.setsubject ("Hello, this is the test!") ");
sendmail.setcontent ("Hello this is a test with multiple attachments!") ");
//mail sendmail = new Mail ("dujiang@sricnet.com", "du_jiang@sohu.com", "smtp.sohu.com", "Du_jiang", "31415926", "Hello "The stomach, how are you?" ");
sendmail.attachfile ("C:\\Test.txt");
sendmail.attachfile ("Dnd.jar");
Sendmail.sendmail ();
}
}//end