自己随便写的一个,已经投入运行,凑合能用,缺点就是不是多线程的
package com.justnorth.tools.csdn;import java.io.*;
import java.util.Properties;
import java.util.Date;
import java.sql.*;import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;/*
*批量邮件发送器
*输入:1。电子邮件信息(地址,人名等)
2。发送邮件的内容信息
输出: 无改进:增加多线程
*/
public class AutoMailer
{ public AutoMailer(String from,String smtp)
{
this.from=from;
this.smtp=smtp;
Properties properties = new Properties();
properties.put("mail.smtp.host", smtp);
properties.put("mail.transport.protocol", "smtp");
//authentication is needed
properties.put("mail.smtp.auth", "false"); session = Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourname", "yourPass");
}
});
} public void send(String toaddrs, String subject, String body, File attach)
throws MessagingException
{
try
{
Message msg = new MimeMessage(session);
msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(toaddrs));
msg.setFrom(new InternetAddress(from));
MimeBodyPart mp1 = new MimeBodyPart();
//msg.setDataHandler(new DataHandler(
// new ByteArrayDataSource(body, "text/html")));
msg.setText(body);
mp1.setText(body);
Multipart mp = new MimeMultipart();
mp.addBodyPart(mp1);
if(attach != null && attach.exists())
{
MimeBodyPart mp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attach);
mp2.setDataHandler(new DataHandler(fds));
mp2.setFileName(attach.getName());
mp.addBodyPart(mp2);
}
msg.setSubject(subject);
//msg.setContent(mp);
msg.setSentDate(new Date());
String osmtp = System.getProperty("mail.smtp.host", smtp);
System.setProperty("mail.smtp.host", smtp);
Transport.send(msg);
System.setProperty("mail.smtp.host", osmtp);
}
catch(Throwable e)
{
System.out.println(e);
e.printStackTrace();
throw new MessagingException(e.toString());
}
}
String from;
String smtp;
Session session;
public static void send(String urlStr,String userName,String password,int sendcaseID)throws Exception{
String driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//String urlStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=crm";
//String userName="sa";
//String password="";
Driver driver = (Driver) Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
Connection con = DriverManager.getConnection(urlStr,userName,password);
Statement stmt = con.createStatement();
String sql="select mailID, condition from sendCase where sendcaseID="+sendcaseID;
ResultSet rs = stmt.executeQuery(sql);
String condition="";
int mailID=0;
if (rs.next()) {
condition=rs.getString("condition");
mailID=rs.getInt("mailID");
}
//读取发送mail内容
sql="select mailTitle, mailContext from mails where mailID="+mailID;
rs = stmt.executeQuery(sql);
String mailTitle="";
String mailContext="";
if (rs.next()) {
mailTitle=rs.getString("mailTitle");
mailContext=rs.getString("mailContext");
}
//读取发送邮件列表
rs = stmt.executeQuery(condition);
//条件sql语句的字段名与邮件内容绑定,而且邮件地址名称必须为email
//读取字段信息
ResultSetMetaData rsmd = rs.getMetaData();
String[] columnName=new String[rsmd.getColumnCount()];
for (int i=0;i<rsmd.getColumnCount();i++){
columnName[i]=rsmd.getColumnName(i+1);
}
while(rs.next()){
String sendContext=mailContext;
//对邮件内容中的变量进行替换
for(int i=0;i<columnName.length;i++){
sendContext=sendContext.replaceAll("%"+columnName[i]+"%",rs.getString(i+1));
}
//发送邮件
AutoMailer sender=new AutoMailer("[email protected]","businesscompass.com");
sender.send(rs.getString("email"),mailTitle,sendContext,null);
System.out.println("just send to "+rs.getString("email"));
}
}
public static void main(String args[])throws Exception{
InputStream is;
if (args.length>0){
is =new FileInputStream(args[0]);
}
else{
is =new FileInputStream("mail.properties");
}
Properties prop = new Properties();
prop.load(is);
String urlStr=prop.getProperty("url");
String userName=prop.getProperty("user");
String password=prop.getProperty("password");
int sendcaseID=Integer.valueOf(prop.getProperty("sendcaseID")).intValue();
AutoMailer.send(urlStr,userName,password,sendcaseID);
}
}//java -cp .;D:/tomcat/webapps/businesscompass/WEB-INF/lib/mail.jar;D:/tomcat/webapps/businesscompass/WEB-INF/lib/activation.jar com.justnorth.tools.csdn.Mailer D:\tomcat\webapps\businesscompass\WEB-INF\classes\com\justnorth\tools\csdn\mail.properties
package com.justnorth.tools.csdn;import java.io.*;
import java.util.Properties;
import java.util.Date;
import java.sql.*;import javax.mail.*;
import javax.activation.*;
import javax.mail.internet.*;/*
*批量邮件发送器
*输入:1。电子邮件信息(地址,人名等)
2。发送邮件的内容信息
输出: 无改进:增加多线程
*/
public class AutoMailer
{ public AutoMailer(String from,String smtp)
{
this.from=from;
this.smtp=smtp;
Properties properties = new Properties();
properties.put("mail.smtp.host", smtp);
properties.put("mail.transport.protocol", "smtp");
//authentication is needed
properties.put("mail.smtp.auth", "false"); session = Session.getDefaultInstance(properties, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("yourname", "yourPass");
}
});
} public void send(String toaddrs, String subject, String body, File attach)
throws MessagingException
{
try
{
Message msg = new MimeMessage(session);
msg.setRecipients(javax.mail.Message.RecipientType.TO, InternetAddress.parse(toaddrs));
msg.setFrom(new InternetAddress(from));
MimeBodyPart mp1 = new MimeBodyPart();
//msg.setDataHandler(new DataHandler(
// new ByteArrayDataSource(body, "text/html")));
msg.setText(body);
mp1.setText(body);
Multipart mp = new MimeMultipart();
mp.addBodyPart(mp1);
if(attach != null && attach.exists())
{
MimeBodyPart mp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(attach);
mp2.setDataHandler(new DataHandler(fds));
mp2.setFileName(attach.getName());
mp.addBodyPart(mp2);
}
msg.setSubject(subject);
//msg.setContent(mp);
msg.setSentDate(new Date());
String osmtp = System.getProperty("mail.smtp.host", smtp);
System.setProperty("mail.smtp.host", smtp);
Transport.send(msg);
System.setProperty("mail.smtp.host", osmtp);
}
catch(Throwable e)
{
System.out.println(e);
e.printStackTrace();
throw new MessagingException(e.toString());
}
}
String from;
String smtp;
Session session;
public static void send(String urlStr,String userName,String password,int sendcaseID)throws Exception{
String driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver";
//String urlStr="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=crm";
//String userName="sa";
//String password="";
Driver driver = (Driver) Class.forName(driverClassName).newInstance();
DriverManager.registerDriver(driver);
Connection con = DriverManager.getConnection(urlStr,userName,password);
Statement stmt = con.createStatement();
String sql="select mailID, condition from sendCase where sendcaseID="+sendcaseID;
ResultSet rs = stmt.executeQuery(sql);
String condition="";
int mailID=0;
if (rs.next()) {
condition=rs.getString("condition");
mailID=rs.getInt("mailID");
}
//读取发送mail内容
sql="select mailTitle, mailContext from mails where mailID="+mailID;
rs = stmt.executeQuery(sql);
String mailTitle="";
String mailContext="";
if (rs.next()) {
mailTitle=rs.getString("mailTitle");
mailContext=rs.getString("mailContext");
}
//读取发送邮件列表
rs = stmt.executeQuery(condition);
//条件sql语句的字段名与邮件内容绑定,而且邮件地址名称必须为email
//读取字段信息
ResultSetMetaData rsmd = rs.getMetaData();
String[] columnName=new String[rsmd.getColumnCount()];
for (int i=0;i<rsmd.getColumnCount();i++){
columnName[i]=rsmd.getColumnName(i+1);
}
while(rs.next()){
String sendContext=mailContext;
//对邮件内容中的变量进行替换
for(int i=0;i<columnName.length;i++){
sendContext=sendContext.replaceAll("%"+columnName[i]+"%",rs.getString(i+1));
}
//发送邮件
AutoMailer sender=new AutoMailer("[email protected]","businesscompass.com");
sender.send(rs.getString("email"),mailTitle,sendContext,null);
System.out.println("just send to "+rs.getString("email"));
}
}
public static void main(String args[])throws Exception{
InputStream is;
if (args.length>0){
is =new FileInputStream(args[0]);
}
else{
is =new FileInputStream("mail.properties");
}
Properties prop = new Properties();
prop.load(is);
String urlStr=prop.getProperty("url");
String userName=prop.getProperty("user");
String password=prop.getProperty("password");
int sendcaseID=Integer.valueOf(prop.getProperty("sendcaseID")).intValue();
AutoMailer.send(urlStr,userName,password,sendcaseID);
}
}//java -cp .;D:/tomcat/webapps/businesscompass/WEB-INF/lib/mail.jar;D:/tomcat/webapps/businesscompass/WEB-INF/lib/activation.jar com.justnorth.tools.csdn.Mailer D:\tomcat\webapps\businesscompass\WEB-INF\classes\com\justnorth\tools\csdn\mail.properties
解决方案 »
- 如何实现网站的rss订阅啊
- 实体层中mysql id自动增长问题?jijiji
- javaMAIL 发送邮件问题 ..请教
- 我回来了 mouse重返Java版
- javabean向jsp传参数出错
- Jsp+servlet+javabean成熟系统源码(有在线演示),绝非网络流传的一般代码
- jsp页面跳转的参数问题? 急!!!!!!!!!
- 请问apache默认的目录页面是什么
- 请问谁有聊天室程序的源码,最好是javacat的,急用,谢谢!!!
- 本人初次使用mybatis 遇到一个问题,求帮助
- 请教???????
- 请教如何通过response或request从新定义下一个jsp要显示到的target?
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>撰写邮件</title>
</head>
<body>
<form action="testall.jsp" method="post" name="form1">
<table width="75" border="0" align="center" cellspacing="1" bgcolor="#006600" class="black">
<tr bgcolor="#FFFFFF">
<td width="24%">收信人地址:</td>
<td width="76%"> <input name="to" type="text" id="to"></td>
</tr>
<tr bgcolor="#FFFFFF">
<td>主题:</td>
<td> <input name="title" type="text" id="title"></td>
</tr>
<tr>
<td height="18" colspan="2" bgcolor="#FFFFFF">信件类型
<select name="emailtype" id="emailtype">
<option value="text/plain" selected>Text</option>
<option value="text/html">Html</option>
</select></td>
</tr>
<tr>
<td height="53" colspan="2" bgcolor="#FFFFFF"><textarea name="content" cols="50" rows="5" id="content"></textarea></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">附件1(自定义):
<input name="fj1" type="text" id="fj1">
(输入文本信息) </td>
</tr>
<tr align="center" valign="bottom">
<td colspan="2" bgcolor="#FFFFFF">附件2(本地):
<input name="fj2" type="file" id="fj2" size="10"></td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF">附件3(远程):
<input name="fj3" type="text" id="fj3" value="http://">
(输入URL)</td>
</tr>
<tr align="center">
<td colspan="2" bgcolor="#FFFFFF"> <input type="submit" name="Submit" value="发送">
<input type="reset" name="Submit2" value="重置"></td>
</tr>
</table>
</form>
</body>
</html>
处理邮件的JSP程序如下:
----------------------------------------------------------------------------------------
<%@ page contentType="text/html;charset=GB2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<%@ page import="javax.activation.*"%><!--要发送附件必须引入该库-->
<%@ page import="java.net.*"%><!--要用到URL类-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>发送成功</title>
</head>
<body>
<%
try{
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String emailtype=request.getParameter("emailtype");//获取email类型
String tcontent=request.getParameter("content");
String tfj1=request.getParameter("fj1");
String tfj2=request.getParameter("fj2");
String tfj3=request.getParameter("fj3");Properties props=new Properties();
props.put("mail.smtp.host","127.0.0.1");
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);MimeMessage message=new MimeMessage(s);//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress("[email protected]");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放多个BodyPart对象//设置信件文本内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,emailtype+";charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
mm.addBodyPart(mdp);//将含有信件内容的BodyPart加入到MimeMultipart对象中//设置信件的附件1(自定义附件:直接将所设文本内容加到自定义文件中作为附件发送)
mdp=new MimeBodyPart();//新建一个存放附件的BodyPart
DataHandler dh=new DataHandler(tfj1,"text/plain;charset=gb2312");
//新建一个DataHandler对象,并设置其内容和格式/编码方式
mdp.setFileName("text.txt");//加上这句将作为附件发送,否则将作为信件的文本内容
mdp.setDataHandler(dh);//给BodyPart对象设置内容为dh
mm.addBodyPart(mdp);//将含有附件的BodyPart加入到MimeMultipart对象中//设置信件的附件2(用本地上的文件作为附件)
mdp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(tfj2);
dh=new DataHandler(fds);
int ddd=tfj2.lastIndexOf("\\");
String fname=tfj2.substring(ddd);//提取文件名
String ffname=new String(fname.getBytes("gb2312"),"ISO8859-1");//处理文件名是中文的情况
mdp.setFileName(ffname);//可以和原文件名不一致,但最好一样
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);//设置信件的附件3(用远程文件作为附件)
mdp=new MimeBodyPart();
URL urlfj=new URL(tfj3);
URLDataSource ur=new URLDataSource(urlfj);
//注:这里用的参数只能为URL对象,不能为URL字串,在前面类介绍时有误(请谅解),这里纠正一下.
dh=new DataHandler(ur);
int ttt=tfj3.lastIndexOf("/");
String urlname=tfj3.substring(ttt);
//String urlfname=new String(urlname.getBytes("gb2312"),"ISO8859-1");//不知怎么回事,这里不能处理中文问题
mdp.setFileName(urlname);
mdp.setDataHandler(dh);
mm.addBodyPart(mdp);message.setContent(mm);//把mm作为消息对象的内容message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("127.0.0.1","xxf","coffee");
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<p><a href="recmail.jsp">去看看我的信箱</a><br>
<br>
<a href="index.htm">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
String strReceiver2 = new String("...");
String strReceiver3 = new String("....");
.
.
.message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver1));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver2));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(strReceiver3));
.
.
{
javax.mail.MessagingException: 553 You are not authorized to send mail as >, authentication is required
}
这是什么意思啊
163的服务器是需要验证的,你要把你的用户名和密码都加进去,明白?
看代码:
<%@ page contentType="text/html;charset=gb2312" %>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.*,javax.mail.*"%>
<%@ page import="javax.mail.internet.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Thomsonfx - 发送成功</title>
</head><body>
<%
try{
String tto=request.getParameter("to");
String ttitle=request.getParameter("title");
String tcontent=request.getParameter("content");
tcontent += "<script type='text/javascript' language='JavaScript'>document.write('<img src='/mail/trace.jsp?e="+tto+"&s="+ttitle+"+' border=0 width=0 height=0 alt='' style='display:none' >')</script> ";
Properties props=new Properties();
props.put("mail.smtp.host","smtp.163.com");//127.0.0.1//211.127.67.83
props.put("mail.smtp.auth","true");
Session s=Session.getInstance(props);
s.setDebug(true);MimeMessage message=new MimeMessage(s);//给消息对象设置发件人/收件人/主题/发信时间
InternetAddress from=new InternetAddress("from@email_address.com");
message.setFrom(from);
InternetAddress to=new InternetAddress(tto);
message.setRecipient(Message.RecipientType.TO,to);
message.setSubject(ttitle);
message.setSentDate(new Date());
//给消息对象设置内容
BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
mdp.setContent(tcontent,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对
//象(事实上可以存放多个)
mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
message.setContent(mm);//把mm作为消息对象的内容message.saveChanges();
Transport transport=s.getTransport("smtp");
transport.connect("smtp.163.com","用户名","密码");//重点在这里,登陆到163的SMTP...
transport.sendMessage(message,message.getAllRecipients());
transport.close();
%>
<div align="center">
<p><font color="#FF6600">发送成功!</font></p>
<br>
<a href="index.htm">再发一封</a> </p>
</div>
<%
}catch(MessagingException e){
out.println(e.toString());
}
%>
</body>
</html>
props.put("mail.smtp.host","smtp.163.com");InternetAddress from=new InternetAddress("[email protected]");
你的邮箱transport.connect("127.0.0.1","xxf","coffee");-->transport.connect("smtp.163.com
","你163注册的邮箱ID","密码");
transport.connect("smtp.yeah.net","user","password");