java如何实现批量发送邮件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何实现批量发送邮件相关的知识,希望对你有一定的参考价值。
1.要想效率发邮件可以用多线程每个线程发指定批量的邮件2.要想不被查封为垃圾邮件,这个就有很多地方要注意了
邮件的内容不要含有太多图片信息,内容尽量简洁,不要涉及一些淫秽色情的东西。
不要向同一个人发送同样的邮件。
发邮件时ip最好用代理每发送个50封邮件就换个ip,因为邮件服务器也会检查你ip在这么短时间发了这么多邮件也会认为你是恶意群发。
最好能加入邮件接收者的白名单或订阅名单 参考技术A
邮件营销也是一种很棒的营销方式,下面是一米软件的智能QQ邮件营销系统,你可以了解一下。
1,软件可自动切换不同的动态拨号VPS的IP,登录不同的第三方免费邮件发送方,模拟真实环境,一个IP登录一个小号邮件账号发送,这样可以绕过因为一个IP同时登录同个第三方免费邮箱的不同账户而造成的屏蔽,目前支持的发件邮箱类型有网易163,126邮箱,hotmail,gmail,新浪邮箱,搜狐邮箱等主流的第三方免费邮箱系统。
2,对敏感词自动转码,绝大部分可轻松绕过QQ邮箱等的叶贝思反垃圾邮件系统,可发html内容,并对敏感内容自动转码加密,QQ邮件中显示时会自动解析成可视的网页内容;独有的图片白条技术,邮件内容中的图片可直接显示,不需要点击再显示。
3, 软件集成了SMTP发送和网页协议发送双模式,这在免费邮箱系统中是两套验证模式,也就是说当smtp方式达到最大发送数量时可以再接着以http协议发送,提高了单个免费邮箱的利用率,增加了每天发送的总数量。
4, 一般的自己建邮箱SMTP发送的所谓邮件群发平台都是按发送数量计费,先不说一般的自建的发送系统没有白名单机制,光按发送数量计费也是一笔不小的开销,而大型的第三方免费平台如163,126等都是QQ认证的白名单IP发送,虽然每个免费邮箱每天发送的额度有限(阈值受限),而这已经形成一个产业链了,您可以花费很少的费用直接购买1000以上这种免费邮箱小号,而且24小时后又恢复了原来的发信阈值,也就是说后续发送成本接近于零。
如何在Java ee项目中如何调用outlook发邮件
java mail调用outlook的方法例子1 将邮件写入到文件的代码
msg.saveChanges();
File f = new File("d:/test.eml");
msg.writeTo(new FileOutputStream(f));
2 调用outlook的代码
Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");
3 完整的代码如下
package code.jdk.mail;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.AuthenticationFailedException;
import javax.mail.Message;
import javax.mail.MessagingException;
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;
import javax.mail.internet.MimeUtility;
public class EmailWriteToFile
// 定义发件人、收件人、SMTP服务器、用户名、密码、主题、内容等
private String displayName;
private String to;
private String from;
private String smtpServer;
private String username;
private String password;
private String subject;
private String content;
private boolean ifAuth; // 服务器是否要身份认证
private String filename = "";
private Vector file = new Vector(); // 用于保存发送附件的文件名的集合
private String contentType = "text/html";
private String charset = "utf-8";
public void addFile(String filename)
file.add(filename);
public String getContentType()
return contentType;
public void setContentType(String contentType)
this.contentType = contentType;
public String getCharset()
return charset;
public void setCharset(String charset)
this.charset = charset;
/**
* 设置SMTP服务器地址
*/
public void setSmtpServer(String smtpServer)
this.smtpServer = smtpServer;
/**
* 设置发件人的地址
*/
public void setFrom(String from)
this.from = from;
/**
* 设置显示的名称
*/
public void setDisplayName(String displayName)
this.displayName = displayName;
/**
* 设置服务器是否需要身份认证
*/
public void setIfAuth(boolean ifAuth)
this.ifAuth = ifAuth;
/**
* 设置E-mail用户名
*/
public void setUserName(String username)
this.username = username;
/**
* 设置E-mail密码
*/
public void setPassword(String password)
this.password = password;
/**
* 设置接收者
*/
public void setTo(String to)
this.to = to;
/**
* 设置主题
*/
public void setSubject(String subject)
this.subject = subject;
/**
* 设置主体内容
*/
public void setContent(String content)
this.content = content;
public EmailWriteToFile()
private int port = 25;
public int getPort()
return port;
public void setPort(int port)
this.port = port;
/**
* 发送邮件
*
* @throws IOException
* @throws FileNotFoundException
*/
public boolean send() throws FileNotFoundException, IOException
HashMap<String, String> map = new HashMap<String, String>();
map.put("state", "success");
String message = "邮件发送成功!";
Session session = null;
Properties props = System.getProperties();
props.put("mail.smtp.host", smtpServer);
props.put("mail.smtp.port", port);
try
props.put("mail.smtp.auth", "false");
session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Transport trans = null;
Message msg = new MimeMessage(session);
try
Address from_address = new InternetAddress(from, displayName);
msg.setFrom(from_address);
catch (java.io.UnsupportedEncodingException e)
e.printStackTrace();
InternetAddress[] address = new InternetAddress(to) ;
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
Multipart mp = new MimeMultipart();
MimeBodyPart mbp = new MimeBodyPart();
mbp.setContent(content.toString(), getContentType() + ";charset=" + getCharset());
mp.addBodyPart(mbp);
if (!file.isEmpty()) // 有附件
Enumeration efile = file.elements();
while (efile.hasMoreElements())
mbp = new MimeBodyPart();
filename = efile.nextElement().toString(); // 选择出每一个附件名
FileDataSource fds = new FileDataSource(filename); // 得到数据源
mbp.setDataHandler(new DataHandler(fds)); // 得到附件本身并至入BodyPart
mbp.setFileName(MimeUtility.encodeText(fds.getName(), getCharset(),"B")); // 得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
file.removeAllElements();
msg.setContent(mp); // Multipart加入到信件
msg.setSentDate(new Date()); // 设置信件头的发送日期
// 发送信件
msg.saveChanges();
File f = new File("d:/test.eml");
msg.writeTo(new FileOutputStream(f));
catch (AuthenticationFailedException e)
map.put("state", "failed");
message = "邮件发送失败!错误原因: " + "身份验证错误!";
e.printStackTrace();
return false;
catch (MessagingException e)
message = "邮件发送失败!错误原因: " + e.getMessage();
map.put("state", "failed");
e.printStackTrace();
Exception ex = null;
if ((ex = e.getNextException()) != null)
System.out.println(ex.toString());
ex.printStackTrace();
return false;
// System.out.println(" 提示信息:"+message);
map.put("message", message);
return true;
public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException
EmailWriteToFile o = new EmailWriteToFile();
o.setSmtpServer("localhost");
o.setFrom("from@from.com");
o.setDisplayName("TOM");
o.setTo("to@to.com");
o.setSubject("Test Subject");
o.setContent("Test Content");
o.setCharset("GBK");
o.addFile("e:/读我.txt");
o.send();
Process p = Runtime.getRuntime().exec("cmd /C start msimn.exe /eml:d:/test.eml");
参考技术A HTML中的mailto:dreamdu@163.com,看客户默认安装的邮箱工具,不一定就非得是outlook
java代码中发送邮件的话,可以用javaMail,参考例子在baidu上一搜能搜出一大堆。 参考技术B 参考:http://www.cnblogs.com/codeplus/archive/2011/10/30/2229391.html
以上是关于java如何实现批量发送邮件的主要内容,如果未能解决你的问题,请参考以下文章