http://www.vckbase.com/document/viewdoc/?id=684
http://www.vckbase.com/document/viewdoc/?id=712按照VCKBASE上面方法不能发送成功,怎么回事? 很久以前我试过这个,好象是可以用的难道是现在smtp服务器屏蔽了jmail发送的邮件?

解决方案 »

  1.   

    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.BodyPart;
    import javax.mail.Multipart;
    import javax.mail.Message;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMultipart;
    import java.util.Properties;
    import java.util.Date;
    import java.io.File;
    import java.io.ByteArrayOutputStream;
    import java.io.PrintStream;
    import java.io.FileOutputStream;
    import javax.activation.FileDataSource;
    import javax.activation.DataHandler;
    //import com.posisoft.jdavmail.*;public class MailSend extends javax.mail.Authenticator
    {
    private String smtp_username ;
    private String smtp_password ;
    public MailSend(String usr,String p)
    {
    set_smtp_auth(usr,p);
    }
    public MailSend()
    {
    }
    private void set_smtp_auth(String usr,String pwd)
    {
    smtp_username = usr;
    smtp_password = pwd;
    }
    private static String exceptionFileName = "";
    static void setExceptionFileName(String s)
    {
    exceptionFileName = s;
    }

    public javax.mail.PasswordAuthentication getPasswordAuthentication()
    {
    return new javax.mail.PasswordAuthentication(smtp_username, smtp_password);
    }
       public static String exception_tostring(Exception e, String filename)
       {
        String sret = null;
        try
        {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    PrintStream ps = new PrintStream(bos);
    e.printStackTrace(ps); 
    ps.flush(); 
    ps.close(); 
    bos.close(); 
    sret = bos.toString();
    if(filename!=null&&filename.length()>0)
    {
    FileOutputStream flog = new FileOutputStream(filename);
    flog.write(bos.toByteArray());
    flog.close();
    }
        }
        catch(Exception e2)
        {
        sret = sret +" can't get exception description";
        }
    return sret ;    
       }
       public static Properties getProps(String smtp_server,String smtp_port,
       String bauth)
       {
        Properties props = System.getProperties();
        if(smtp_server!=null)
        props.put("mail.smtp.host", smtp_server);
        if(smtp_port!=null)
        props.put("mail.smtp.port", smtp_port);
        if(bauth!=null)
        props.put("mail.smtp.auth", bauth);
        return props;
       
       }
    public static String send_smtp_mail_native(
    javax.mail.Session session,
    String from_mail,
    String to_mail,
    String subject, String body_text, String attach_file_name
    )
    {
    if(session==null)
    return "Error when send SMTP mail:null session";
    try
    {     
    session.setDebug(false);
    Message message = prepareMimeMessage(from_mail,to_mail,subject,body_text,attach_file_name,session);
    Transport.send(message);
    }
    catch(Exception ex)
    {
    return exception_tostring(ex,exceptionFileName);
    }
    return "Mail was sent out successfully!";
    }
    public static String send_hotmail_native(String hotmail_account, String mail_pwd, String receive_mail,
    String subject,String body,String attach_file_name,
    Session ses, Transport transport)
    {
    if(hotmail_account ==null || mail_pwd==null || receive_mail==null)
    return "send mail error:Invalid account information";
    if(subject==null && body==null && attach_file_name==null)
    return "send mail error:Invalid message";
    try
    {
    transport.connect(null, hotmail_account, mail_pwd);
      // Prepare the message
      MimeMessage txMsg = prepareMimeMessage(hotmail_account,receive_mail,
      subject,body,attach_file_name,ses);
      transport.sendMessage(txMsg, txMsg.getAllRecipients());

    catch (Exception ex) 
    {
    return exception_tostring(ex,exceptionFileName);
    }
    return "Mail was sent successfully!";
    }
      

  2.   

    private static MimeMessage prepareMimeMessage(
    String hotmail_account, String receive_mail,
    String subject,String body,String attach_file_name,
    Session session) throws Exception
    {
    MimeMessage mime_msg = new MimeMessage(session);
    if(subject!=null)
    mime_msg.setSubject(subject);
    else
    mime_msg.setSubject("(no subject)");
    String mail_body="";
    if(body!=null)
    mail_body = (body);
    else
    mail_body = ("(empty)");
    boolean has_attach_file = false;
    if(attach_file_name!=null && attach_file_name.length()>0)
    {
    File tmp = new File(attach_file_name);
    if (!tmp.exists() || !tmp.canRead() || tmp.isDirectory())
    {
    mail_body += " attach file:";
    mail_body += attach_file_name;
    mail_body += "  read error";
    }
    else
    has_attach_file = true;
    }
    InternetAddress addrFrom = new InternetAddress(hotmail_account);
    mime_msg.setFrom(addrFrom);
    InternetAddress addrTo = new InternetAddress(receive_mail, receive_mail);
    mime_msg.addRecipient(Message.RecipientType.TO, addrTo);
    if(has_attach_file)
    {
    BodyPart messageBodyPart = new MimeBodyPart();
    messageBodyPart.setText(mail_body);
    Multipart mp = new MimeMultipart();
    mp.addBodyPart(messageBodyPart);
    ////////////////////attach a file
    MimeBodyPart mbp=new MimeBodyPart();
    FileDataSource fds=new FileDataSource(attach_file_name);
    mbp.setDataHandler(new DataHandler(fds));
    mbp.setFileName(fds.getName());
    mp.addBodyPart(mbp);
    mime_msg.setContent(mp);
    System.out.println("attach file:"+attach_file_name);
    }
    else
    {
    mime_msg.setText(mail_body);
    }
    mime_msg.setSentDate(new Date());
    return mime_msg;
    }
    public static void setSocksProxy(String server, String port)
    {
    if(server==null || port==null)
    {
    System.setProperty("socksProxySet", "false");
    }
    else
    {
    System.setProperty("socksProxySet", "true");
    System.setProperty("socksProxyHost", server);
    System.setProperty("socksProxyPort", port);
    }
    }
    }