1
votes

I want to send a mail using Java but it doesn't work using this tutorial thai I found here http://www.tutorialspoint.com/java/java_sending_email.htm .

Here is my main method

public static void main(String[] args) {
        // Recipient's email ID needs to be mentioned.
      String to = "[email protected]";

      // Sender's email ID needs to be mentioned
      String from = "[email protected]";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("smtp.laposte.net", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
    }

And I have the error below:

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25; nested exception is: java.net.ConnectException: Connection refused: connect at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) at javax.mail.Service.connect(Service.java:295) at javax.mail.Service.connect(Service.java:176) at javax.mail.Service.connect(Service.java:125) at javax.mail.Transport.send0(Transport.java:194) at javax.mail.Transport.send(Transport.java:124) at javamail.JavaMail.main(JavaMail.java:58) Caused by: java.net.ConnectException: Connection refused: connect at java.net.DualStackPlainSocketImpl.connect0(Native Method) at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.net.Socket.connect(Socket.java:589) at java.net.Socket.connect(Socket.java:538) at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319) at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ... 7 more BUILD SUCCESSFUL (total time: 1 second)

3
then find another example on the net that works?Faraz
Try what Tipu Sultan suggested below. If that doesn't work then find another example on the net that works :PFaraz
also, you do know that in your program you have to put correct gmail userID and correct password?Faraz

3 Answers

1
votes

error is in host use

host="smtp.gmail.com";

if you are using gmail id for sending mail. then you need to turn on gmail sending mail from extrnel application for users. follow below link https://support.google.com/a/answer/57919?hl=en turn on and then try again.

1
votes

you can also use spring for sending mail.

bean.xml

<bean id="myMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="smtp.gmail.com" />
        <property name="port" value="587" />
        <property name="username" value="<user_name>" />
        <property name="password" value="<password>" />

        <!-- The name of the property, following JavaBean naming conventions -->
        <property name="javaMailProperties">
            <props>
                <prop key="mail.transport.protocol">smtp</prop>
                <prop key="mail.smtp.auth">true</prop>
                <prop key="mail.smtp.starttls.enable">true</prop>
                <prop key="mail.debug">true</prop>
            </props>
        </property>
    </bean>

.

@Service("myMail")
    public class myMailApi {

        @Autowired
        private MailSender myMailSender;

        public void sendMail(String to,String from,String subject, String body){
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setTo(to);
            simpleMailMessage.setFrom(from);
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(body);
            myMailSender.send(simpleMailMessage);
        }
    }



private static void sendMailClient() {
        String mailXml = "mail-bean.xml";
        ApplicationContext context = new ClassPathXmlApplicationContext(mailXml);
        myMailApi mailBean = (mmyMailApi) context
                .getBean("myMail");
        mailBean.sendMail("<to>",
                "<from>", "<subject>", "<body>");
    }
0
votes

With which server I can send email without have an account and login in java mail api ? Is hotmail autorizing sending emails without logging in in java mail ?