日期:2014-05-18  浏览次数:20761 次

JAVAMAIL怎么使用代理发送邮件
JAVAMAIL怎么使用代理发送邮件,而代理软件要用哪种类型的,能不能例举两个可用的软件.

------解决方案--------------------
得用JAVAMAIL设置一个代理服务器:
Properties props=System.getProperties();//获取系统属性
props.put("http.proxySet","true");//设置代理主机参数
props.put("http.proxyHost","172.28.2.1");
props.put("http.proxyPort","85");
props.put("mail.smtp.host",mailhost);
------解决方案--------------------
你只要能上网就能发邮件,给你一个例子:
Java code
            // 创建 properties ,里面包含了发送邮件服务器的地址。 
            Properties mailProps = new Properties(); 
            final String usr=(String)servletContext.getInitParameter("SendMailFrom");//这里填写你发信者的邮箱地址
            final String pwd=(String)servletContext.getInitParameter("SendMailPassword");//这里填写你发信者的邮箱密码
            mailProps.put("mail.smtp.host", (String)servletContext.getInitParameter("SendMailServer")); //"mail.smtp.host"随便叫啥都行,"192.0.0.1"必须是真实可用的。 
            mailProps.put("mail.smtp.user",usr);
            mailProps.put("mail.smtp.password",pwd);
            mailProps.put("mail.smtp.auth","true");
            // 创建 session 
            // 关键new Authenticator()。如果你的邮箱是SMTP验证的,就得这么写。否则会报错。Session.getInstance(props)这个方法是针对SMTP不要求验证的。
            // 创建 邮件的message,message对象包含了邮件众多有的部件,都是封装成了set方法去设置的 
            Session mailSession = Session.getInstance(mailProps,new Authenticator(){
                public PasswordAuthentication getPasswordAuthentication(){
                return new PasswordAuthentication(usr,pwd); }//这里填写你发信者的邮箱地址和密码
                });
            MimeMessage message = new MimeMessage(mailSession); 
            // 设置发信人 
            message.setFrom(new InternetAddress((String)servletContext.getInitParameter("SendMailFrom"))); 
            // 收信人 
            message.setRecipient(Message.RecipientType.TO,new InternetAddress(address)); 
            // 邮件标题 
            message.setSubject(subject); //haha,吓唬人 
            // 创建 Mimemultipart,这是包含多个附件是必须创建的。如果只有一个内容,没有附件,可以直接用message.setText(String str) 
            //去写信的内容,比较方便。附件等于是要创建多个内容,往下看更清晰。 
            MimeMultipart multi = new MimeMultipart(); 
            // 创建 BodyPart,主要作用是将以后创建的n个内容加入MimeMultipart.也就是可以发n个附件。我这里有2个BodyPart. 
            BodyPart textBodyPart = new MimeBodyPart(); //第一个BodyPart.主要写一些一般的信件内容。 
            textBodyPart.setText(mess); 
            // 压入第一个BodyPart到MimeMultipart对象中。 
            multi.addBodyPart(textBodyPart); 
             // 如果文件名存在就创建第二个BodyPart,是一个FileDAtaSource
            if(filename!=null&&!filename.equals("")){
                File file= new File(filename);
                if(file.isFile()){//如果文件确实存在
                    FileDataSource fds = new FileDataSource(filename);//必须存在的文档,否则throw异常。 
                    BodyPart fileBodyPart = new MimeBodyPart(); //第二个BodyPart 
                    fileBodyPart.setDataHandler(new DataHandler(fds)); //字符流形式装入文件 
                    fileBodyPart.setFileName("newfilename"); //设置文件名,可以不是原来的文件名。 
                    multi.addBodyPart(fileBodyPart); 
                }
            }
            // MimeMultPart作为Content加入message 
            message.setContent(multi); 
            // 所有以上的工作必须保存。 
            message.saveChanges(); 
            // 发送,利用Transport类,它是SMTP的邮件发送协议, 
            Transport.send(message);

------解决方案--------------------
探讨
得用JAVAMAIL设置一个代理服务器:
Properties props=System.getProperties();//获取系统属性
props.put("http.proxySet","true");//设置代理主机参数
props.put("http.proxyHost","172.28.2.1");
props.put("http.proxyPort","85");
props.put("mail.smtp.host",mailhost);