package com.e5systems.hr.commons.javamail;
import java.io.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
/**
* <p>
* Title: MailBean
* </p>
* <p>
* Description: offer send email function(/p>
*
* @version 1.0
*/
class ExtendStrings {
public ExtendStrings() {
}
/**
* trim(String str)
*/
public String CS(String str) {
try {
if (str == null)
return "";
str = str.trim();
if (str == null)
return "";
str = new String(str.getBytes("8859_1"), "GBK");
} catch (Exception e) {
System.out.println(e);
}
return str;
}
}
public class SendEmails {
private String errMsg = "";
private ExtendStrings ExStr = new ExtendStrings();
private String sender = "";// sender
private String smtpHost = "";// send Email server
private String user = ""; // user name
private String password = "";// logon password
private String subject = "";// mail subject
String fileAttachment = "";// attachment
public SendEmails(String user,String password,String sender,String subject,String attachment) {
this.setPropertiesAttri(user,password,sender,subject,attachment);
}
private void setPropertiesAttri(String user,String password,String sender,String subject
,String attachment) {
try {
this.setSmtpHost("smtp.163.com");//(prop.get("SmtpHost").toString());
this.setUser(user);
this.setPassword(password);
this.setSender(sender);
this.setSubject(ExStr.CS(subject));
this.setFileAttachment(attachment.replace('\\','/'));
} catch (Exception ex) {
System.err.println("exception in SendEmail.java:" + ex.toString());
}
}
/** set sender address */
public void setSender(String sender) {
this.sender = sender;
}
public String getSender() {
return sender;
}
/** set send server(smtp) */
public void setSmtpHost(String smtpHost) {
this.smtpHost = smtpHost;
}
public String getSmtpHost() {
return smtpHost;
}
/** set logon user name */
public void setUser(String user) {
this.user = user;
}
public String getUser() {
return user;
}
/** set logon password */
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
/**set mail subject */
public void setSubject(String subject) {
this.subject = subject;
}
public String getSubject() {
return subject;
}
/**
* use smtp send Email
*
* @throws MessagingException
* mail fail
*/
public void smtp(String receiver, String content) throws MessagingException {
if (smtpHost == null)
throw new MessagingException("smtpHost not found");
if (user == null)
throw new MessagingException("user not found");
if (password == null)
throw new MessagingException("password not found");
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
MimeMessage mimeMsg = new MimeMessage(session);
if (sender != null)
{
mimeMsg.setFrom(new InternetAddress(sender));
}
if (receiver != null)
{
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));
}
if (subject != null)
{
mimeMsg.setSubject(subject, "GBK");
}
MimeBodyPart part = new MimeBodyPart();
part.setText(content == null ? "" : content, "GBK");
part.setContent(content.toString(), "text/html;charset=GBK");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);
// Part two is attachment
if (fileAttachment != null && fileAttachment.trim().length() > 1) {
part = new MimeBodyPart();
DataSource source = new FileDataSource(fileAttachment);
part.setDataHandler(new DataHandler(source));
part.setFileName(fileAttachment);
multipart.addBodyPart(part);
}
mimeMsg.setContent(multipart);
mimeMsg.setSentDate(new Date());
Transport.send(mimeMsg);
}
/** split string */
private InternetAddress[] parse(String addressSet) throws AddressException {
ArrayList list = new ArrayList();
StringTokenizer tokens = new StringTokenizer(addressSet, ";");
while (tokens.hasMoreTokens()) {
list.add(new InternetAddress(tokens.nextToken().trim()));
}
InternetAddress[] addressArray = new InternetAddress[list.size()];
list.toArray(addressArray);
return addressArray;
}
/**
* interface
*/
public static boolean sendEmails(String recever, String content,String suject,String attachment) {
String sender="邮箱地址";
String username="用户名";
String password="密码";
if (recever == null || content == null||username==null
||password==null ||sender==null) {
return false;
}
if(attachment==null){
attachment="";
}
if(suject==null){
suject=username;
}
SendEmails sendEmail = new SendEmails(username,password,sender,suject,attachment);
try {
sendEmail.smtp(recever, content);
} catch (Exception ex) {
System.err.println("send Email is faild!" + ex.toString());
}
return true;
}
/**
* @return Returns the fileAttachment.
*/
public String getFileAttachment() {
return fileAttachment;
}
/**
* @param fileAttachment The fileAttachment to set.
*/
public void setFileAttachment(String fileAttachment) {
this.fileAttachment = fileAttachment;
}
}
[此贴子已经被作者于2005-11-30 15:15:51编辑过]
别做程序了,给你们一个赚钱的秘诀。做豆腐;if 做硬了 then 卖豆腐干;if 做稀了 then 卖豆腐花;if 太稀了 then 卖豆浆;if 豆腐卖不动了放几天 then 卖臭豆腐; else if 还卖不动放坏了 then 卖腐乳;