刚好我也在学习,网上找了些:
邮件群发:
收件人的地址设置为tomail i的形式,利用For循环向这些地址发送邮件,以实现群发的目的.
利用Address类设置邮件信息的收件人和发件人信息,在创建了邮件地址类后,通过message的setFrom()方法设置邮件的发件人,代码如下:
message.setFrom(from_mail);
设置收件人地址时使用setRecipient()方法设置收信人地址,代码如下:
message.setRecipient(type,address);
①.)Message.RecipientType.TO--发送.
①.、创建一个Http的模拟请求工具类,然后写一个POST方法或者GET方法
package byd.core;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.Socket;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* 该类使用Socket连接到邮件服务器, 并实现了向指定邮箱发送邮件及附件的功能.
*
* @author Kou Hongtao
*/
public class Email {
* 换行符
private static final String LINE_END = "\r\n";
* 值为"true"输出高度信息(包括服务器响应信息),值为" false"则不输出调试信息.
private boolean isDebug = true;
* 并在邮件发送完毕后将这些消息返回给用户.
private boolean isAllowReadSocketInfo = true;
* 邮件服务器地址
private String host;
* 发件人邮箱地址
private String from;
* 收件人邮箱地址
private ListString to;
* 抄送地址
private ListString cc;
* 暗送地址
private ListString bcc;
* 邮件主题
private String subject;
* 用户名
private String user;
* 密码
private String password;
* MIME邮件类型
private String contentType;
* 的分隔标识,我们可以将邮件的正文及每一个附件都看作是一个邮件单元 .
private String boundary;
* 邮件单元分隔标识符,该属性将用来在邮件中作为分割各个邮件单元的标识 .
private String boundaryNextPart;
* 传输邮件所采用的编码
private String contentTransferEncoding;
* 设置邮件正文所用的字符集
private String charset;
* 内容描述
private String contentDisposition;
* 邮件正文
private String content;
* 发送邮件日期的显示格式
private String simpleDatePattern;
* 附件的默认MIME类型
private String defaultAttachmentContentType;
* 邮件单元的集合,用来存放正文单元和所有的附件单元.
private ListMailPart partSet;
private ListMailPart alternativeList;
private String mixedBoundary;
private String mixedBoundaryNextPart;
private static MapString, String contentTypeMap;
private static enum TextType {
PLAIN("plain"), HTML("html");
private String v;
private TextType(String v) {
this.v = v;
}
public String getValue() {
return this.v;
static {
// MIME Media Types
contentTypeMap = new HashMapString, String();
contentTypeMap.put("xls", "application/vnd.ms-excel");
contentTypeMap.put("xlsx", "application/vnd.ms-excel");
contentTypeMap.put("xlsm", "application/vnd.ms-excel");
contentTypeMap.put("xlsb", "application/vnd.ms-excel");
contentTypeMap.put("doc", "application/msword");
contentTypeMap.put("dot", "application/msword");
contentTypeMap.put("docx", "application/msword");
contentTypeMap.put("docm", "application/msword");
contentTypeMap.put("dotm", "application/msword");
* ,今天这一节制作这个子类主要是为了区别邮件单元对象和邮件服务对象 ,使程序易读一些.
private class MailPart extends Email {
public MailPart() {
* 默认构造函数
public Email() {
defaultAttachmentContentType = "application/octet-stream";
simpleDatePattern = "yyyy-MM-dd HH:mm:ss";
boundaryNextPart = "--" + boundary;
contentType = "multipart/mixed";
charset = Charset.defaultCharset().name();
partSet = new ArrayListMailPart();
alternativeList = new ArrayListMailPart();
to = new ArrayListString();
cc = new ArrayListString();
bcc = new ArrayListString();
mixedBoundary = "=NextAttachment_zlz_" + System.currentTimeMillis();
mixedBoundaryNextPart = "--" + mixedBoundary;
* 所指定的默认类型.
* @param fileName
* 文件名
* @return 返回文件对应的MIME类型.
private String getPartContentType(String fileName) {
String ret = null;
if (null != fileName) {
int flag = fileName.lastIndexOf(".");
if (0 = flag flag fileName.length() - 1) {
fileName = fileName.substring(flag + 1);
ret = contentTypeMap.get(fileName);
if (null == ret) {
ret = defaultAttachmentContentType;
return ret;
* @param str
* 需要转码的字符串
* @param charset
* 原字符串的编码格式
if (null != str) {
try {
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
* @param bs
* 需要转码的字节数组
* 将所有的邮件单元按照标准的MIME格式要求合并.
* @return 返回一个所有单元合并后的字符串.
private String getAllParts() {
StringBuilder sbd = new StringBuilder(LINE_END);
sbd.append(mixedBoundaryNextPart);
sbd.append(LINE_END);
sbd.append("Content-Type: ");
sbd.append("multipart/alternative");
sbd.append(";");
sbd.append("boundary=\"");
sbd.append(boundary).append("\""); // 邮件类型设置
addPartsToString(alternativeList, sbd, getBoundaryNextPart());
sbd.append(getBoundaryNextPart()).append("--");
addPartsToString(partSet, sbd, mixedBoundaryNextPart);
sbd.append(mixedBoundaryNextPart).append("--");
// sbd.append(boundaryNextPart).
// append(LINE_END);
alternativeList.clear();
partSet.clear();
return sbd.toString();
希望我的回答对你有帮助,谢谢!