只发给一个人成功了
sendmail.setTo("[email protected]");
如果要分给2个人,如
sendmail.setTo("[email protected];[email protected]");
就出错。这2个邮箱都是有效的。我用;号和,号都试过了,都失败。
正常的写错是什么?
sendmail.setTo("[email protected]");
如果要分给2个人,如
sendmail.setTo("[email protected];[email protected]");
就出错。这2个邮箱都是有效的。我用;号和,号都试过了,都失败。
正常的写错是什么?
解决方案 »
- 有没有大侠用过IEinspector这个工具,给个入门级的使用说明!谢谢!
- javascript验证问题
- 各位大侠们帮我看看这个错误吧我都快被整疯了
- 怎么才能一组一组循环显示?
- 循环读取表中数据,只能读取一条记录。附代码,请看看。(jsp)。谢谢
- jsp里如何判断text里的值是否是日期型?
- 問一下<Context docBase="C:/JavaTest/demo/webroot" path="/demo" reloadable="true">的意思!!
- 如果上一个页面的request里含一些特殊字符,怎样在这个页面得到?(这个问题没有搜索到正确的回复)
- 请高手指点:怎样运行servlet呢?
- JSP错误,在线等待
- ◆动态创建UIComponent(高手请进)
- JSP环境配置不知道哪里出了问题,请大家帮忙看看
* Set Addressee
*/
public boolean setTo(String name)
{
try{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(name));
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
首先声明一个数组,来保存你要群发的对象。然后你在程序中多调用几次setTo()就可以了,不过有点麻烦。
中间用逗号做为分格符。
/*
* Created on 2005-11-18
*/
package sendmail;import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.SendFailedException;
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;/**
* @author Xiang Yabin
*/
public class SendMail
{
private MimeMessage mimeMsg;
private Session session;
private Properties props;
private Multipart mp;
private String username = "";
private String password = "";public SendMail(String smtp)
{
this.setSmtpHost(smtp);
this.createMimeMessage();
}public void setSmtpHost(String hostname)
{
if(props == null)
{
props = System.getProperties();//Get system property object
}
props.put("mail.smtp.host",hostname);//Set SMTP
}public boolean createMimeMessage()
{
try{
session = Session.getDefaultInstance(props,null);//Get mail session object
mimeMsg = new MimeMessage(session);//Create Mime mail object
mp = new MimeMultipart();
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}public void setAuthentication(boolean need)
{
if(props == null)
{
props = System.getProperties();
}
if(need)
{
props.put("mail.smtp.auth","true");
}
else
{
props.put("mail.smtp.auth","false");
}
}public void setNameAndPass(String name,String pass)
{
username = name;
password = pass;
}public boolean setSubject(String subject)
{
try{
mimeMsg.setSubject(subject);//Set mail subject
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}public boolean setBody(String body)
{
try{
BodyPart bodypart = new MimeBodyPart();
bodypart.setContent(""+ body,"text/html;charset=gb2312");//Set mail send type
mp.addBodyPart(bodypart);
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}public boolean addAttachment(String filename)
{
try{
BodyPart bodypart = new MimeBodyPart();
FileDataSource fileDS = new FileDataSource(filename);
bodypart.setDataHandler(new DataHandler(fileDS));
bodypart.setFileName(MimeUtility.encodeText(fileDS.getName()));
mp.addBodyPart(bodypart);
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* Set Sender
*/
public boolean setFrom(String name)
{
try{
mimeMsg.setFrom(new InternetAddress(name,"china Commodity net"));
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* Set Addressee
*/
public boolean setTo(String name)
{
try{
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(name));
return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}public boolean send()
{
try{
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("is Sending....");
Session mailsession = Session.getInstance(props,null);
Transport transport = mailsession.getTransport("smtp");
transport.connect((String)props.get("smtp.mail.host"),username,password);
transport.sendMessage(mimeMsg,mimeMsg.getRecipients(Message.RecipientType.TO));
System.out.println("Success");
transport.close();
return true;
}
catch(SendFailedException e)
{
Address[] address = e.getValidUnsentAddresses();
SQLConnection sql = new SQLConnection();
Connection con = sql.getConnection();
try{
PreparedStatement ps = null;
ResultSet rs = null;
for(int i=0; i{
ps = con.prepareStatement("insert into no_send_mail(unsend) values(?)");
ps.setString(1,address[i].toString());
rs = ps.executeQuery();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
System.out.println("mail no send"+address.length);
address = e.getInvalidAddresses();
try{
PreparedStatement ps = null;
ResultSet rs = null;
for(int i=0; i{
ps = con.prepareStatement("insert into no_send_mail(invalid) values(?)");
ps.setString(1,address[i].toString());
rs = ps.executeQuery();
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
System.out.println("mail is invalid "+address.length);
return false;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
}
public static void main(String [] args)
{
SendMail sendmail = new SendMail("smtp.163.com");
sendmail.setAuthentication(true);
sendmail.setNameAndPass("用户名","密码");//用户名,和密码我就不写了。
sendmail.setSubject("this is a test 这是一个测试");
sendmail.setBody("this is a test 这是一个测试");
//sendmail.addAttachment("C:/boot.ini");//代表发送附件
sendmail.setFrom("[email protected]");
sendmail.setTo("[email protected],[email protected]");//如果群发邮件,用逗号分开
sendmail.send();
}
}
请问群发是否一定要stmp认证?
例如我用的是公司的邮箱,
props.put("mail.smtp.host",hostname)时填的是公司的SMTP
,密码是kkk
那我的用户名和密码是填什么(即用户名是填abc还是[email protected])?
另:
请问群发是否一定要stmp认证?