3

I would like to send email without authentication using java. Can someone help me?

With authentication, I do it as follows:

    public void sendEmail() throws EmailException{
    SimpleEmail email = new SimpleEmail();
    email.setHostName("smtp.gmail.com");
    email.setSmtpPort(465);
    email.addTo("[email protected]", "XXXX");
    email.setFrom("[email protected]","XXXXX");
    email.setSubject("testando . . .");
    email.setMsg("testando 1");
    email.setSSL(true);
    email.setAuthentication("[email protected]", "XXXXX");
    email.send();
}

I forgot to say that i do not have a provider. i need an provider finally, i have emailFrom Subject and Message, and need send this email how?

1
  • 5
    This has nothing to do with Java. You can send email without authenticating only if the mail server allows it, in which case you would omit the setAuthentication() call. If the SMTP server requires authentication you must provide it. If you are testing you can use a mock SMTP server, or set up your own real SMTP server and configure it without authentication. Commented Apr 3, 2014 at 15:29

3 Answers 3

6

If it is only for testing purposes, you may try Papercut. While it’s running, Papercut automatically picks up e-mail sent to the standard SMTP port (25) on any IP address. You just send mail from your application and switch to Papercut to review it.

Papercut @ github: https://github.com/ChangemakerStudios/Papercut/releases

import java.util.Date;
import java.util.Properties;

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

public class SendMail {

    public static void main(String[] args) throws Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", "127.0.0.1");
        props.put("mail.smtp.port", "25");
        props.put("mail.debug", "true");
        Session session = Session.getDefaultInstance(props);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
        message.setSubject("Notification");
        message.setText("Successful!", "UTF-8"); // as "text/plain"
        message.setSentDate(new Date());
        Transport.send(message);
    }

}
Sign up to request clarification or add additional context in comments.

2 Comments

If I want to use sendgrid apikey ,how I should send it ?
The Github link is still working, but the website link to Papercut is down.
3
import java.util.*;
import javax.mail.*;

public class SimpleEmail {

    public static void main(String[] args) {
        System.out.println("SimpleEmail Start");
        String smtpHostServer = "smtp.gmail.com";
        String toEmail = "[email protected]";
        Properties props = System.getProperties();
        props.put("mail.smtp.host", smtpHostServer);
        props.put("mail.smtp.starttls.enable", true);
        props.put("mail.smtp.port", "25");
        Session session = Session.getDefaultInstance(props);
        try {
            MimeMessage msg = new MimeMessage(session);
            msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
            msg.addHeader("format", "flowed");
            msg.addHeader("Content-Transfer-Encoding", "8bit");
            msg.setFrom(new InternetAddress("[email protected]", "NoReply-JD"));
            msg.setReplyTo(InternetAddress.parse("[email protected]", false));
            msg.setSubject("SimpleEmail Testing Subject", "UTF-8");
            msg.setText("SimpleEmail Testing Body", "UTF-8");
            msg.setSentDate(new Date());
            msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toEmail, false));
            System.out.println("Message is ready");
            Transport.send(msg);
            System.out.println("EMail Sent Successfully!!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

1 Comment

Facing An Issue com.sun.mail.smtp.SMTPSendFailedException: 530-5.7.0 Authentication Required. Learn more at 530 5.7.0 support.google.com/mail/?p=WantAuthError o6sm1435614pgg.37 - gsmtp at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2249) at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1740) at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1239) at javax.mail.Transport.send0(Transport.java:255) at javax.mail.Transport.send(Transport.java:124) at com.necti.smsemail.SimpleEmail.main(SimpleEmail.java:59)
0

Try this example:

// File Name SendEmail.java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail
{
   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("mail.smtp.host", 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();
      }
   }
}

Via: tutorialspoint

2 Comments

I forgot to say that i do not have a provider. i need an provider finally, i have emailFrom Subject and Message, and need send this email how?
You saw the link at the end of the answer? I do not quite understand your doubts here in comment?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.