package com.xsl.email;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class MailUtil {
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username = "xiaoshaolong"; // smtp认证用户名和密码
private String password = "5956yixsl";
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象
/**
*
*/
// public MailUtil() {
// setSmtpHost(getConfig.mailHost);// 如果没有指定邮件服务器,就从getConfig类中获取
// createMimeMessage();
// }
//构造方法
public MailUtil(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
/**
* @param hostName
* String
*
*/
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
}
/**
* @return boolean
*/
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
} catch (Exception e) {
System.err.println("获取邮件会话对象时发生错误!" + e);
return false;
}
System.out.println("准备创建MIME邮件对象!");
try {
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart();
return true;
} catch (Exception e) {
System.err.println("创建MIME邮件对象失败!" + e);
return false;
}
}
/**
* @param need
* boolean
*/
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
/**
* @param name
* String
* @param pass
* String
*/
public void setNamePass(String name, String pass) {
username = name;
password = pass;
}
/**
* @param mailSubject
* String
* @return boolean
*/
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(mailSubject);
return true;
} catch (Exception e) {
System.err.println("设置邮件主题发生错误!");
return false;
}
}
/**
* @param mailBody
* String
*/
public boolean setBody(String mailBody) {
try {
BodyPart bp = new MimeBodyPart();
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ mailBody, "text/html;charset=GB2312");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("设置邮件正文时发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean addFileAffix(String filename) {
System.out.println("增加邮件附件:" + filename);
try {
BodyPart bp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(filename);
bp.setDataHandler(new DataHandler(fileds));
bp.setFileName(fileds.getName());
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
System.err.println("增加邮件附件:" + filename + "发生错误!" + e);
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setTo(String to) {
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
/**
* @param name
* String
* @param pass
* String
*/
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,password);
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
// transport.send(mimeMsg);
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
System.err.println("邮件发送失败!" + e);
return false;
}
}
/**
* Just do it as this
*/
public static void main(String[] args) {
String mailbody = "<meta http-equiv=Content-Type content=text/html; charset=gb2312>"
+ "你好";
MailUtil themail = new MailUtil("smtp.126.com");
themail.setNeedAuth(true);
if (themail.setSubject("标题") == false)
return;
if (themail.setBody(mailbody) == false)
return;
if (themail.setTo("451123178@qq.com") == false)
return;
if (themail.setFrom("xiaoshaolong@126.com") == false)
return;
if (themail.addFileAffix("d:\\nod32.txt") == false)
return;
themail.setNamePass("xiaoshaolong", "5956yixsl");
if (themail.sendout() == false)
return;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
用java代码发邮件,真是牛啊,可群发,也可发附件
共28个文件
class:6个
java:6个
jsp:3个
4星 · 超过85%的资源 需积分: 20 46 下载量 66 浏览量
2009-12-03
10:44:55
上传
评论 1
收藏 785KB RAR 举报
温馨提示
这真的是少见的,用java代码,只要打开eclips,单击右键,run as...即可发出你想要发的邮件了,可以qq,163,sina,gmail,等等,只要设置一下就行了
资源推荐
资源详情
资源评论
收起资源包目录
Email2.rar (28个子文件)
Email2
.project 1KB
mail.jar 425KB
.settings
.jsdtscope 500B
org.eclipse.jdt.core.prefs 334B
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.jsdt.ui.superType.name 6B
.mymetadata 291B
WebRoot
WEB-INF
web.xml 672B
lib
jstl.jar 20KB
standard.jar 384KB
classes
com
xsl
email
MailUtil.class 6KB
TestEmail.class 4KB
md5
TestUUID.class 486B
TestMD5.class 2KB
vo
Users.class 1KB
servlet
RegistrationServlet.class 2KB
META-INF
MANIFEST.MF 36B
registration
active.jsp 434B
registration.jsp 3KB
jquery.min.js 53KB
welcome.jsp 448B
.myeclipse
src
com
xsl
email
MailUtil.java 6KB
TestEmail.java 3KB
md5
TestUUID.java 247B
TestMD5.java 1KB
vo
Users.java 971B
servlet
RegistrationServlet.java 2KB
.classpath 525B
共 28 条
- 1
资源评论
- mjy@#¥&20122012-11-19好贵啊,下完之后有错误
- dreamer232014-01-06怎么要花这么多银两,不过还是谢谢分享!
blcstpt25_2009
- 粉丝: 77
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功