尝试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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值