| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1433 人关注过本帖
标题:发送E-mail
只看楼主 加入收藏
yangwudong
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:244
专家分:0
注 册:2005-8-18
收藏
得分:0 


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 卖腐乳;
2005-11-30 15:02
yangwudong
Rank: 3Rank: 3
等 级:新手上路
威 望:7
帖 子:244
专家分:0
注 册:2005-8-18
收藏
得分:0 
正在使用的代码,建议大家不要只用jsp,最起码也要用一用servlet吧!如果想学习struts配置的话,请看置顶贴,我马上发!

别做程序了,给你们一个赚钱的秘诀。做豆腐;if 做硬了 then 卖豆腐干;if 做稀了 then 卖豆腐花;if 太稀了 then 卖豆浆;if 豆腐卖不动了放几天 then 卖臭豆腐; else if 还卖不动放坏了 then 卖腐乳;
2005-11-30 15:04
zhuly
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2005-11-18
收藏
得分:0 

感谢了朋友们!我是个菜鸟,我是来学习的。


有着对网站挚热的追求!
2005-12-02 15:03
天地乾坤
Rank: 1
等 级:新手上路
帖 子:85
专家分:0
注 册:2005-11-21
收藏
得分:0 
多谢斑竹!!
2005-12-03 11:15
快速回复:发送E-mail
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017795 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved