版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010687717/article/details/82658657
因为javamail不支持http代理,所以需要搭建socks代理
服务器配置
软件是danted,配置如下(允许匿名代理),其中internal和external根据需要修改
logoutput: /var/log/danted.log
internal: 0.0.0.0 port = 6666
#这里external为代理服务器上访问外网的网卡的IP.
external: 200.100.1.10
method: none
clientmethod: none
user.privileged: proxy
user.notprivileged: nobody
user.libwrap: nobody
client pass {
from: 0.0.0.0/0 to: 0.0.0.0/0
log: connect disconnect
}
pass {
from: 0.0.0.0/0 to: 0.0.0.0/0 port gt 1023
command: bind
log: connect disconnect
}
pass {
from: 0.0.0.0/0 to: 0.0.0.0/0
command: connect udpassociate
log: connect disconnect
}
block {
from: 0.0.0.0/0 to: 0.0.0.0/0
log: connect error
}
客户端代码
其中,MyAuthenticator 不确定是否必要
class MyAuthenticator extends Authenticator {
private String user = "";
private String password = "";
public MyAuthenticator(String user, String password) {
this.user = user;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password.toCharArray());
}
}
public class CAutoTest {
public static void main(String[] args) throws IOException{
Properties props = System.getProperties();
props.setProperty("proxySet", "true");
props.setProperty("socksProxyHost", "192.168.1.10");
props.setProperty("socksProxyPort", "6666");
props.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
// 设置邮件服务器主机名
props.setProperty("mail.host", "smtp.exmail.qq.com");
// 发送邮件协议名称
props.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.smtp.ssl.socketFactory", sf);
Authenticator.setDefault(new MyAuthenticator("", ""));
Session session = Session.getInstance(props);
Message message = new MimeMessage(session);
Address address = null;
try {
address = new InternetAddress(你的邮箱);
} catch (AddressException e) {
e.printStackTrace();
}
try {
message.setFrom(address);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.setSubject("测试");
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.setText("test2");
} catch (MessagingException e) {
e.printStackTrace();
}
try {
message.setSentDate(new Date());
} catch (MessagingException e) {
e.printStackTrace();
}
Transport transport = null;
try {
transport = session.getTransport();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
//邮件服务器、发送邮箱账号、第三方验证码
try {
transport.connect("smtp.exmail.qq.com", 你的邮箱, 你的邮箱密码);
} catch (MessagingException e) {
e.printStackTrace();
}
try {
transport.sendMessage(message, new Address[] { new InternetAddress(你的邮箱) });
} catch (MessagingException e) {
e.printStackTrace();
}
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("邮件发送!");
}
}