import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mailer {
/** javamail session对象. */
protected Session session;
/** 发信地址 */
protected String from;
/** 信的标题*/
protected String subject;
/** 存放字符串形式的收信地址 */
protected ArrayList toList = new ArrayList();
/** 抄送地址*/
protected ArrayList ccList = new ArrayList();
/** 暗送地址 */
protected ArrayList bccList = new ArrayList();
/** 信的文本内容*/
protected String body;
/** SMTP服务器地址*/
protected String mailHost;
/**是否在控制台打印信息*/
protected boolean verbose=true;
/** Get from */
public String getFrom() {
return from;
}
public void setFrom(String fm) {
from = fm;
}
public String getSubject() {
return subject;
}
public void setSubject(String subj) {
subject = subj;
}
public ArrayList getToList() {
return toList;
}
public void setToList(ArrayList to) {
toList = to;
}
public void setToList(String s) {
toList = tokenize(s);
}
public void addTo(String to) {
toList.add(to);
}
public ArrayList getCcList() {
return ccList;
}
public void setCcList(ArrayList cc) {
ccList = cc;
}
public void setCcList(String s) {
ccList = tokenize(s);
}
public void addCc(String cc) {
ccList.add(cc);
}
public ArrayList getBccList() {
return bccList;
}
public void setBccList(ArrayList bcc) {
bccList = bcc;
}
public void setBccList(String s) {
bccList = tokenize(s);
}
public void addBcc(String bcc) {
bccList.add(bcc);
}
public String getBody() {
return body;
}
public void setBody(String text) {
body = text;
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean v) {
verbose = v;
}
public boolean isComplete() {
if (from == null || from.length()==0) {
System.err.println("doSend: no FROM");
return false;
}
if (subject == null || subject.length()==0) {
System.err.println("doSend: no SUBJECT");
return false;
}
if (toList.size()==0) {
System.err.println("doSend: no recipients");
return false;
}
if (body == null || body.length()==0) {
System.err.println("doSend: no body");
return false;
}
if (mailHost == null || mailHost.length()==0) {
System.err.println("doSend: no server host");
return false;
}
return true;
}
public void setServer(String s) {
mailHost = s;
}
/** Send the message.
*/
public synchronized void doSend() throws MessagingException {
if (!isComplete())
throw new IllegalArgumentException("doSend called before message was complete");
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
// Create the Session object
if (session == null) {
session = Session.getDefaultInstance(props, null);
if (verbose)
session.setDebug(true); // Verbose!
}
// create a message
final Message mesg = new MimeMessage(session);
InternetAddress[] addresses;
// TO Address list
addresses = new InternetAddress[toList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)toList.get(i));
mesg.setRecipients(Message.RecipientType.TO, addresses);
// From Address
mesg.setFrom(new InternetAddress(from));
// CC Address list
addresses = new InternetAddress[ccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)ccList.get(i));
mesg.setRecipients(Message.RecipientType.CC, addresses);
// BCC Address list
addresses = new InternetAddress[bccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)bccList.get(i));
mesg.setRecipients(Message.RecipientType.BCC, addresses);
// The Subject
mesg.setSubject(subject);
// Now the message body.
mesg.setText(body);
// Finally, send the message! (use static Transport method)
// Do this in a Thread as it sometimes is too slow for JServ
// new Thread() {
// public void run() {
// try {
Transport transport =session.getTransport("smtp");
transport.connect(mailHost, "zz3zcwb", "123456");
transport.sendMessage(mesg, mesg.getAllRecipients());
// } catch (MessagingException e) {
// throw new IllegalArgumentException(
// "Transport.send() threw: " + e.toString());
// }
// }
// }.start();
}
public static void send(String mailhost,String recipient, String sender, String subject, String message)
throws MessagingException {
Mailer m = new Mailer();
m.setServer(mailhost);
m.addTo(recipient);
m.setFrom(sender);
m.setSubject(subject);
m.setBody(message);
m.doSend();
}
protected ArrayList tokenize(String s) {
ArrayList al = new ArrayList();
StringTokenizer tf = new StringTokenizer(s, ",");
// For each word found in the line
while (tf.hasMoreTokens()) {
// trim blanks, and add to list.
al.add(tf.nextToken().trim());
}
return al;
}
public static void main(String args[]){
Mailer mailer=new Mailer();
mailer.setServer("smtp.163.com");
mailer.setFrom("zz3zcwb@163.com");
mailer.addTo("zz3zcwb@sina.com");
mailer.addTo("zz3zcwb@163.com");
mailer.setSubject("我发的信,");
mailer.setBody("I am OK,很好!请不要发大量拉圾邮件");
try{
mailer.doSend();
}catch (MessagingException e) {}
}
}
运行结果:
C:\java>javac Mailer.java
C:\java>java Mailer
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25
220 Coremail SMTP(Anti Spam) System (163com[20030606])
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO teacher
250-192.168.1.212
250-PIPELINING
250-AUTH LOGIN PLAIN NTLM
250-AUTH=LOGIN PLAIN NTLM
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN NTLM"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
enozemN3Yg==
334 UGFzc3dvcmQ6
MTIzNDU2
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<zz3zcwb@163.com>
250 Ok
RCPT TO:<zz3zcwb@sina.com>
250 Ok
RCPT TO:<zz3zcwb@163.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: zz3zcwb@sina.com
DEBUG SMTP: zz3zcwb@163.com
DATA
354 End data with <CR><LF>.<CR><LF>
Message-ID: <813251.1098515605463.JavaMail.cwb@teacher>
From: zz3zcwb@163.com
To: zz3zcwb@sina.com, zz3zcwb@163.com
Subject: =?GBK?B?ztK3orXE0MUs?=
Mime-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
SSBhbSBPS6Osuty6w6Ohx+uyu9Kqt6K088G/wK27+NPKvP4=
.
250 Ok: queued as KcCN948EekFKVOYA.1
C:\java>
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class Mailer {
/** javamail session对象. */
protected Session session;
/** 发信地址 */
protected String from;
/** 信的标题*/
protected String subject;
/** 存放字符串形式的收信地址 */
protected ArrayList toList = new ArrayList();
/** 抄送地址*/
protected ArrayList ccList = new ArrayList();
/** 暗送地址 */
protected ArrayList bccList = new ArrayList();
/** 信的文本内容*/
protected String body;
/** SMTP服务器地址*/
protected String mailHost;
/**是否在控制台打印信息*/
protected boolean verbose=true;
/** Get from */
public String getFrom() {
return from;
}
public void setFrom(String fm) {
from = fm;
}
public String getSubject() {
return subject;
}
public void setSubject(String subj) {
subject = subj;
}
public ArrayList getToList() {
return toList;
}
public void setToList(ArrayList to) {
toList = to;
}
public void setToList(String s) {
toList = tokenize(s);
}
public void addTo(String to) {
toList.add(to);
}
public ArrayList getCcList() {
return ccList;
}
public void setCcList(ArrayList cc) {
ccList = cc;
}
public void setCcList(String s) {
ccList = tokenize(s);
}
public void addCc(String cc) {
ccList.add(cc);
}
public ArrayList getBccList() {
return bccList;
}
public void setBccList(ArrayList bcc) {
bccList = bcc;
}
public void setBccList(String s) {
bccList = tokenize(s);
}
public void addBcc(String bcc) {
bccList.add(bcc);
}
public String getBody() {
return body;
}
public void setBody(String text) {
body = text;
}
public boolean isVerbose() {
return verbose;
}
public void setVerbose(boolean v) {
verbose = v;
}
public boolean isComplete() {
if (from == null || from.length()==0) {
System.err.println("doSend: no FROM");
return false;
}
if (subject == null || subject.length()==0) {
System.err.println("doSend: no SUBJECT");
return false;
}
if (toList.size()==0) {
System.err.println("doSend: no recipients");
return false;
}
if (body == null || body.length()==0) {
System.err.println("doSend: no body");
return false;
}
if (mailHost == null || mailHost.length()==0) {
System.err.println("doSend: no server host");
return false;
}
return true;
}
public void setServer(String s) {
mailHost = s;
}
/** Send the message.
*/
public synchronized void doSend() throws MessagingException {
if (!isComplete())
throw new IllegalArgumentException("doSend called before message was complete");
Properties props = new Properties();
props.put("mail.smtp.host", mailHost);
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
// Create the Session object
if (session == null) {
session = Session.getDefaultInstance(props, null);
if (verbose)
session.setDebug(true); // Verbose!
}
// create a message
final Message mesg = new MimeMessage(session);
InternetAddress[] addresses;
// TO Address list
addresses = new InternetAddress[toList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)toList.get(i));
mesg.setRecipients(Message.RecipientType.TO, addresses);
// From Address
mesg.setFrom(new InternetAddress(from));
// CC Address list
addresses = new InternetAddress[ccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)ccList.get(i));
mesg.setRecipients(Message.RecipientType.CC, addresses);
// BCC Address list
addresses = new InternetAddress[bccList.size()];
for (int i=0; i<addresses.length; i++)
addresses[i] = new InternetAddress((String)bccList.get(i));
mesg.setRecipients(Message.RecipientType.BCC, addresses);
// The Subject
mesg.setSubject(subject);
// Now the message body.
mesg.setText(body);
// Finally, send the message! (use static Transport method)
// Do this in a Thread as it sometimes is too slow for JServ
// new Thread() {
// public void run() {
// try {
Transport transport =session.getTransport("smtp");
transport.connect(mailHost, "zz3zcwb", "123456");
transport.sendMessage(mesg, mesg.getAllRecipients());
// } catch (MessagingException e) {
// throw new IllegalArgumentException(
// "Transport.send() threw: " + e.toString());
// }
// }
// }.start();
}
public static void send(String mailhost,String recipient, String sender, String subject, String message)
throws MessagingException {
Mailer m = new Mailer();
m.setServer(mailhost);
m.addTo(recipient);
m.setFrom(sender);
m.setSubject(subject);
m.setBody(message);
m.doSend();
}
protected ArrayList tokenize(String s) {
ArrayList al = new ArrayList();
StringTokenizer tf = new StringTokenizer(s, ",");
// For each word found in the line
while (tf.hasMoreTokens()) {
// trim blanks, and add to list.
al.add(tf.nextToken().trim());
}
return al;
}
public static void main(String args[]){
Mailer mailer=new Mailer();
mailer.setServer("smtp.163.com");
mailer.setFrom("zz3zcwb@163.com");
mailer.addTo("zz3zcwb@sina.com");
mailer.addTo("zz3zcwb@163.com");
mailer.setSubject("我发的信,");
mailer.setBody("I am OK,很好!请不要发大量拉圾邮件");
try{
mailer.doSend();
}catch (MessagingException e) {}
}
}
运行结果:
C:\java>javac Mailer.java
C:\java>java Mailer
DEBUG: setDebug: JavaMail version 1.3.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.s
mtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.163.com", port 25
220 Coremail SMTP(Anti Spam) System (163com[20030606])
DEBUG SMTP: connected to host "smtp.163.com", port: 25
EHLO teacher
250-192.168.1.212
250-PIPELINING
250-AUTH LOGIN PLAIN NTLM
250-AUTH=LOGIN PLAIN NTLM
250 8BITMIME
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN NTLM"
DEBUG SMTP: Found extension "AUTH=LOGIN", arg "PLAIN NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
enozemN3Yg==
334 UGFzc3dvcmQ6
MTIzNDU2
235 Authentication successful
DEBUG SMTP: use8bit false
MAIL FROM:<zz3zcwb@163.com>
250 Ok
RCPT TO:<zz3zcwb@sina.com>
250 Ok
RCPT TO:<zz3zcwb@163.com>
250 Ok
DEBUG SMTP: Verified Addresses
DEBUG SMTP: zz3zcwb@sina.com
DEBUG SMTP: zz3zcwb@163.com
DATA
354 End data with <CR><LF>.<CR><LF>
Message-ID: <813251.1098515605463.JavaMail.cwb@teacher>
From: zz3zcwb@163.com
To: zz3zcwb@sina.com, zz3zcwb@163.com
Subject: =?GBK?B?ztK3orXE0MUs?=
Mime-Version: 1.0
Content-Type: text/plain; charset=GBK
Content-Transfer-Encoding: base64
SSBhbSBPS6Osuty6w6Ohx+uyu9Kqt6K088G/wK27+NPKvP4=
.
250 Ok: queued as KcCN948EekFKVOYA.1
C:\java>
发表评论
-
tomcat手动配置
2011-01-06 12:03 779为了快速开发,不用Eclipse部署项目,而用手动配置,优点启 ... -
Java Socket编程
2011-01-04 11:56 674第一步 充分理解Socket ... -
Java类加载以及反射
2011-01-04 11:54 613JAVA中类文件加载是动态的。JVM指令是被封装在了.clas ... -
多线程JavaSocket示例
2011-01-04 11:53 813这篇做为学习孙卫琴<<Java网络编程精解> ... -
Java过滤器
2011-01-04 11:50 702一、 使浏览器不缓存页面的过滤器 import javax.s ... -
Java中的String类(二)
2011-01-04 11:31 7342、String的常用操作方法 2.1、字符与字符串 ... -
Java中的String类(一)
2011-01-04 11:30 6521、String类 1.1、String的两种实例化方式 ... -
Java位移运算符原理
2011-01-04 11:27 901* 总体概念: * 1.位移运算符只对位进行操作 * ... -
Java堆栈
2011-01-04 11:24 5981 . 栈(stack)与堆(heap)都是Java用来在Ra ... -
Java字符串比较
2011-01-04 11:22 1328字符串比较 Java代码 String str1=&quo ... -
Java读取Properties文件方法
2011-01-04 11:16 671使用J2SE API读取Properties文件的六种方法 ... -
学习Java30个目标
2011-01-04 11:09 5861.你需要精通面向对象分析与设计(OOA/OOD)、设计模式( ... -
Java时间类以及函数
2011-01-04 10:27 6181.计算某一月份的最大天数 Calendar time=Ca ... -
Java入门需要掌握的30个基本概念
2011-01-04 10:06 644这是在网上看到的一份 ...
相关推荐
本文将详细解析如何利用Access构建一个群发邮件程序,以及相关的关键技术点。 首先,"access群发邮件程序"指的是使用Access数据库来设计并执行批量发送电子邮件的系统。Access本身并不直接支持邮件功能,但可以通过...
下面将详细讲解如何实现一个群发邮件程序,并涉及相关技术知识点。 1. **SMTP(Simple Mail Transfer Protocol)**:SMTP是互联网上标准的邮件传输协议。通过SMTP,邮件客户端可以将邮件发送到邮件服务器,然后由...
C#群发邮件程序是一种基于C#编程语言开发的应用,主要功能是实现批量发送电子邮件。在电子邮件营销、通知发送或日常工作中,这种程序能够极大提高效率,避免手动逐一发送的繁琐过程。它允许用户导入Excel(通常是...
摘要:VB源码,网络相关,群发邮件 VB群发邮件程序源码,建立在单个邮件发送基础之上,群发邮件是一个常见的网络应用,本源代码供VB初学者参考学习。 信息来源:乐乐源码网(www.lelecode.com)
提供的"QunFa"压缩包文件可能包含了一个简单的C#群发邮件程序。下载并解压后,通过阅读代码可以了解实际实现的细节,如错误处理、日志记录等。 总结,使用C#进行群发邮件涉及到SMTP协议的运用、MailMessage对象的...
【C#邮件群发winform程序】是一款基于C#编程语言和.NET框架开发的应用程序,专为实现电子邮件的批量发送而设计。它提供了一个用户友好的Windows桌面界面,方便用户进行邮件管理和群发操作。以下是该程序的关键知识点...
资源名:邮件群发系统ASP程序源码 资源类型:程序源代码 源码说明:1. 多个邮件地址发送邮件 2.可以选择发邮件人 3.后台添加email地址 3.access等中导入email地址 4.添加多组SMTP服务器,和邮件内容 5.html,文本...
k780邮件群发程序更新日志 ========2012-05-12======== --新增-------------- 1.新增邮件内容编辑器。 2.可在系统设置中设置是否启用登录保护。 3.邮箱分类中显示该分类中邮箱条数。 --修改-------------- 1.修正因...
标题中的“Excel导入群发邮件源码”指的是一个利用C#编程语言开发的软件功能,其核心目的是将Excel文件中的数据作为输入,批量发送电子邮件。这个功能通常在需要向大量收件人发送相同或定制信息时使用,比如企业内部...
在系统中,`Global.asax`文件是ASP.NET应用程序的全局应用事件处理程序,它定义了应用程序生命周期中的关键事件,如启动、结束、会话开始和结束等,可能在这个群发邮件的程序中,它用于设置一些全局的初始化配置或...
<... <TITLE> UNTITLED </TITLE> <?... if ($subject) { $query = select name,email from user; $res = mysql_query($query); $row = mysql_fetch_array($res); while ($row) { mail($row[’email’]...
- **反垃圾邮件策略**:频繁的群发邮件可能会被识别为垃圾邮件,导致邮件被屏蔽或者IP地址被列入黑名单。因此,合理控制发送频率、提供退订选项和确保用户同意接收邮件是必要的。 - **邮件格式兼容性**:不同的邮件...
在互联网营销、用户通知或客户服务等场景中,群发邮件是一个常见且实用的需求。通过PHP来实现这一功能,可以有效地自动化这个过程,提高效率。 在PHP中,发送邮件主要依赖于`mail()`函数,这是一个内置的PHP函数,...
Delphi邮件群发程序是一种基于Delphi编程环境开发的应用,用于高效地向多个收件人发送电子邮件。Delphi是Borland公司(现Embarcadero Technologies)开发的一款集成开发环境(IDE),它使用Object Pascal作为主要...
该源文件是一个基于MFC(Microsoft Foundation Classes)的桌面应用程序,用于实现表格明细群发邮件的功能。MFC是微软提供的一套C++类库,它为开发者提供了构建Windows应用程序的框架,大大简化了Windows API的使用...
邮件群发后台服务程序是一种专门设计用于批量发送电子邮件的应用程序,它在后台默默地运行,无需用户直接交互。这种服务程序通常被企业或营销团队用来高效地分发新闻通讯、产品更新、促销信息或其他营销材料。在本文...
群发邮件源码是用于批量发送电子邮件的程序代码,它可以帮助用户一次性向多个收件人发送相同的或定制的邮件内容,常被用在营销、通知或者信息传递等场景。了解和掌握群发邮件源码的知识点,对于进行电子邮件营销或者...
**PHPMailer是一个强大的PHP邮件发送...通过PHPMailer,开发者可以轻松地在PHP应用中实现邮件发送功能,包括HTML邮件、附件、SMTP验证以及群发邮件。理解并熟练运用这些知识点,能有效提升PHP邮件处理的效率和安全性。
邮件群发程序,由Java编写.程序由本人原创,如有问题可加我QQ(12356918)交流.
程序含有源代码 该程序是针对邮件推广应用而编写的,开发环境是Win98+Access97+Delphi,在Win98和Win2000 中测试过、都正常运行,能够准确群发邮件。使用程序时要设置SMTP服务器、端口、用户名、发送方的邮件地址、...