高分求解: 有关发送email的问题
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
/**can't use setText and setHtml n the same time;otherwise,
* the image in htmlcode that setHtml() set
* can't be showed normally;
*/
public class Mail {
/**smtpHost is used to identify a smtpServer that used to send mail
*/
private String smtpHost = null;
/**from is used to identify mail Sender.
*/
private String from = null;
/**to is used to identify mail Receivers,they are divided by ",".
*/
private String to = null;
/**cc is used to identify Receivers who receive Carbon Copy,
* they are divided by ",".
*/
private String cc = null;
/**bcc is used to identify Receivers who receive Blind Carbon Copy,
* they are divided by ",".
*/
private String bcc = null;
/**subject is used to identify the Subject of this Mail.
*/
private String subject = null;
/**text is used to identify text Content of this Mail.
*/
private String text = null;
/**html is used to identify html Content of this Mail.
*/
private String html = null;
/**img is used to identify aimages that enbeded in Html Content,
* they are divided by ",".
*/
private String img = null;
/**attachment is used to identify attachments that this Mail Contain,
* they are divided by ",".
*/
private String attachment = null;
/**parts is used to identify which part(text,html,img,attachment) this Mail contain.
*/
private String parts = "";
/**mailMulPart contain all content in this mail,it will be invoked by
* message.setContent(mailMulPart).
*/
private MimeMultipart mailMulPart = new MimeMultipart();
/**vectorMimeBodyPart contains all MimeBodyParts of this Mail.
*/
private Vector vectorMimeBodyPart = new Vector();
//MimeMessage message = new MimeMessage(
public Mail(){
}
public void setSmtpHost(String smtpHost){
this.smtpHost = smtpHost;
}
public void setFrom(String from){
this.from = from;
}
public void setTo(String to){
this.to = to;
}
public void setCC(String cc){
this.cc = cc;
}
public void setBCC(String bcc){
this.bcc = bcc;
}
public void setText(String text){
this.text = text;
}
public void setHtml(String html){
this.html = html;
}
public void setImg(String img){
this.img = img;
}
public void setSubject(String subject){
this.subject = subject;
}
public void setAttachment(String attachment){
this.attachment = attachment;
}
public boolean send(){
boolean mailStatus = false;
if(smtpHost == null || smtpHost.trim().length() <1 || to == null || to.trim().length() <1 || from == null || from.trim().length() <1 || subject == null || subject.trim().length() <1){
//System.out.println("MailClass send() Exception : Mail Send Failed! Make true smtpHost,from,to,subject have been set!");
return mailStatus;
}
Properties p = new Properties();
p.put("mail.smtp.host",smtpHost);
Session mailSession = Session.getInstance(p,null);
MimeMessage message = new MimeMessage(mailSession);
try {
mailMulPart.setSubType("related");
Address fromAdr = new InternetAddress(from);
Address[] toAdr = InternetAddress.parse(to);
for(int i=0;i<toAdr.length;i++){
//System.out.println(toAdr[i].toString());
}
if(cc != null && cc.trim().length() > 0){
Address[] ccAdr = InternetAddress.parse(cc);
message.setRecipients(message.RecipientType.CC,ccAdr);
for(int i=0;i<ccAdr.length;i++){
//System.out.println(ccAdr[i].toString());
}
}
if(bcc != null && bcc.trim().length() > 0){
Address[] bccAdr = InternetAddress.parse(bcc);
message.setRecipients(message.RecipientType.BCC,bccAdr);
for(int i=0;i<bccAdr.length;i++){
//System.out.println(bccAdr[i].toString());
}
}
message.setFrom(fromAdr);
message.setRecipients(message.RecipientType.TO,toAdr);
message.setSubject(subject);
if(text != null && text.trim().length()>0){
parts = "text";
//message.setText(text);
}
if(html != null && html.trim().length()>0){
parts = parts + ";html";
}
if(img != null && img.trim().length()>0){
parts = parts + ";img";
}
if(attachment != null && attachment.trim().length()>0){
parts = parts + ";attachment";
}
StringTokenizer stParts = new StringTokenizer(parts,";");
while(stParts.hasMoreTokens()){
String tempStParts = stParts.nextToken();
//mailBodyPart[i] = new MimeBodyPart();
if(tempStParts.equals("text")){
MimeBodyPart singleMailBodyPart = new MimeBodyPart();
singleMailBodyPart.setContent(text,"text/plain;charset=gb2312");
vectorMimeBodyPart.addElement(singleMailBodyPart);
}
else if(tempStParts.equals("html")){
MimeBodyPart singleMailBodyPart = new MimeBodyPart();
singleMailBodyPart.setContent(html,"text/html;charset=gb2312");
vectorMimeBodyPart.addElement(singleMailBodyPart);
}
else if(tempStParts.equals("img")){
StringTokenizer stImg = new StringTokenizer(img,",");
while(stImg.hasMoreTokens()){
String tempStImg = stImg.nextToken();
MimeBodyPart singleMailBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(tempStImg);
//singleMailBodyPart.setFileName("ddddd.jpg");
//singleMailBodyPart.setText("美丽图片");
singleMailBodyPart.setDataHandler(new DataHandler(fds));
singleMailBodyPart.setHeader("Content-ID",fds.getName());
vectorMimeBodyPart.addElement(singleMailBodyPart);
//mailBodyPart[i].setContent(html,"text/html;charset=gb2312");
}
}
else if(tempStParts.equals("attachment")){
StringTokenizer stAttachment = new StringTokenizer(attachment,",");
while(stAttachment.hasMoreTokens()){
String tempStAttachment = stAttachment.nextToken();
MimeBodyPart singleMailBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(tempStAttachment);
singleMailBodyPart.setDataHandler(new DataHandler(fds));
singleMailBodyPart.setFileName(fds.getName());
vectorMimeBodyPart.addElement(singleMailBodyPart);
//mailBodyPart[i].setDataHandler(new DataHandler(fds));
//mailBodyPart[i].setFileName("j.jpg");
}
}
}
MimeBodyPart[] mailBodyPart = new MimeBodyPart[vectorMimeBodyPart.size()];
for(int i=0;i<vectorMimeBodyPart.size();i++){
mailMulPart.addBodyPart((MimeBodyPart)vectorMimeBodyPart.get(i));
}
message.setContent(mailMulPart);
message.setSentDate(new Date());
Transport.send(message);
mailStatus = true;
}
catch (Exception ex) {
//System.out.println("------------------Exception :---------------");
//ex.printStackTrace();
//System.out.println("------------------Exception :---------------");
}
return mailStatus;
}
}

