Javamail - sending mail from localhost to external accounts

Karthick Radhakrishnan picture Karthick Radhakrishnan · Jun 13, 2014 · Viewed 29.4k times · Source

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 ([email protected] <-> [email protected]). But problem is when i try to send from local domain to gmail or yahoo account i'm Unable to do it eg([email protected] -> [email protected]). 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 = "[email protected]";
                String from = "[email protected]";               
                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

Answer

Pavan Patidar picture Pavan Patidar · Jun 13, 2014

Try this. Working perfectly! put your Gmail ID at [email protected], 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("[email protected]","Password");
        }
    });

    try {

        SMTPMessage message = new SMTPMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                                 InternetAddress.parse( "[email protected]" ));

        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);         
     }
  }
}