`
eyezgx
  • 浏览: 2135 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

尝试JAVA通过HTTP代理发送邮件,居然出错

阅读更多
    由于客户需要,本人开始尝试JAVA编码通过HTTP代理发送邮件。但一直出现“Sending the email to the following server failed : smtp.163.com:25”的异常错误。非常纠结,如果有高人看见这篇文章,请指点!

     下面是具体实现方法:

public String sendMailByProxy(String mailHostName,String userName,String passWord,String sender,ArrayList recipientslist,ArrayList ccList,ArrayList bccList,String title,String content,ArrayList aFileAffixes,boolean delFile){
String ret = "0000发送成功";
try {
String proxyHost = ClientInfo.getClientComCfgPValue("mail.proxyHost","");  //代理服务器IP
String proxyPort = ClientInfo.getClientComCfgPValue("mail.proxyPort","");  //代理服务器端口

String checkUserPass = ClientInfo.getClientComCfgPValue("mail.checkUserAndPass","Y");  //是否验证用户名、密码

final String usernames = userName;
    final String passwords = passWord;

//设置代理服务器
Properties props = System.getProperties();
props.setProperty("proxySet", "true");
    props.setProperty("http.proxyHost", proxyHost);
    props.setProperty("http.proxyPort", proxyPort);
    props.setProperty("mail.smtp.host", mailHostName);

    props.put("mail.smtp.auth", "true");

//使用验证
Session session = null;
if(checkUserPass == "Y" || "Y".equals(checkUserPass)){
session = Session.getInstance(props,
             new Authenticator() {
                 protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                     return new javax.mail.PasswordAuthentication(usernames,
                    passwords);
                  }
              });
}

HtmlEmail mail = new HtmlEmail();
mail.setMailSession(session);
mail.setFrom(sender);   //发件人

ArrayList toList = getSplitArrayList(recipientslist);
if(toList!=null && toList.size()>0){
for(int i = 0; i < toList.size(); i++){
mail.addTo(toList.get(i).toString());     //收件人
}
}

ArrayList cList = getSplitArrayList(ccList);
if(cList!=null && cList.size()>0){
for(int i = 0; i < cList.size(); i++){
mail.addCc(cList.get(i).toString());     //抄送人
}
}

ArrayList bcList = getSplitArrayList(bccList);
if(bcList!=null && bcList.size()>0){
for(int i = 0; i < bcList.size(); i++){
mail.addBcc(bcList.get(i).toString());    //秘密抄送人
}
}

mail.setCharset("GB2312");  //邮件标题和内容中文转码
mail.setSubject(title);     //标题
mail.setHtmlMsg("<p align="+"center"+"><b><u>"+content+"</u></b></p>");     //内容

if(aFileAffixes!=null && aFileAffixes.size()>0){
for(int i=0;i<aFileAffixes.size();i++){
String filePath = aFileAffixes.get(i).toString();    //附件路径(包括附件名)
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1);   //附件名称

EmailAttachment att = new EmailAttachment();   //附件对象
att.setPath(filePath);
att.setDisposition(EmailAttachment.ATTACHMENT);
att.setDescription(fileName);
att.setName(MimeUtility.encodeText(fileName));   //将附件名称进行转码(以防中文名称导致乱码)

mail.attach(att);     //附件
}
}

mail.send();


if(delFile){
if(aFileAffixes!=null){
for(int i=0;i<aFileAffixes.size();i++){
File f = new File(aFileAffixes.get(i).toString());
if(f.isFile()){
f.delete();
}
}
}
}

} catch (Exception err) {
err.printStackTrace();
ret = "9999Email发送异常["+err.getMessage()+"]";
if(delFile){
if(aFileAffixes!=null){
for(int i=0;i<aFileAffixes.size();i++){
File f = new File(aFileAffixes.get(i).toString());
if(f.isFile()){
f.delete();
}
}
}
}
}
return ret;
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics