3

Need to send email from localhost to external accounts like gmail and yahoo. Right now i have a program which can send and recieve mails from my local domain by local email server up and running eg (admin@ib-status.com <-> devteam@ib-status.com). But problem is when i try to send from local domain to gmail or yahoo account i'm Unable to do it eg(admin@ib-status.com -> myaccount@gmail.com). Need help on this

PS. I'm Using Hmailserver for emailserver

public class JMailer {

          private static String HOSTNAME = "localhost";
            private static String USERNAME = "admin";
            private static String PASSWORD = "Mylocaldomainpassword";

            public static void main(String[] args) {
            try {  
                String to = "MygmailAccount@gmail.com";
                String from = "admin@ib-status.com";               
                Properties properties = System.getProperties();

                properties.setProperty("mail.smtp.host",HOSTNAME);
                Session session = Session.getInstance(properties, new Authenticator() {                
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(USERNAME, PASSWORD);
                    }
                });
                    MimeMessage message = new MimeMessage(session);
                    message.setFrom(new InternetAddress(from));
                    message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
                    message.setSubject("My Subject!");
                    message.setText("Here Goes My Message");                    
                    Transport.send(message);
                    System.out.println("Message Sending Completed");
                } catch (MessagingException mex) {
                    mex.printStackTrace();
                }
            }
}

and my error from Hmailserver log is below

"SMTPC" 4508 0 "2014-06-13 15:18:01.022" "TCP" "SMTPDeliverer - Message 13 - Connection failed: Host name: 74.125.25.27, message: No connection could be made because the target machine actively refused it"

did i miss anything here?why remote machine's connection is refused ? and i dont want to use gmail's SMTP server to send message.all i need is i want my own smtp sever running to send and recieve

Karthick Radhakrishnan
  • 1,151
  • 3
  • 12
  • 32

2 Answers2

2

Try this. Working perfectly! put your Gmail ID at sender@gmail.com, and your Gmail password at password.

import com.sun.mail.smtp.SMTPMessage;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendmailSSl {
public static void main(String[] args) {
    Properties props = new Properties();
    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", "805");

    Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("sender@gmail.com","Password");
        }
    });

    try {

        SMTPMessage message = new SMTPMessage(session);
        message.setFrom(new InternetAddress("sender@gmail.com"));
        message.setRecipients(Message.RecipientType.TO,
                                 InternetAddress.parse( "reciver@gmail.com" ));

        message.setSubject("Testing Subject");
        message.setText("This is Test mail");
        message.setContent("This Is my First Mail Through Java");
        message.setNotifyOptions(SMTPMessage.NOTIFY_SUCCESS);
        int returnOption = message.getReturnOption();
        System.out.println(returnOption);        
        Transport.send(message);
        System.out.println("sent");

    }
     catch (MessagingException e) {
        throw new RuntimeException(e);         
     }
  }
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
Pavan Patidar
  • 363
  • 1
  • 2
  • 10
  • 2
    The above doesn't do what Karthick wants. It also includes a number of the [common JavaMail mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). – Bill Shannon Jun 13 '14 at 19:30
  • yes thank you bill, I'm not looking for using google SMTP server, I'd like to host own local email server, right now I've heard about JBoss Mail service and i'm working on it,will keep you updated any solutions meanwhile is appreciated. – Karthick Radhakrishnan Jun 14 '14 at 07:01
1

Finally i'm able to crack this, I tested in 2 email servers which can get our job done,Apache james and hmailserver. Hmailserver is pretty easy run and configure because it has gui to do that.

HmailServer 5.4.2

 1. Configure as per the documentation 
 2. Do not use localhost and make sure you change it in C:\Windows\System32\drivers\etc\hosts from "127.0.0.1 localhost" -> "127.0.0.1 example.com"
 3. In add domain of hmailserver give "example.com"
 4. In Domain created add accounts eg.user@example.com
 5. under setting->protocold->smtp->delivery of email add "example.com"

Now run the below program we are good to go.

Apache James apache-james-3.0-beta4

Mostly same as above but this do not have any GUI to configure,this is light weight command line email server which also runs on Linux.

apply same procedure but it has specific command line to create domain and accounts, before that you need to log into admin account to create users.

the hurdle you will face using Apache james is it runs well with 32 bit, but it will have server starting issue for 64 bit because the "Wrapper.exe " it uses from tanuki . where thy dont support 64 bit version of wrapper.exe so i had to but trial license and add 64 bit wrapper.exe and modify james.bat.

Other than that it works fine.

package com.javabp.jmailer;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class JMailer {
    public static void main(String[] args) 
    {
        /***** CHANGE THESE FOUR VARIABLE VALUES TO REFLECT YOUR ENVIRONMENT ******/
        String user = "user";   // Newly created user on JAMES Server
        String password = "password"; // user password

        String fromAddress = "user@example.com"; // newlycreateduser@localhost
        String toAddress = "myaccount@gmail.com";


        // Create a mail session
        Properties properties = new Properties();
        properties.put("mail.transport.protocol", "smtp");
        properties.put("mail.smtp.host", "example.com");
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.username", user);
        properties.put("mail.smtp.password", password);
        Session session = Session.getDefaultInstance(properties, null);

        try 
        {
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(fromAddress));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));

            message.setSubject("Email From my Own Server");
            message.setText("Test Mail sent from My Apache James Server!!");
            Transport.send(message);

            System.out.println("Email sent successfully");
        }
        catch (MessagingException e) 
        {
            e.printStackTrace();
        }
    }
}

the above code works for both Hmailserver and Apache James.

Pointer for emails

   * if your sending to external accounts be sure you see you mail at spam folders unless you have registered domain and hosted.
   * once you send a mail to those server there is been chance your IP or domain will be blocked especially gmail. so it is better to have dynamic IP so you can restart your internet connection to send a mail again and also make sure you change your domain before sending even you changed your IP.
Karthick Radhakrishnan
  • 1,151
  • 3
  • 12
  • 32