Как установить обычную аутентификацию http-имя пользователя / пароль в java почте?

Я использую 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 ответов

Другие вопросы по тегам