如何在java邮件中设置http代理用户名/密码基本身份验证?

时间:2015-01-06 18:56:29

标签: java email http-proxy proxy-server

我正在使用javamail发送邮件(SMTP协议),如下所示:

import java.util.Properties; 
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailManager {

private static Session session;
final static String from = my_email;
final static String passwd = my_password;
 //for basic authentication
final static String usern = proxy_username;
final static String pas = proxy_password;

static {
    Properties props = System.getProperties();
    props.setProperty("proxySet", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socks.host","172.31.100.14");    //proxy host
    props.put("mail.smtp.socks.port","3128");         //proxy port
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    session = Session.getInstance(props,
            new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, passwd);
        }
    });
    System.setProperty("java.net.socks.username", usern);             
    System.setProperty("java.net.socks.password", pas);
}

public static boolean sendMail(String to) {
    boolean flag = true;

    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setText(content);
        Transport.send(message);
    } catch (Exception e) {
        flag = false;
        System.out.println(e);
    }
    return flag;
}
}

这是一个错误:

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
 nested exception is:
  java.net.SocketException: Malformed reply from SOCKS server

关于发生了什么的任何想法?

0 个答案:

没有答案