只发给一个人成功了
sendmail.setTo("[email protected]");
如果要分给2个人,如
sendmail.setTo("[email protected];[email protected]");
就出错。这2个邮箱都是有效的。我用;号和,号都试过了,都失败。
正常的写错是什么?

解决方案 »

  1.   

    /**
    * 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()就可以了,不过有点麻烦。
      

  2.   

    下面的程序是贴别人的,可以实现群发的。
    中间用逗号做为分格符。
    /*
    * 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();
    }
    }
      

  3.   

    你这个方法带你个参数的?建议写成SetTo(String a,String b,......)
      

  4.   

    谢谢。
    请问群发是否一定要stmp认证?
      

  5.   

    stmp认证时填的用户和密码是填什么呀?
    例如我用的是公司的邮箱,
    props.put("mail.smtp.host",hostname)时填的是公司的SMTP
    ,密码是kkk
    那我的用户名和密码是填什么(即用户名是填abc还是[email protected])?
    另:
    请问群发是否一定要stmp认证?