使用Javascript实现电子邮件群发功能
部分代码参考网络资料
必备条件是:客户端安装Jmail组件。
收件人的电子邮件地址列表使用 逗号"," 分割。 目前这个群发功能在 IE 6.0 下测试通过。
主要代码如下:
<script type="text/javascript">
function $( id ){return document.getElementById( id );}
function getValueById( id ) { return document.getElementById(id).value; }
// 检查表单的值是否为空
function ChkFormValue()
{
if( $('sys_username').value == '') { alert('发送邮件的 用户名不能为空。'); $('sys_username').focus(); return false; }
if( $('sys_emailAddress').value == '') { alert('发送邮件的 邮件地址不能为空。'); $('sys_emailAddress').focus(); return false; }
if( $('sys_password').value == '') { alert('发送邮件的 密码不能为空。'); $('sys_password').focus(); return false; }
if( $('sys_Smtp').value == '') { alert('发送邮件的 smtp服务器地址不能为空。'); $('sys_Smtp').focus(); return false; }
if( $('id_Mail').value == '') { alert('收件人 邮件地址不能为空。'); $('id_Mail').focus(); return false; }
if( $('id_Subject').value == '') { alert('邮件的主题 不能为空。'); $('id_Subject').focus(); return false; }
if( $('id_Body').value == '') { alert('发送邮件的内容 不能为空。'); $('id_Body').focus(); return false; }
return true;
}
SetDefaultValue('1'); // 1 使用系统默认的邮件配置信息,发送邮件,0 清空配置信息
function SetDefaultValue(mySwitch)
{
if (mySwitch == '1')
{
document.getElementById('sys_username').value = 'test@163.com';
document.getElementById('sys_emailAddress').value = 'test@163.com';
document.getElementById('sys_password').value = '123456';
document.getElementById('sys_Smtp').value = 'smtp.163.com';
}
else
{
document.getElementById('sys_username').value = '';
document.getElementById('sys_emailAddress').value = '';
document.getElementById('sys_password').value = '';
document.getElementById('sys_Smtp').value = '';
}
}
//点击 "发送邮件" 按钮之后,调用这个函数
function sendMail()
{
if (!ChkFormValue()) { return false; } //首先检测表单的内容是否正确
var mailAddressList = getValueById( "id_Mail" ).split(","); //获得发送的email地址的列表
//首先清空上次的发送状态记录
$('clientShowDo').innerHTML = '';
$('clientShowSuccess').innerHTML = "0" ;
$('clientShowFail').innerHTML = "0" ;
$('clientShowFailList').innerHTML = "失败列表:" ;
$('clientShowFailDescrip').innerHTML = "失败描述:"
for(var i=0; i<mailAddressList.length; i++) //根据分割","得到的数组,循环发送Email
{
// alert(mailAddressList[i]);
DosendMail(mailAddressList[i]);
}
alert('发送完毕');
$('id_Mail').value = '';
$('id_Body').value = '';
}
//发送邮件的主函数
function DosendMail(mailAddressList)
{
$('clientShowDo').innerHTML=''
var result = false;
try
{
var objMail = new ActiveXObject( "JMail.Message" );
if ( !objMail )
{
alert( "客户端创建Jmail对象错误!" );
return false;
}
var subject = getValueById( "id_Subject" );
var body = getValueById( "id_Body" );
mailAddressList = mailAddressList.replace(new RegExp(" ","gm"),""); //去除邮件地址中可能存在的空格
objMail.Logging = true;
objMail.Charset = "GB2312";
objMail.Priority = 3;
objMail.ContentType = "text/html";
objMail.From = getValueById('sys_emailAddress');
objMail.FromName = "你的称呼";
objMail.AddRecipient( mailAddressList );
objMail.Subject = subject;
objMail.Body = body;
objMail.MailServerUserName = getValueById('sys_username');
objMail.MailServerPassword = getValueById('sys_password');
var sys_StmpAddr = getValueById('sys_Smtp');
objMail.Send( sys_StmpAddr );
result = true;
setTimeout("1200",$('clientShowDo').innerHTML='OK');
$('clientShowSuccess').innerHTML = parseInt($('clientShowSuccess').innerHTML) + 1 ; //显示已经发送成功的条数
//SetDefaultValue('0'); //清空默认的设置
}
catch ( e )
{
//alert( e.description );
//alert( objMail.Log );
$('clientShowFail').innerHTML = parseInt($('clientShowFail').innerHTML) + 1 ; //错误数加1
$('clientShowFailList').innerHTML = $('clientShowFailList').innerHTML + "<br>" + mailAddressList ; //追加到错误列表
$('clientShowFailDescrip').innerHTML = $('clientShowFailDescrip').innerHTML + "<hr>" + objMail.Log //显示详细的错误信息
}
return result;
}
</script>
function $( id ){return document.getElementById( id );}
function getValueById( id ) { return document.getElementById(id).value; }
// 检查表单的值是否为空
function ChkFormValue()
{
if( $('sys_username').value == '') { alert('发送邮件的 用户名不能为空。'); $('sys_username').focus(); return false; }
if( $('sys_emailAddress').value == '') { alert('发送邮件的 邮件地址不能为空。'); $('sys_emailAddress').focus(); return false; }
if( $('sys_password').value == '') { alert('发送邮件的 密码不能为空。'); $('sys_password').focus(); return false; }
if( $('sys_Smtp').value == '') { alert('发送邮件的 smtp服务器地址不能为空。'); $('sys_Smtp').focus(); return false; }
if( $('id_Mail').value == '') { alert('收件人 邮件地址不能为空。'); $('id_Mail').focus(); return false; }
if( $('id_Subject').value == '') { alert('邮件的主题 不能为空。'); $('id_Subject').focus(); return false; }
if( $('id_Body').value == '') { alert('发送邮件的内容 不能为空。'); $('id_Body').focus(); return false; }
return true;
}
SetDefaultValue('1'); // 1 使用系统默认的邮件配置信息,发送邮件,0 清空配置信息
function SetDefaultValue(mySwitch)
{
if (mySwitch == '1')
{
document.getElementById('sys_username').value = 'test@163.com';
document.getElementById('sys_emailAddress').value = 'test@163.com';
document.getElementById('sys_password').value = '123456';
document.getElementById('sys_Smtp').value = 'smtp.163.com';
}
else
{
document.getElementById('sys_username').value = '';
document.getElementById('sys_emailAddress').value = '';
document.getElementById('sys_password').value = '';
document.getElementById('sys_Smtp').value = '';
}
}
//点击 "发送邮件" 按钮之后,调用这个函数
function sendMail()
{
if (!ChkFormValue()) { return false; } //首先检测表单的内容是否正确
var mailAddressList = getValueById( "id_Mail" ).split(","); //获得发送的email地址的列表
//首先清空上次的发送状态记录
$('clientShowDo').innerHTML = '';
$('clientShowSuccess').innerHTML = "0" ;
$('clientShowFail').innerHTML = "0" ;
$('clientShowFailList').innerHTML = "失败列表:" ;
$('clientShowFailDescrip').innerHTML = "失败描述:"
for(var i=0; i<mailAddressList.length; i++) //根据分割","得到的数组,循环发送Email
{
// alert(mailAddressList[i]);
DosendMail(mailAddressList[i]);
}
alert('发送完毕');
$('id_Mail').value = '';
$('id_Body').value = '';
}
//发送邮件的主函数
function DosendMail(mailAddressList)
{
$('clientShowDo').innerHTML=''
var result = false;
try
{
var objMail = new ActiveXObject( "JMail.Message" );
if ( !objMail )
{
alert( "客户端创建Jmail对象错误!" );
return false;
}
var subject = getValueById( "id_Subject" );
var body = getValueById( "id_Body" );
mailAddressList = mailAddressList.replace(new RegExp(" ","gm"),""); //去除邮件地址中可能存在的空格
objMail.Logging = true;
objMail.Charset = "GB2312";
objMail.Priority = 3;
objMail.ContentType = "text/html";
objMail.From = getValueById('sys_emailAddress');
objMail.FromName = "你的称呼";
objMail.AddRecipient( mailAddressList );
objMail.Subject = subject;
objMail.Body = body;
objMail.MailServerUserName = getValueById('sys_username');
objMail.MailServerPassword = getValueById('sys_password');
var sys_StmpAddr = getValueById('sys_Smtp');
objMail.Send( sys_StmpAddr );
result = true;
setTimeout("1200",$('clientShowDo').innerHTML='OK');
$('clientShowSuccess').innerHTML = parseInt($('clientShowSuccess').innerHTML) + 1 ; //显示已经发送成功的条数
//SetDefaultValue('0'); //清空默认的设置
}
catch ( e )
{
//alert( e.description );
//alert( objMail.Log );
$('clientShowFail').innerHTML = parseInt($('clientShowFail').innerHTML) + 1 ; //错误数加1
$('clientShowFailList').innerHTML = $('clientShowFailList').innerHTML + "<br>" + mailAddressList ; //追加到错误列表
$('clientShowFailDescrip').innerHTML = $('clientShowFailDescrip').innerHTML + "<hr>" + objMail.Log //显示详细的错误信息
}
return result;
}
</script>
代码文件下载:EmailClient.rar