here is the solution that you can send mail through a proxy server
Properties p = System.getProperties();
p.setProperty("proxySet","true");
p.setProperty("socksProxyHost","192.168.155.1");
p.setProperty("socksProxyPort","1080");
Send Mail through a proxy server (Gmail)
import java.security.Security;
import java.util.Date;
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;
publicclass GmailSender {
publicstaticvoid main(String[] args) throws AddressException, MessagingException {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("proxySet","true");
props.setProperty("socksProxyHost","192.168.155.1");
props.setProperty("socksProxyPort","1080");
props.setProperty("mail.smtp.host", "smtp.gmail.com");
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.port", "465");
props.setProperty("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
final String username = "USERNAME";
final String password = "PASSWORD";
Session session = Session.getDefaultInstance(props,
new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(username, password);
}});
// -- Create a new message --
Message msg = new MimeMessage(session);
// -- Set the FROM and TO fields --
msg.setFrom(new InternetAddress("xxxxxx@gmail.com"));
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("xxxxxx@yahoo.com",false));
msg.setSubject("Hello");
msg.setText("How are you");
msg.setSentDate(new Date());
Transport.send(msg);
System.out.println("Message sent.");
}
}
Fetch Mail through a proxy server (Gmail)
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
publicclass GmailFetch {
publicstaticvoid main(String argv[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
// Get a Properties object
Properties props = System.getProperties();
props.setProperty("proxySet","true");
props.setProperty("socksProxyHost","192.168.155.1");
props.setProperty("socksProxyPort","1080");
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
Session session = Session.getDefaultInstance(props,null);
URLName urln = new URLName("pop3","pop.gmail.com",995,null,
"USERNAME", "PASSWORD");
Store store = session.getStore(urln);
Folder inbox = null;
try {
store.connect();
inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_ONLY);
FetchProfile profile = new FetchProfile();
profile.add(FetchProfile.Item.ENVELOPE);
Message[] messages = inbox.getMessages();
inbox.fetch(messages, profile);
System.out.println("Inbox Number of Message" + messages.length);
for (int i = 0; i < messages.length; i++) {
String from = decodeText(messages[i].getFrom()[0].toString());
InternetAddress ia = new InternetAddress(from);System.out.println("FROM:" + ia.getPersonal()+'('+ia.getAddress()+')');
System.out.println("TITLE:" + messages[i].getSubject());
System.out.println("DATE:" + messages[i].getSentDate());
}
}
finally {
try {
inbox.close(false);
}
catch (Exception e) {
}
try {
store.close();
}
catch (Exception e) {
}
}
}
protectedstatic String decodeText(String text)
throws UnsupportedEncodingException {
if (text == null)
returnnull;
if (text.startsWith("=?GB") || text.startsWith("=?gb"))
text = MimeUtility.decodeText(text);
else
text = new String(text.getBytes("ISO8859_1"));
return text;
}
}
Without SSL connection, Normal SMTP
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
publicclass AttachExample {
publicstaticvoid main (String args[]) throws Exception
{
System.getProperties().put("proxySet","true");
System.getProperties().put("socksProxyPort","1080");
System.getProperties().put("socksProxyHost","192.168.155.1");
Properties props = System.getProperties();
String from = "xxxxx@spymac.com";
String to = "xxxx@yahoo.com";
String filename = "AttachExample.java";
// Get system properties
final String username = "USERNAME";
final String password = "PASSWORD";
props.put("mail.user", username);
props.put("mail.host", "mail.spymac.com");
props.put("mail.debug", "true");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(props,
new Authenticator(){
protected PasswordAuthentication getPasswordAuthentication() {
returnnew PasswordAuthentication(username, password);
}});
// Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("Hello JavaMail Attachment");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Here's the file");
// Create a Multipart
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageBodyPart);
// // Part two is attachment // // Create second body part
messageBodyPart = new MimeBodyPart();
// Get the attachment
DataSource source = new FileDataSource(filename);
// Set the data handler to the attachment
messageBodyPart.setDataHandler(new DataHandler(source));
// Set the filename
messageBodyPart.setFileName(filename);
// Add part two
multipart.addBodyPart(messageBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(message);
}
}
when running GmailFetch,I got the message(my jdk version is 1.5.0):
Exception in thread "main" javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.SocketException: Malformed reply from SOCKS server
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:120)
at javax.mail.Service.connect(Service.java:233)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at cn.edu.scu.forcast.test.GmailFetcher.main(GmailFetcher.java:37)
Re: JavaMail send a mail through a Proxy Server..(answer) and with Gmail(S
Nov 29, 2005 2:33 PM
(reply 4
of 19) (In reply to
#3 )
hello, i tried the gmailsender piece of code. however i have an error
on this line:
getPasswordAuthentication() {
return new PasswordAuthentication(xxxxx@gmail.com, xxxxx);
}});
for the username bit- xxxx@gmail.com, i get an error:
"syntax error on token "@",.expected"
can someone tell me how to fix this?
Re: JavaMail send a mail through a Proxy Server..(answer) and with Gmail(S
Mar 26, 2006 9:29 PM
(reply 6
of 19) (In reply to
#5 )
JavaMail uses mail protocols - SMTP, IMAP, POP.
These protocols are peers of HTTP. They don't layer
on top of HTTP. Thus, an HTTP proxy is of no use
with mail protocols.
SOCKS, on the other hand, works at a lower level than
all of these protocols.
However, when I run it on a proxy client, I get the following:
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: java.lang.NullPointerException
Exception in thread "main" javax.mail.MessagingException: Exception reading response
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1427)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1225)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:340)
at javax.mail.Service.connect(Service.java:297)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.apna.beans.ForumMail.main(ForumMail.java:56)
Caused by: javax.net.ssl.SSLException: java.lang.NullPointerException
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:166)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1476)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1443)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1426)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:86)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:97)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:235)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:75)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1405)
... 8 more
Caused by: java.lang.NullPointerException
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.getHost(SSLSocketImpl.java:1656)
at com.sun.net.ssl.internal.ssl.Handshaker.getHostSE(Handshaker.java:190)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.getKickstartMessage(ClientHandshaker.java:722)
at com.sun.net.ssl.internal.ssl.Handshaker.kickstart(Handshaker.java:522)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1101)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1024)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:675)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
... 13 more
Java Result: 1
Any ideas how I can run the program on my proxy client?
Re: JavaMail send a mail through a Proxy Server..(answer) and with Gmail(SSL)
Oct 24, 2007 7:03 AM
(reply 10
of 19) (In reply to
original post )
Hi kanweng,
I am getting the following error when i was trying to run GmailFetch.java class.
Exception in thread "main" javax.mail.MessagingException: Connect failed;
nested exception is:
java.net.SocketException: Unconnected sockets not implemented
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:148)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at com.ibm.podsmart.popmail.GmailFetch.main(GmailFetch.java:36)
Caused by: java.net.SocketException: Unconnected sockets not implemented
at javax.net.SocketFactory.createSocket(SocketFactory.java:2)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:222)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:163)
at com.sun.mail.pop3.Protocol.<init>(Protocol.java:81)
at com.sun.mail.pop3.POP3Store.getPort(POP3Store.java:201)
at com.sun.mail.pop3.POP3Store.protocolConnect(POP3Store.java:144)
... 4 more
please anyone can help me. How can I override SSLSocketFactory class.
When I use a Gmail account (SSL connection) on a proxy client, I get the following:
DEBUG SMTP: exception reading response: javax.net.ssl.SSLException: java.lang.NullPointerException
javax.mail.MessagingException: Exception reading response;
nested exception is:
javax.net.ssl.SSLException: java.lang.NullPointerException
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at packCommunications.EmailAccount.sendEmail(EmailAccount.java:281)
at packGUI.PanelConfigConexion$9.run(PanelConfigConexion.java:633)
at java.lang.Thread.run(Thread.java:619)
Caused by: javax.net.ssl.SSLException: java.lang.NullPointerException
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Alerts.java:190)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1520)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1487)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1470)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.handleException(SSLSocketImpl.java:1396)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:86)
at com.sun.mail.util.TraceInputStream.read(TraceInputStream.java:110)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read(BufferedInputStream.java:237)
at com.sun.mail.util.LineInputStream.readLine(LineInputStream.java:88)
at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1589)
... 8 more
Caused by: java.lang.NullPointerException
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.getHost(SSLSocketImpl.java:1700)
at com.sun.net.ssl.internal.ssl.Handshaker.getHostSE(Handshaker.java:198)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.getKickstartMessage(ClientHandshaker.java:833)
at com.sun.net.ssl.internal.ssl.Handshaker.kickstart(Handshaker.java:538)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1119)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1028)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:677)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
... 13 more
However, when I use any other account without SSL transport, everything work fine. Any idea about this problem?
Re: JavaMail send a mail through a Proxy Server..(answer) and with Gmail(S
Dec 19, 2007 11:23 AM
(reply 14
of 19) (In reply to
#13 )
The root exception is clearly coming from the
Java SE SSL code. You might want to try
asking for help in one of the Java SE forums.
You might also want to try some of the
debugging techniques described in SSLNOTES.txt.
This topic has
19
replies
on
2
pages.
1
|
2
|
Next »