java邮件发送,例子

package com.test;

import java.net.MalformedURLException;
import java.net.URL;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;

public class EmailTest {
public String sendSingleMail() {
String resultmsg = "发信失败!";
SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.qq.com");
try {
email.addTo("收件人");
email.setFrom("发件人");
// 请修改成你的邮箱账号和密码。
email.setAuthentication("用户名", "密码");
email.setSubject("发信测试标题");
email.setMsg("This is a simple test of commons-email");
resultmsg = email.send();
if (resultmsg != null) {
resultmsg = "发送成功!";
}
} catch (EmailException e) {
e.printStackTrace();
}
return resultmsg;
}

public String sendAttachmentsMail() {
String resultmsg = "发信失败!";
// 创建一个附件对象
EmailAttachment attachment = new EmailAttachment();
// 设置附件的地址,可以是本机地址(包括绝对地址和相对地址),
// 也可以是internet地址,如:http://www.5ipig.com/blog/attachment.php?fid=2
attachment.setPath("E:\\1.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of gaylys");
attachment.setName("gaylys");
// 创建多文本邮件对象
MultiPartEmail email = new MultiPartEmail();
email.setHostName("smtp.163.com");
try {
email.addTo("收件人");
email.setFrom("发件人");
// 请修改成你的邮箱账号和密码。
email.setAuthentication("用户名", "密码");
email.setSubject("带附件的邮件");
email.setMsg("Here is the picture you wanted");
// 将附件添加到邮件
email.attach(attachment);
// 发送邮件
resultmsg = email.send();
if (resultmsg != null && !"".equals(resultmsg)) {
resultmsg = "发送成功!";
}
} catch (EmailException e) {
e.printStackTrace();
}
return resultmsg;
}

/**
* 发送html邮件
*
* @return 发送结果,是否发送成功
*/
public String sendHtmlEmail() {
String resultmsg = "发信失败!";
// 创建邮件对象
HtmlEmail email = new HtmlEmail();
email.setHostName("smtp.163.com");
try {
email.addTo("收件人");
email.setFrom("发件人");
// 请修改成你的邮箱账号和密码。
email.setAuthentication("邮箱用户名", "密码");
email.setSubject("发送html邮件的测试");
// 嵌入附件地址
URL url = new URL("http://www.csdn.net/");
@SuppressWarnings("unused")
String cid = email.embed(url, "3D桌面");
// 设置编码格式,不设置会出现中文乱码。
email.setCharset("GB2312");
// 写入html代码
email.setHtmlMsg("3D桌面 - <img src=\"\" alt=\" />");
// 设置替代文字
email.setTextMsg("Your email client does not support HTML messages");
// 发送邮件
resultmsg = email.send();
if (resultmsg != null && !"".equals(resultmsg)) {
resultmsg = "发送成功!";
}
} catch (EmailException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
}
return resultmsg;
}
public static void main(String[] args) {
EmailTest mail = new EmailTest();
System.out.println(mail.sendSingleMail());
}
}


所用的jar包自己在网上找下.很多的.这里就不提供了.呵呵
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值