Topic: Simple Mail Sender (bean)

  Print this page

1.Simple Mail Sender (bean) Copy to clipboard
Posted by: diystar
Posted on: 2003-06-19 17:50

import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* <p>Title: 简单的MailBean</p>
* <p>Description: 提供简便的Mail发送功能(支持多附件及身份验证)</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author diystar
* @version 1.0
*/
public class SimpleMail
{
private String sender = null;//发件人地址
private String smtpHost = null;//邮件发送服务器(smtp)
private String user = null;//登录用户名
private String password = null;//登录密码

private String receiver = null;//收件人地址
private String copyReceiver = null;//抄送人地址
private String hiddenReceiver = null;//暗送人地址

private String subject = null;//mail主题
private String content = null;//mail内容
private String affixPath = null;//mail附件的路径

/** 设置发件人地址 */
public void setSender(String sender) {this.sender = sender;}
public String getSender() {return sender;}

/** 设置邮件发送服务器(smtp) */
public void setSmtpHost(String smtpHost) {this.smtpHost = smtpHost;}
public String getSmtpHost() {return smtpHost;}

/** 设置登录用户名 */
public void setUser(String user) {this.user = user;}
public String getUser() {return user;}

/** 设置登录密码 */
public void setPassword(String password) {this.password = password;}
public String getPassword() {return password;}

/** 设置收件人地址 */
public void setReceiver(String receiver) {this.receiver = receiver;}
public String getReceiver() {return receiver;}

/** 设置抄送人地址 */
public void setCopyReceiver(String receiver) {this.copyReceiver = receiver;}
public String getCopyReceiver() {return copyReceiver;}

/** 设置暗送人地址 */
public void setHiddenReceiver(String receiver) {this.hiddenReceiver = receiver;}
public String getHiddenReceiver() {return hiddenReceiver;}

/** 设置mail主题 */
public void setSubject(String subject) {this.subject = subject;}
public String getSubject() {return subject;}

/** 设置mail内容 */
public void setContent(String content) {this.content = content;}
public String getContent() {return content;}

/** 设置mail附件的路径 */
public void setAffixPath(String affixPath) {this.affixPath = affixPath;}
public String getAffixPath() {return affixPath;}

/**
* 使用smtp发送邮件
* @throws MessagingException mail发送失败
*/
public void smtp() 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");
if (receiver == null && copyReceiver == null && hiddenReceiver == null)
throw new MessagingException("receiver not found");

Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);//设置smtp主机
properties.put("mail.smtp.auth", "true");//使用smtp身份验证

Session session = Session.getDefaultInstance(properties, new Authenticator()
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(user, password);
}
});//获得邮件会话对象

MimeMessage mimeMsg = new MimeMessage(session);//创建MIME邮件对象

if (sender != null)//设置发件人地址
mimeMsg.setFrom(new InternetAddress(sender));

if (receiver != null)//设置收件人地址
mimeMsg.setRecipients(Message.RecipientType.TO, parse(receiver));

if (copyReceiver != null)//设置抄送人地址
mimeMsg.setRecipients(Message.RecipientType.CC, parse(copyReceiver));

if (hiddenReceiver != null)//设置暗送人地址
mimeMsg.setRecipients(Message.RecipientType.BCC, parse(hiddenReceiver));

if (subject != null)//设置邮件主题
mimeMsg.setSubject(subject, "UTF-8");

MimeBodyPart part = new MimeBodyPart();//mail内容部分
part.setText(content == null ? "" : content, "UTF-8");

Multipart multipart = new MimeMultipart();
multipart.addBodyPart(part);//在 Multipart 中增加mail内容部分

if (affixPath != null)//mail附件部分
{
ArrayList parts = parsePath(affixPath);
for (int i = 0; i < parts.size(); i ++)
{
multipart.addBodyPart( (MimeBodyPart) parts.get(i));
}
}

mimeMsg.setContent(multipart);//增加 Multipart 到信息体
mimeMsg.setSentDate(new Date());//设置发送日期

Transport.send(mimeMsg);//发送邮件
}

/** 解析地址集合字符串 */
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;
}

/** 解析附件路径字符串 */
private ArrayList parsePath(String affixPath) throws MessagingException
{
ArrayList list = new ArrayList();

StringTokenizer tokens = new StringTokenizer(affixPath, ";");
while (tokens.hasMoreTokens())
{
MimeBodyPart part = new MimeBodyPart();
FileDataSource source = new FileDataSource(tokens.nextToken().trim());
part.setDataHandler(new DataHandler(source));
part.setFileName(source.getName());
list.add(part);
}

return list;
}

