Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How i send email without authentication using java

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?

like image 212
felipe muner Avatar asked Sep 16 '26 15:09

felipe muner


2 Answers

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

}
like image 195
Paul Vargas Avatar answered Sep 19 '26 06:09

Paul Vargas


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();
        }
    }
}
like image 36
sarbjeet singh Avatar answered Sep 19 '26 05:09

sarbjeet singh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!