Java, send mail using SMTP always mail goes to spam folder, Why? - java

i'm say apology to ask this repeated Question..
i'm using gmail smtp server to send mails..
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.starttls.enable", "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");
But always my mail sending spam folder only....
Note: i'm using gmail as company name myname#mtmr.in
if it is normal gmail like myname#gmail.com,, it is working fine.. but i want through company mail..

It is difficult to say for sure why your email is being marked as SPAM, but one possibility1 is that your company has gotten itself a reputation for spamming. (Or more precisely, lots of gmail users have hit the "report spam" button on emails that they received from your company.)
If that is the problem, then there is not much you can do about that except create a filter on your gmail account to move those emails out of your spam. Of course, that only affects emails going to your account ... not anyone else's.
The long term remedy is to convince the people who are sending spam / spammy messages using your company's email address to STOP IT!
1 - The fact that the same email sent using your personal email address is NOT marked as SPAM suggests that this is more than just "a possibility" ...

Related

Javamail ignore socks proxy when ssl is enabled

I've setup javamail to run through a socks proxy with ssl, but it won't go through it. I tried setting both mail.smtp.socks.host and mail.smtp.socks.port but it keeps going direct to the mail host. Here's my properties:
smtpProps = new Properties();
smtpProps.setProperty("mail.smtp.host", sendHost);
smtpProps.setProperty("mail.smtp.port", String.valueOf(sendPort));
smtpProps.setProperty("mail.transport.protocol", "smtps");
smtpProps.setProperty("mail.smtp.ssl.enable", "true");
smtpProps.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
smtpProps.setProperty("mail.smtp.socketFactory.fallback", "true");
smtpProps.setProperty("mail.smtp.auth", "true");
smtpProps.setProperty("mail.smtp.connectiontimeout", String.valueOf(sendConnTimeout) );
smtpProps.setProperty("mail.smtp.writetimeout", String.valueOf(sendTimeout) );
smtpProps.setProperty("mail.smtp.socks.host", proxyHost);
smtpProps.setProperty("mail.smtp.socks.port", String.valueOf(proxyPort));
smtpProps.setProperty("mail.debug", "true");
smtpProps.setProperty("mail.socket.debug", "true");
I've tried all smtps property name combination without any success. I'm using Java 7 and javamail 1.4.7.
The only one workaround I found for now is to use a custom version of SSL socket factory
smtpProps.setProperty("mail.smtp.socketFactory.class",
proxied?
"my.socks.ProxiedSSLSocketFactory" :
"javax.net.ssl.SSLSocketFactory");
`
obviously I can't use system properties socksProxyHost & socksProxyPort.
Any ideas?

Java Mail API fails intermittently

I have created automated email report using java mail api which needs to be triggered everyday after a batch file run.Although it works fine most of the times,at times it gives exception javax.mail.MessagingException: Could not connect to SMTP host: my host name, port: 25; nested exception is: java.net.ConnectException: Connection refused.This is not due to authentication issue as i use the same credentials whenever i sent the email.
I am not sure why java mail api fails intermittently.Can i get some suggestion to debug the issue?
I am using the below code snippet -
String to = "xyz#gmail.com";//change accordingly
String from = "abc#mydomain.com";//change accordingly
final String username = "abc";//change accordingly
final String password = "*****";//change accordingly
String host = "My SMTP server";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "25");
// Get the Session object.
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject("Testing Subject");
message.setText("message to stakeholders");
Transport.send(message);
}
It's a good idea to check logs on the mail server to see possible cause of the refusal - especially since you know WHEN failed attempts were made at.
If JavaMail can't connect "sometimes" with "connection refused", the possibilities are:
The server is actually down.
The server is up, but too busy to accept new connections.
The server is up, but is rejecting connections for some policy reason, e.g., you've connected too frequently.
Some firewall or anti-virus program has rejected the connection.
Check the server configuration and log files. If there's nothing on the server indicating that it's rejecting connections, check the client.

Determine Mail Settings from Domain

I'm currently writing a simple Java application that will allow the user to send and receive email. When the application starts, it prompts the user to log in in the following format:
username#provider.com
passwd
Currently I have the field split so that provider.com is placed in its own string. Is there any way I can use the java mail api or something else to retrieve the settings for "provider.com?" I'm looking to get back strings such as "smtp.gmail.com" when the user has a gmail account and so-forth. Any help would be appreciated!
EDIT: To clarify what I am after...
final Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.gmail.com"); // I want to get this (smtp.gmail.com)
properties.put("mail.smtp.port", "465"); // And this (465)
// from the user entered "gmail.com" or "live.com"
I just found this after some more searching: Finding SMTP host and port knowing the e-mail address using JAVA API
It seems there is no predictable way to do this. Very disappointing.

setup email connection in java

I am trying to make connection to the email server using property files and session store value. and all the parameters are given dynamically.
this is what i'm trying
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", emailHost);
props.put("mail.smtp.port", "2525");
Session sessions = Session.getDefaultInstance(props);
Store store = sessions.getStore(emailAccType);
store.connect(emailHost, emailId, emailPwd);
Even if i am giving the correct email and password, the connection is not setting up.
please do the needful help.
Thanks in advance.
Couple of ideas:
make sure the email server does not require pop3 login first.
make sure port is open in firewall
Do you get any exceptions during your tries?
Does it throw an exception or it just doesn't work?
I am using gmail for sending emails and I used something like this:
Session.getInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(email, password);
}
});

Must issue a STARTTLS command first when trying to send a SimpleMailMessage in Grails

I try to send a message in a grails application. I use Java code with this problem tho, here is my code
SimpleMailMessage message_ref = new SimpleMailMessage();
JavaMailSenderImpl sender_ref = new JavaMailSenderImpl();
sender_ref.setHost("smtp.gmail.com")
sender_ref.setUsername("testAccount#googlemail.com")
sender_ref.setPassword("topsecret")
message_ref.setTo("testRecipient#gmx.de")
message_ref.setSubject("Hello there")
message_ref.setText("How are you")
sender_ref.send(message_ref)
I'm getting the following exception:
SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first
I found a similar problem here on stackoverflow here
Must issue a STARTTLS command first. Sending email with Java and Google Apps
but it didn't help me cause he used an different approach.
Can somebody tell me what's wrong? I'm expecting the error is not in the code but in some configuration file and this is where my knowledge edges.
Quoting from the Grails mail plugin docs:
grails {
mail {
host = "smtp.gmail.com"
port = 465
username = "youracount#gmail.com"
password = "yourpassword"
props = ["mail.smtp.auth":"true",
"mail.smtp.socketFactory.port":"465",
"mail.smtp.socketFactory.class":"javax.net.ssl.SSLSocketFactory",
"mail.smtp.socketFactory.fallback":"false"]
} }
I can't help you much, but your problem is basically that you need to use SSL to communicate with the server. Google does not allow plain-text communication for a lot of good reasons. I don't know much about grails, but I assume it has some sort of ssl-support. If it does not, you're probably better off doing it in javax.mail.
StartTLS is just a text-command you send to the smtp-server to explicitly start secure communications.
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mail.smtp.port", smtpPort);
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);

Categories

Resources