[java]代码库import java.util.*;

import javax.mail.*;

import javax.mail.internet.*;

import javax.activation.*;

/**

*

* 发送邮件

*/

public class SMTPSender {

private MimeMessage mimeMsg; // MIME邮件对象

private Session session; // 邮件会话对象

private Properties props; // 系统属性

private boolean needAuth = false; // smtp是否需要认证

private String username = ""; // smtp认证用户名和密码

private String password = "";

private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象

public SMTPSender(String smtp) {

setSmtpHost(smtp);

createMimeMessage();

}

public void setHost(String smtp) {

setSmtpHost(smtp);

createMimeMessage();

}

/**

*

* @param hostName

*/

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

*/

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

*/

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

* @param pass

*/

public void setNamePass(String name, String pass) {

username = name;

password = pass;

}

/**

*

* @param mailSubject

* @return

*/

public boolean setSubject(String mailSubject) {

System.out.println("设置邮件主题!");

try {

mimeMsg.setSubject(mailSubject);

return true;

} catch (Exception e) {

System.err.println("设置邮件主题发生错误!");

return false;

}

}

/**

*

* @param mailBody

* @return

*/

public boolean setBody(String mailBody) {

try {

BodyPart bp = new MimeBodyPart();

bp.setContent("" + mailBody, "text/html;charset=GB2312");

mp.addBodyPart(bp);

return true;

} catch (Exception e) {

System.err.println("设置邮件正文时发生错误!" + e);

return false;

}

}

/**

*

* @param filename

* @return

*/

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 from

* @return

*/

public boolean setFrom(String from) {

System.out.println("设置发信人!");

try {

mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人

return true;

} catch (Exception e) {

return false;

}

}

/**

*

* @param to

* @return

*/

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 copyto

* @return

*/

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;

}

}

/**

*

* @return

*/

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;

}

}

/**

*

* @param toemail

* @param title

* @param msg

* @return

*/

public boolean initMail(String toemail, String title, String msg) {

String mailbody = msg;

System.out.println("toemail:" + toemail);

SMTPSender themail = new SMTPSender(mailbody);

themail.setHost("smtp.163.com");

themail.setNeedAuth(true);

if (themail.setSubject(title) == false) {

System.out.println("setsubject fail");

}

if (themail.setBody(mailbody) == false) {

System.out.println("setbody fail");

}

if (themail.setTo(toemail) == false) {

System.out.println("setto fail");

}

if (themail.setFrom("xxx@163.com") == false) {

System.out.println("setfrom fail");

themail.setNamePass("xxx", "xxx");

}

if (themail.sendout() == false) {

return false;

}

return true;

}

}

mimemessage类是什么_邮件发送类相关推荐

  1. 邮件发送类,支持HTML格式,支持优先级设置

    www.chinacs.net  2002-5-9  中文C#技术站 邮件发送类,支持HTML格式,支持优先级设置.通过SOCKET类实现的 using System; using System.Te ...

  2. 邮件发送类,支持Gmail

    邮件发送类,支持Gmail,使用简单方便 using System; using System.Collections.Generic; using System.Text; using System ...

  3. C#类与对象_创建玩家类

    C#:类与对象_创建玩家类,实现字段,方法,引用和简单游戏逻辑 //创建CF当中的玩家类Player,该类含有字段:名字,性别, 血量,武器. //武器背包当中匕首,步枪,机枪,狙击枪. 玩家类具有以 ...

  4. 邮件发送类_10 分钟实现 Spring Boot 发生邮件功能

    基础知识 什么是SMTP? 什么是IMAP? 什么是POP3? IMAP和POP3协议有什么不同呢? 进阶知识 什么是JavaMailSender和JavaMailSenderImpl? 如何通过Ja ...

  5. 邮件发送类_SpringBoot优雅地发送邮件

    在小明经历的多个项目开发中,总会遇到消息通知的场景,比如某个广告主提交一个表单,我们要通知提醒运营人员及时查看. 消息通知的形式也有很多,比如:短信.邮件.app推送等,本文主要给大家描述一下邮件通知 ...

  6. 螺旋测微器b类不确定度_螺旋测微器b类不确定度_物理实验直测量不确定度评估.ppt...

    物理实验直测量不确定度评估 直接测量不确定度评估 Gauss分布 测量列的平均值.标准差 A类不确定度 t分布 B类不确定度 直接测量的合成不确定度 Gauss分布 也称正态分布. δ的平均值等于0. ...

  7. 关于 PHPMailer 邮件发送类的使用心得(含多文件上传)

    This is important for send mail PHPMailer 核心文件 class.phpmailer.php class.phpmaileroauth.php class.ph ...

  8. php 调用现成类实现163邮箱邮件发送

    今天学习使用了使用163邮箱作为发送邮箱,虽然使用了网上的一些方法,但是都没有成功,后来使用了老师给的一个封装好的类,地址如下: 链接:http://pan.baidu.com/s/1hrOCyqw ...

  9. python 数据类笔试题_一道 Python 类的笔试题详解

    r = {} class C(object): def __init__(self, a, b): self.a = a self.b = b if b == 'a': orig = super(C, ...

最新文章

  1. JetBrains WebStorm 快捷键失效
  2. python在不同层级目录import模块的方法
  3. 解决VM 与 Device/Credential Guard 不兼容。在禁用 Device/Credential Guard 后,可以运行 VM 的方法
  4. hive不在同一台机 hue_环境篇:呕心沥血@CDH线上调优
  5. 数组常见异常 学习笔记
  6. [读书笔记]Ajax的通信方式 (一)
  7. 远程linux服务器中安装jupyter通过本地浏览器访问使用
  8. htmlh1 h6,HTML 5 h1 至 h6 标签 - HTML 参考手册
  9. 西门子plc软件 linux,西门子PLC编程软件
  10. Dubbo视频教程--基础篇--第03节--ZooKeeper注册中心安装详细步骤(单节点)
  11. 数学建模——数据包络分析步骤及程序详解
  12. IE 浏览器旧版本下载
  13. csr蓝牙适配 linux,新款4.0蓝牙适配器 迷你4.0蓝牙适配器 Bluetooth CSR 4.0 Dongle
  14. php 字典树,关于PHP字典树的定义与实现方法
  15. [文章] 小本本记下来--CC
  16. Windows10专业版重装系统教程
  17. linux死机,Linux 死机了怎么办
  18. 【STM32CubeMX+Keil+PROTEUS】之---4*4键盘仿真驱动
  19. 行内和块级元素区别?如何让行内元素设置宽高?
  20. 蓝牙—RFCOMM协议

热门文章

  1. 想要更高效地使用云计算,推荐学习云计算部署的五大策略
  2. serv-u 用户时间显示相差8小时_调好闹钟!4月8日凌晨,将迎来今年最大满月
  3. 你好 同样在努力的陌生人
  4. kubeadm 安装 k8s 集群
  5. unity3d+Android:v3签名问题,修改为v2
  6. 华为 路由双点双向引入
  7. 云原生kubernetes五 :pod创建流程
  8. L2~L5泊车场景泊车功能演进
  9. 经济法期末模拟试卷及答案
  10. MySQL的地理位置类型