/** 一个简单的用例,大家只要将相关的参数改成自己的就行 */
public static void main(String[] args)
{
SimpleMail mail = new SimpleMail();
mail.setSender("my@21cn.com");
mail.setSmtpHost("pop.21cn.com");
mail.setUser("user");
mail.setPassword("password");
mail.setReceiver("other1@21cn.com; other2@21cn.com");
mail.setContent("test mail sender");
mail.setAffixPath("C:/boot.ini; C:/config.sys");

try
{
mail.smtp();
}
catch (MessagingException ex)
{
ex.printStackTrace();
}
}

}


SimpleMail.java (6.56k)

2.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-06-19 18:02

看到好像很多人为此困扰,我就斗胆献丑,喜欢的支持一下Cool

3.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: why
Posted on: 2003-06-19 19:39

既然是diystar原创的,就打分吧。Smile

小的修改了大侠的留言,选了 Disable Smileys
indentation 有点不太理想喔,希望大侠有空时修葺一下啦!

如果加一个用例,就更理想了。(虽然小的不需要)

4.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-06-20 09:12

多谢版主,我已调整了程序的格式

5.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: floater
Posted on: 2003-06-20 23:39

Could you write a simple main() to show how to use it please?

6.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: FinalFantasy
Posted on: 2003-06-22 00:09

如果能加入 charset 的变换,IMAP的支持(主要就是参数的不同。)

还有pop/IMAP的的接受,就更好了。。。^_^,收下了,谢谢。。

7.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-06-23 09:04

已增加了main用例,关于pop/IMAP的支持嘛……

8.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: FinalFantasy
Posted on: 2003-06-23 16:22

哦,我也许表达的不清楚,

IMAP是一个协议,收发都用它的,所以,实现也很容易。

9.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: asdlcj
Posted on: 2003-06-24 23:31

我在 sohu 上有油箱!
邮件发送服务器
可不可以这样设置 啊?

www.sohu.com
谢了

10.Re:Simple Mail Sender (bean) [Re: asdlcj] Copy to clipboard
Posted by: 牛老板
Posted on: 2003-06-24 23:56

呵呵,我用了几种转换方式,终于让他们该换行就换行,呵呵,累死了,收藏了这个贴子.

11.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: asdlcj
Posted on: 2003-06-25 00:08

大哥!你说啊!
怎么设置 啊

12.Re:Simple Mail Sender (bean) [Re: asdlcj] Copy to clipboard
Posted by: why
Posted on: 2003-06-25 02:51

asdlcj wrote:
大哥!你说啊!
怎么设置 啊

if your SMTP server doesn't require authentication, then
Transport.send(mimeMsg);//发送邮件
will do the job; if it does require authentication, then you need something like:
message.saveChanges(); // implicit with send()
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();

you may have to setup something else, like an Authenticator... I didn't have the resource to try -- my ISP doesn't require authentication.

Source: http://developer.java.sun.com/developer/onlineTraining/JavaMail/contents.html

13.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: floater
Posted on: 2003-06-25 03:01

simple, straightforward, clean, quite an example for us.

14.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: szmustang
Posted on: 2003-06-25 09:19

Good! 简洁明了!

15.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: diystar
Posted on: 2003-06-25 11:40

牛老板 wrote:
呵呵,我用了几种转换方式,终于让他们该换行就换行,呵呵,累死了,收藏了这个贴子.


好像论坛的代码拷贝回车符会无效,看来我还是传一份附件吧

16.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: asdlcj
Posted on: 2003-06-26 21:16

public static void main(String[] args) {
SimpleMail mail = new SimpleMail();
mail.setSender("li_chunjin@21cn.com");
mail.setSmtpHost("pop.sohu.com");
mail.setUser("li_chunjin");
mail.setPassword("******");
mail.setReceiver("li-chunjin@21cn.com; lichunjin@21cn.com");
mail.setContent("test mail sender");
mail.setAffixPath("C:/boot.ini; C:/config.sys");
try {
mail.smtp(); }
catch (MessagingException ex) {
ex.printStackTrace();
}
}
我main中这样改!
编译成功产生2个class
可是我java SimpleMail
产生错误:找不到main的class
怎么回事啊?谢谢!

17.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: abc123
Posted on: 2003-06-27 00:20

乱码问题没有解决?

18.Re:Simple Mail Sender (bean) [Re: diystar] Copy to clipboard
Posted by: asdlcj
Posted on: 2003-06-27 18:06

这是用字母 的!不会乱吗的
不是这个问题啊


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923