sendMail

/*

此篇?介?用javamail???送?件功能,此中涉及smtp??,?件附件?送,及HTML?容?件等。

其他有?多?箱的??,接管POP3?件及IMAP等?容。

如下程式必要:javamail,JAF包,j2ee.jar包孕了上述??包,建?大年夜家安?J2SDKEE或直接拷?j2ee.jar,?其添加到jbuilder的library中,

或系?ClassPath中

*/

/**

* @author Haley haley_hj@163.com

* @QQ:87823050

* @homepage:http://haley.yeah.net

* @12/18/2002

*/

import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import java.util.Date;

import javax.activation.*;

import java.io.*;

public class sendMail {

private MimeMessage mimeMsg; //MIME?件物件

private Session session; //?件??物件

private Properties props; //系??性

private boolean needAuth = false; //smtp是否必要??

private String username = ""; //smtp??用?名和密?

private String password = "";

private Multipart mp; //Multipart物件,?件?容,??,附件等?容均添加到此中後再天生MimeMessage物件

public sendMail() {

setSmtpHost("172.24.15.228");//指定?件伺服器

createMimeMessage();

}

public sendMail(String smtp){

setSmtpHost(smtp);

createMimeMessage();

}

/**

* @param hostName String

*/

public void setSmtpHost(String hostName) {

System.out.println("?置系??性:mail.smtp.host = "+hostName);

if(props == null)props = System.getProperties(); //?得系??性物件

props.put("mail.smtp.host",hostName); //?置SMTP主?

}

/**

* @return boolean

*/

public boolean createMimeMessage()

{

try{

System.out.println("???取?件???象!");

session = Session.getDefaultInstance(props,null); //?得?件???象

}

catch(Exception e){

System.err.println("?取?件??物件??生??!"+e);

return false;

}

System.out.println("???建MIME?件?象!");

try{

mimeMsg = new MimeMessage(session); //?建MIME?件物件

mp = new MimeMultipart();

return true;

}

catch(Exception e){

System.err.println("?建MIME?件物件失去!"+e);

return false;

}

}

/**

* @param need boolean

*/

public void setNeedAuth(boolean need) {

System.out.println("?置smtp身份??: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("?置?件主?!");

try{

mimeMsg.setSubject(mailSubject);

return true;

}

catch(Exception e) {

System.err.println("?置?件主??生??!");

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("?置?件正文??生??!"+e);

return false;

}

}

/**

* @param name String

* @param pass String

*/

public boolean addFileAffix(String filename) {

System.out.println("增添?件附件:"+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("增添?件附件:"+filename+"?生??!"+e);

return false;

}

}

/**

* @param name String

* @param pass String

*/

public boolean setFrom(String from) {

System.out.println("?置?信人!");

try{

mimeMsg.setFrom(new InternetAddress(from)); //?置?信人

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,InternetAddress.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[])InternetAddress.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("正在?送?件....");

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("?送?件成功!");

transport.close();

return true;

}

catch(Exception e)

{

System.err.println("?件?送失去!"+e);

return false;

}

}

/**

* Just do it as this

*/

public static void main(String[] args) {

String mailbody = ""+

"

Haley

";

sendMail themail = new sendMail("172.24.15.228");

themail.setNeedAuth(true);

if(themail.setSubject("JavaMail") == false) return;

if(themail.setBody(mailbody) == false) return;

if(themail.setTo("haley@172.24.15.228") == false) return;

if(themail.setFrom("haley@172.24.15.228") == false) return;

if(themail.addFileAffix("c:sqlnet.log") == false) return;

themail.setNamePass("haley","584520");

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

}

}

您可能还会对下面的文章感兴趣: