下面代码是利用Java mail包封装了一个发送邮件的类

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties; import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class SendMail {
private static final String MAIL_ADDRESS_REGEX = "^[\\w\\.=-]+@[\\w\\.-]+\\.[\\w]{2,3}$"; private String mailServer;
private String sender;
private String[] receiver; public SendMail(){ } public void setMailBasicInfo(String mailServer,String sender,String receiver){
this.mailServer = mailServer;
this.sender = sender;
this.receiver =receiver.split(",");
} public void setMailBasicInfo(String mailServer,String sender,String[] users){
this.mailServer = mailServer;
this.sender = sender;
this.receiver = users;
} public void setMailBasicInfo(String mailServer,String sender,List<String> users){
this.mailServer = mailServer;
this.sender = sender;
this.receiver = new String[users.size()];
users.toArray(this.receiver);
} public boolean send(String subject, String content, List<String> fileNames)
{
subject = subject==null?"":subject;
content = content==null?"":content;
Properties props = new Properties();
props.put("mail.smtp.host", mailServer);
Session session = Session.getInstance(props, null);
try
{
InternetAddress[] receiver = getReceiverList();
if (receiver.length == 0)
{
System.out.println("receiver is null");
return false;
}
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
msg.setRecipients(Message.RecipientType.TO, receiver);
msg.setSubject(subject);
msg.setSentDate(new Date()); Multipart container = new MimeMultipart();
MimeBodyPart textBodyPart = new MimeBodyPart();
textBodyPart.setContent(content.toString(), "text/html;charset=gbk");
container.addBodyPart(textBodyPart); doAttachFile(container,fileNames);
msg.setContent(container);
System.out.println("begin send mail");
Transport.send(msg);
System.out.println("send mail success");
}
catch (MessagingException e)
{
System.out.println("send mail fail");
System.out.println(e);
return false;
}
catch(Exception e){
return false;
}
return true;
} public boolean send(String subject, String content){
return send(subject,content,null);
} public boolean send(String subject){
return send(subject,null);
} private void doAttachFile(Multipart container, List<String> fileNames) throws MessagingException{
if(fileNames==null || fileNames.size()==0)
return;
for(String filename:fileNames){
File f = new File(filename);
if(!f.isFile())
continue;
System.out.println("the attach file is:"+filename);
MimeBodyPart fileBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource(f);// 要发送的附件地址
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());// 设置附件的名称
container.addBodyPart(fileBodyPart);
}
} private InternetAddress[] getReceiverList() throws AddressException
{
ArrayList<InternetAddress> toList = new ArrayList<InternetAddress>();
for (int i = 0; i < receiver.length; ++i)
{
if (receiver[i].matches(MAIL_ADDRESS_REGEX))
{
toList.add(new InternetAddress(receiver[i]));
}
} return (InternetAddress[]) toList.toArray(new InternetAddress[toList.size()]);
}
}

使用举例

String host = "168.xx.xx.xx"; //邮件服务器的地址
String subject = "发送邮件的主题";
String sender = "test@gmail.com";
List<String> receivers = new ArrayList<String>();
receivers.add("user1@263.com");
receivers.add("user2@263.com");
String content = "邮件主题";
SendMail sendMail = new SendMail();
sendMail.setMailBasicInfo(host, sender, receivers);
sendMail.send(subject, content, null); //没有附件 正文也可以是html内容,如
String content = "<html>详细信息:<a href='xxxx'>请点击查看!</a></html>";

