Send mail through firewall
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* @author HappyQin
*/
public class MailService {
private static boolean ready = false;
private static Properties prop = null;
private static String smtpHost;
private static String smtpAuth;
private static String smtpUser;
private static String smtpPassword;
private static String mailAddress;
private MailService() {
init();
}
private static void init() {
prop = new Properties();
FileInputStream fis = null;
try {
URL url = MailService.class.getClassLoader().getResource(
"smtp.config");
if (url != null) {
fis = new FileInputStream(url.getFile());
//System.out.println(url.getFile());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fis != null) {
try {
prop.load(fis);
} catch (IOException e1) {
e1.printStackTrace();
}
}
mailAddress = prop.getProperty("SMTP_EMAIL");
smtpPassword = prop.getProperty("SMTP_PWD");
smtpAuth = prop.getProperty("SMTP_AUTH");
smtpHost = prop.getProperty("SMTP_HOST");
smtpUser = prop.getProperty("SMTP_USER");
ProxyConfig proxy = new ProxyConfig();
if ((proxy.getSocksProxyEnabled() != null)
&& ("true".equalsIgnoreCase(proxy.getSocksProxyEnabled()))) {
System.setProperty("proxyHost", proxy.getSocksProxyHost());
System.setProperty("proxyPort", proxy.getSocksProxyPort());
System.setProperty("proxyUserName", smtpUser);
System.setProperty("proxyPassword", smtpPassword);
System.setProperty("socksProxySet", "true");
System.setProperty("socksProxyHost", proxy.getSocksProxyHost());
System.setProperty("socksProxyPort", proxy.getSocksProxyPort());
System.setProperty("socksProxyUserName", smtpUser);
System.setProperty("socksProxyPassword", smtpPassword);
System.setProperty("socks.useProxy", "true");
System.setProperty("socks.proxyHost", proxy.getSocksProxyHost());
System.setProperty("socks.proxyPort", proxy.getSocksProxyPort());
System.setProperty("socks.proxyUserName", smtpUser);
System.setProperty("socks.proxyPassword", smtpPassword);
if (proxy.getHttpNonProxyHosts() != null)
System.setProperty("socksNonProxyHosts", proxy
.getSocksNonProxyHosts());
}
ready = true;
}
public static void sendMail(String to, String subject, String content) {
if (!ready) {
init();
}
Properties props = new Properties();
props.put("mail.smtp.host", smtpHost);
props.put("mail.smtp.port", "" + 25);
props.put("mail.smtp.auth", smtpAuth);
props.put("mail.smtp.user", smtpUser);
//props.put("mail.store.protocol", "pop3");
//props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,
new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(smtpUser,
smtpPassword);
}
});
MimeMessage message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress(mailAddress));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(
to));
message.setSubject(subject);
message.setContent(content, "text/plain;charset=gb2312");
Transport.send(message);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
MailService.sendMail("[email protected]", "Subject Test", "Mail Content");
}
}
----------------------------------------------------------
Where is the ProxyConfig.java?
ProxyConfig.java
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
/**
* @author HappyQin
*/
public class ProxyConfig {
private String httpProxyEnabled;
private String httpProxyHost;
private String httpProxyPort;
private String httpNonProxyHosts;
private String httpProxyAuthNeeded;
private String httpProxyUser;
private String httpProxyPwsd;
private String socksProxyEnabled;
private String socksProxyHost;
private String socksProxyPort;
private String socksNonProxyHosts;
private String socksProxyAuthNeeded;
private String socksProxyUser;
private String socksProxyPwsd;
Properties prop = new Properties();
/**
* @return Returns the httpNonProxyHosts.
*/
public String getHttpNonProxyHosts() {
return prop.getProperty("http.nonProxyHosts");
}
/**
* @return Returns the httpProxyAuthNeeded.
*/
public String getHttpProxyAuthNeeded() {
return prop.getProperty("http.proxyAuthNeeded");
}
/**
* @return Returns the httpProxyEnabled.
*/
public String getHttpProxyEnabled() {
return prop.getProperty("http.proxyEnabled");
}
/**
* @return Returns the httpProxyHost.
*/
public String getHttpProxyHost() {
return prop.getProperty("http.proxyHost");
}
/**
* @return Returns the httpProxyPort.
*/
public String getHttpProxyPort() {
return prop.getProperty("http.proxyPort");
}
/**
* @return Returns the httpProxyPwsd.
*/
public String getHttpProxyPwsd() {
return prop.getProperty("http.proxyPwsd");
}
/**
* @return Returns the httpProxyUser.
*/
public String getHttpProxyUser() {
return prop.getProperty("http.proxyUser");
}
/**
* @return Returns the socksNonProxyHosts.
*/
public String getSocksNonProxyHosts() {
return prop.getProperty("socks.nonProxyHosts");
}
/**
* @return Returns the socksProxyAuthNeeded.
*/
public String getSocksProxyAuthNeeded() {
return prop.getProperty("socks.proxyAuthNeeded");
}
/**
* @return Returns the socksProxyEnabled.
*/
public String getSocksProxyEnabled() {
return prop.getProperty("socks.proxyEnabled");
}
/**
* @return Returns the socksProxyHost.
*/
public String getSocksProxyHost() {
return prop.getProperty("socks.proxyHost");
}
/**
* @return Returns the socksProxyPort.
*/
public String getSocksProxyPort() {
return prop.getProperty("socks.proxyPort");
}
/**
* @return Returns the socksProxyPwsd.
*/
public String getSocksProxyPwsd() {
return prop.getProperty("socks.proxyPwsd");
}
/**
* @return Returns the socksProxyUser.
*/
public String getSocksProxyUser() {
return prop.getProperty("socks.proxyUser");
}
/**
*
*/
public ProxyConfig() {
init();
}
private void init() {
prop = new Properties();
FileInputStream fis = null;
try {
URL url = getClass().getClassLoader().getResource(
"proxy.config");
if (url != null) {
fis = new FileInputStream(url.getFile());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fis != null) {
try {
prop.load(fis);
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}