Java mail how use proxy? - java

Need to send message (smtp) using a proxy, code below, the proxy is completely ignored, what to do?
Properties props = new Properties();
System.setProperty("mail.socket.debug", "true");
props.put("mail.socket.debug", "true");
if (_config.getIsProxy()) {
props.put("mail.smtp.socks.host", proxyHost);
props.put("mail.smtp.socks.port", proxyPost);
}
props.put("mail.smtp.timeout", _config.getTimeout()*1000);
props.put("mail.smtp.connectiontimeout", _config.getTimeout()*1000);
props.put("mail.smtp.ssl.enable", "true");
props.put("mail.store.protocol", "smtp");
props.put("mail.smtp.host", server);
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");

Related

AuthenticationFailedException in sending email from java code

I am trying to write a small java program that can send email from my corporate outlook account. I am planning to run this java program on my office machine only. Below is my program:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp-mail.outlook.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
#Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(my_email_id, my_password);
}
});
MimeMessage mime = new MimeMessage(session);
try {
mime.setSender(new InternetAddress(emailid));
mime.setRecipient(Message.RecipientType.TO, new InternetAddress(emailid));
mime.setSubject("Test");
mime.setText("Testing");
Transport.send(mime);
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
I am getting below error :
javax.mail.AuthenticationFailedException: 535 5.7.3 Authentication unsuccessful [BM1PR01CA0166.INDPRD01.PROD.OUTLOOK.COM]
I have tried removing the socketFactory properties as well, but no luck. Can anyone help me out with this one? I am guessing it might have something to do with proxy or any other security on my office machine, but I am not able to figure out what it is.

Java, unable to send mail on linux

Using my home computer I am able to send an email through Gmail without error, however, when I try to run the same code on my Linux box It throws:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2182)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:726)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at javax.mail.Service.connect(Service.java:195)
at javax.mail.Transport.send0(Transport.java:254)
at javax.mail.Transport.send(Transport.java:124)
at io.paratek.updates.util.Mail.sendMail(Mail.java:34)
at io.paratek.updates.MailTest.main(MailTest.java:8)
My code
public class Mail {
public static void sendMail(String subject, String contents, String to) {
Properties props = System.getProperties();
props.put("mail.smtp.host", "smtp.gmail.com");
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 = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("myemail", "mypasswd");
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(to));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(subject);
message.setText(contents);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
I am using Oracle JDK 181 on both systems.
I am able to successfully connect "telnet smtp.gmail.com 465" on the Linux box
After switching to TLS it works
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");

Javax.mail package didn't work in release mode apk

I'm using javax.mail package for mail sending,
while application in debug mode it's work fine and send every mail to my account, but when i create release application mail stop sending form application.
code is as given below:
Properties props = new Properties();
props.put("mail.smtp.user", "abc#xyz.com");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.debug", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
//Creating a new session
session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
//Authenticating the password
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("abc#xyz.com", "password#123");
}
});
try {
//Creating MimeMessage object
MimeMessage mm = new MimeMessage(session);
//Setting sender address
mm.setFrom(new InternetAddress("abc#xyz.com"));
//Adding receiver
mm.addRecipient(Message.RecipientType.TO, new InternetAddress("abc#xyz.com"));
//Adding subject
mm.setSubject(subject);
//Adding message
mm.setText(message);
//Sending email
Transport.send(mm);
} catch (MessagingException e) {
e.printStackTrace();
}
Generally this problem happen because of proguard, If you add proguard into your project it'll stop some of functionality like this.
I did it by adding package javax in proguard-rules.pro file.
-keep class javax.** {*;}
this will add all javax methods's permission in proguard.

unalbe to connect smtp.gmail.com at 465 in a loop

I am sending mail through gmail coount using port 465.
I am sending mail in loop.(reciients are present in a list)
I am able to send first mail succesfully , but as soon as second mail shoots
I get error unalbe to connect smtp.gmail.com at 465.
enter code hereprops.put("mail.smtp.host", smtp_host);
props.put("mail.smtp.port", smtp_port);
props.put("mail.smtp.user", smtp_user);
props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);
props.put("mail.smtp.socketFactory.port", smtp_port);
props.put("mail.smtp.socketFactory.class", socket_factory));
props.put("mail.smtp.socketFactory.fallback", "false");
try {
//Obtain the default mail session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setRecipient(Message.RecipientType.CC, new InternetAddress(cc));
message.setSubject(subject);
message.setContent(messageText, "text/html");
message.setHeader("Content-Type" , "text/html" );
message.saveChanges();
//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
e.printStackTrace();
}
Use Port 587 for TLS/STARTTLS. And following properties only if you use TLS/STARTTLS
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
And if you are trying to connect through SSL. Try only this -
props.put("mail.smtp.host", "smtp.gmail.com");
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 = Session.getDefaultInstance(props, null);
Change this with -
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username#gmail.com", "password");
}
});

Using javamail to send from hotmail?

I've got gmail and yahoo working, but not hotmail. Here's what I have, what am I doing wrong?
private String mailhost = "smtp.live.com";
public hotmailSenderActivity(String user, String password) {
this.user = user;
this.password = password;
//This connects to the actual mailserver
Security.addProvider(new com.provider.JSSEProvider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", mailhost);
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("smtp.starttls.enable", "true");
props.put("mail.smtp.socketFactory.fallback", "false");
props.setProperty("mail.smtp.quitwait", "false");
session = Session.getDefaultInstance(props, this);
I have tried port 25 + 587 without the SSL stuff. I have tried port 465 WITH the SSL stuff. The email and password are correct (Ive hard coded them to be sure).
I don't receive any errors... So whats the problem?
1) use debug output:
session.setDebug(true);
2) hotmail smtp server starts non-ssl connection on port 25 or 587, and uses starttls after initial connection; thus remove lines
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
3) mimimum amount of settings is then:
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.live.com");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.auth", "true");
this assumes port is 25, otherwise add props.put("mail.smtp.port", "587");
4) yet even nicer looks this:
...
props.put("mail.smtp.starttls.enable", "true");
Session session = Session.getDefaultInstance(props);
Transport trans = session.getTransport("smtp");
trans.connect("smtp.live.com", 25, "user", "pass");
now you're connected, use methods of Transport
http://www.oracle.com/technetwork/java/javamail/faq/index.html#hotmail
Get rid of all that socket factory stuff, you don't need it.

Categories