Java网络编程:利用Java mail包发送电子邮件的更多相关文章

  1. Java网络编程的Java流介绍

    前言 网络程序所做的很大一部分工作都是简单的输入输出:将数据字节从一个系统移动到另一个系统.Java的I/O建立于流(stream)之上.输入流读取数据,输出流写入数据.过滤器流(filter)流可以 ...

  2. JAVA网络编程【转】出处不详

    网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...

  3. 【转】JAVA 网络编程

    网络编程 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很长一段时间无法进入网络编程的大门而放弃了对于该部分技术的学习. 在 学习网络编程以前,很多初学者可能觉得网络编 ...

  4. Java网络编程和NIO详解开篇:Java网络编程基础

    Java网络编程和NIO详解开篇:Java网络编程基础 计算机网络编程基础 转自:https://mp.weixin.qq.com/s/XXMz5uAFSsPdg38bth2jAA 我们是幸运的,因为 ...

  5. java网络编程+通讯协议的理解

    参考: http://blog.csdn.net/sunyc1990/article/details/50773014 网络编程对于很多的初学者来说,都是很向往的一种编程技能,但是很多的初学者却因为很 ...

  6. Java工程师学习指南第5部分:Java网络编程与NIO

    本文整理了微信公众号[Java技术江湖]发表和转载过的Java网络编程相关优质文章,想看到更多Java技术文章,就赶紧关注本公众号吧. 深度解读 Tomcat 中的 NIO 模型 [Java基本功]浅 ...

  7. Java网络编程和NIO详解4:浅析NIO包中的Buffer、Channel 和 Selector

    Java网络编程与NIO详解4:浅析NIO包中的Buffer.Channel 和 Selector 转自https://www.javadoop.com/post/nio-and-aio 本系列文章首 ...

  8. Java网络编程:QQ邮件发送客户端程序设计

    目录 一.目标介绍 1.认识SMTP(邮件传输协议) 2.POP3(邮件接收协议) 二.基于Base64编码邮箱及授权码 1.开通QQ邮箱SMTP/POP3服务 2.Java编写BASE64编码程序 ...

  9. java网络编程socket解析

    转载:http://www.blogjava.net/landon/archive/2013/07/02/401137.html Java网络编程精解笔记2:Socket详解 Socket用法详解 在 ...

随机推荐

  1. java源代码如何打成jar包

    链接地址:http://jingyan.baidu.com/article/046a7b3ed8b23ef9c27fa9b9.html 有时自已写了一个很巧妙的方法,想分享给别人用,这时我们就可以将其 ...

  2. 在VS2010上使用C#调用非托管C++生成的DLL文件

    背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使用一些第三方通讯组件的时候,通过C#来开发应用软件时,就需要利用DllImport特性进行方法调用.本篇文章将引导你快速理解这个调用 ...

  3. eclipse修改默认工作空间

    新安装的myEclipse(eclipse)第一次启动时就会弹出让你选择工作空间的对话框 如果勾选了Use this as the default and do not ask again 下次要启动 ...

  4. mysql ifnull if

    IFNULL(expr1,expr2) 如果expr1不是NULL,IFNULL()返回expr1,否则它返回expr2.IFNULL()返回一个数字或字符串值,取决于它被使用的上下文环境. mysq ...

  5. python:利用asyncio进行快速抓取

    web数据抓取是一个经常在python的讨论中出现的主题.有很多方法可以用来进行web数据抓取,然而其中好像并没有一个最好的办法.有一些如scrapy这样十分成熟的框架,更多的则是像mechanize ...

  6. Dining(最大流)

    Dining Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11844   Accepted: 5444 Descripti ...

  7. [置顶] 吃论扯谈---吃货和Office 365订阅的关系

    什么事物都可以和吃联系起来,在女孩子穿裙子的季节这是一个悲伤的故事! 说明: :Office365是微软云计算产品之一,其采取订阅的方式,按人头*每月的方式付费,用户可以选择自己需要的服务 2:Off ...

  8. gdal库的三个使用心得

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近使用gdal库比较多,就谈谈gdal库的一些使用心得. 第一个是GDALOpen的访问权限参数会影响图像的创建金字 ...

  9. .NET通用权限系统快速开发框架源代码

    有兴趣的朋友欢迎加群讨论:312677516 一.开发技术:B/S(.NET C# ) 1.Windows XP以上 (支援最新Win 8) 2.Microsoft Visual Studio 201 ...

  10. hadoop源代码解读namenode高可靠:HA;web方式查看namenode下信息;dfs/data决定datanode存储位置

    点击browserFilesystem,和命令查看结果一样 当我们查看hadoop源代码时,我们看到hdfs下的hdfs-default.xml文件信息 我们查找${hadoop.tmp.dir}这是 ...