使用Commons-mail发送邮件:更合逻辑的实例

Java邮件发送示例
本文介绍如何使用Java发送带有附件的电子邮件,包括设置发件人、收件人、邮件主题和内容等步骤,并提供了一个完整的示例代码。
先定义mail的一个bean:
public class Mail {

private String toAddress; // 邮件接收者
private String nickname; // 收件人昵称
private String subject; // 邮件主题
private String content; // 邮件内容
private String ChartSet; //字符集
private Map<String, String> AttachmentsPath; //附件路径列表


/////setter() and getter()...
}

有了bean后,发送文本形式邮件。发件人在类初始化时完成,然后接下来就可以不断发送而不需再定义。
public class TextMailSender {

private String hostname;
private String username;
private String password;
private String address;
private Boolean TLS;

public TextMailSender(String hostname, String address, String username, String password, Boolean TLS) {
this.hostname = hostname;
this.username = username;
this.password = password;
this.address = address;
this.TLS = TLS;
}

/**
* @throws EmailException
*
* **/
public void execute(Mail mail) throws EmailException{

SimpleEmail email = new SimpleEmail();
email.setTLS(TLS);
email.setHostName(hostname);
email.setAuthentication(username, password); // 用户名和密码
email.setFrom(address); // 发送地址

email.addTo(mail.getToAddress()); // 接收地址
email.setSubject(mail.getSubject()); // 邮件标题
email.setCharset(mail.getChartSet());
email.setMsg(mail.getContent()); // 邮件内容
email.send();
}

////
public static void main(String[] args) {
TextMailSender sender = new TextMailSender("smtp.qq.com", "cesul@qq.com", "cesul", "******", true);

Mail mail = new Mail();
mail.setToAddress("cesul@qq.com");
mail.setSubject("这又是一封测试邮件!");
mail.setContent("呵呵呵呵呵");
mail.setChartSet("utf-8");
try {
sender.execute(mail);
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}


如果需要添加(多个)附件,使用commons-mail的另一个类:
public class AttachmentSender {

private String hostname; //"SMTP服务器"
private String username;
private String password;
private String address;
private String nickname;
private Boolean TLS;

public AttachmentSender(String hostname, String address, String nickname, String username, String password, Boolean TLS) {
this.hostname = hostname;
this.nickname = nickname;
this.username = username;
this.password = password;
this.address = address;
this.TLS = TLS;
}

/**
* @throws UnsupportedEncodingException
* @throws EmailException
*
* **/
@SuppressWarnings("unchecked")
public void execute(Mail mail) throws UnsupportedEncodingException, EmailException{

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName(hostname);
email.setAuthentication(username, password);
email.setFrom(address, nickname); //可以加入发信人称呼
email.setTLS(TLS);

email.setCharset(mail.getChartSet());
email.addTo(mail.getToAddress(), mail.getNickname());
email.setSubject(mail.getSubject());
email.setMsg(mail.getContent());

EmailAttachment attachment;
Iterator<? extends Object> it = mail.getAttachmentsPath().entrySet().iterator(); //附件是多个 ,遍历
while (it.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>)it.next();
attachment = new EmailAttachment();
attachment.setPath(entry.getKey()); //键是附件路径
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(MimeUtility.encodeWord("附件","UTF-8",null));
attachment.setName(MimeUtility.encodeWord(entry.getValue(),"UTF-8",null)); //值是附件描述名
email.attach(attachment); // add the attachment
}
email.send(); // send the email
}


public static void main(String[] args) {

AttachmentSender sender = new AttachmentSender("smtp.qq.com", "cesul@qq.com", "陈志钊", "cesul", "******", true);

Mail mail = new Mail();
mail.setToAddress("cesul@qq.com");
mail.setNickname("你好");
mail.setSubject("Here is the picture you wanted");
mail.setContent("呵呵呵呵呵");
mail.setChartSet("utf-8");

Map<String, String> attachment = new HashMap<String, String>();
attachment.put("E:\\Photos\\2010-08-17-0.bmp", "这是你要的图片.bmp");
attachment.put("E:\\Photos\\2010-07-19-1.bmp", "这也是你要的图片.bmp"); //不要把相同路径的文件发两次
mail.setAttachmentsPath(attachment);

try {
sender.execute(mail);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (EmailException e) {
e.printStackTrace();
}
System.out.println("Finished");
}
}


总结:请参看各“发送类”的构造方法:-)
====================================

要用到的最新jar包下载:

* Commons-email-1.2.zip:
[url]http://commons.apache.org/email/download_email.cgi[/url]
* JavaBeans(TM) Activation Framework 1.1.1:
[url]https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SimpleBundleDownload[/url]
* JavaMail(TM) API 1.4.4 for All Supported Platforms:

[url]https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewFilteredProducts-SingleVariationTypeFilter[/url]
提供了一个基于51单片机的RFID门禁系统的完整资源文件,包括PCB图、原理图、论文以及源程序。该系统设计由单片机、RFID-RC522频射卡模块、LCD显示、灯控电路、蜂鸣器报警电路、存储模块和按键组成。系统支持通过密码和刷卡两种方式进行门禁控制,灯亮表示开门成功,蜂鸣器响表示开门失败。 资源内容 PCB图:包含系统的PCB设计图,方便用户进行硬件电路的制作和调试。 原理图:详细展示了系统的电路连接和模块布局,帮助用户理解系统的工作原理。 论文:提供了系统的详细设计思路、实现方法以及测试结果,适合学习和研究使用。 源程序:包含系统的全部源代码,用户可以根据需要进行修改和优化。 系统功能 刷卡开门:用户可以通过刷RFID卡进行门禁控制,系统会自动识别卡片并判断是否允许开门。 密码开门:用户可以通过输入预设密码进行门禁控制,系统会验证密码的正确性。 状态显示:系统通过LCD显示屏显示当前状态,如刷卡成功、密码错误等。 灯光提示:灯亮表示开门成功,灯灭表示开门失败或未操作。 蜂鸣器报警:当刷卡或密码输入错误时,蜂鸣器会发出报警声,提示用户操作失败。 适用人群 电子工程、自动化等相关专业的学生和研究人员。 对单片机和RFID技术感兴趣的爱好者。 需要开发类似门禁系统的工程师和开发者。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值