13

JavaMail sends e-mails using an e-mail server, typically through the SMTP interface of an e-mail provider. Is it necessary to use an e-mail server to send e-mails?

I have a web site that will send e-mails and if possible I'd like to send the e-mails directly from the web site code without using an e-mail server. The JavaMail FAQ says that an e-mail server is required.

Is there a way to send e-mails without using an e-mail server, either with JavaMail or another API?

4
  • No, it is not possible. How are email clients supposed to get the email you are sending them?
    – Oded
    Commented Feb 17, 2014 at 20:52
  • goodreads.com/quotes/…
    – JensG
    Commented Feb 17, 2014 at 21:34
  • An alternative, which won't necessarily work with JavaMail, is to set up an MTA such as Postfix. You won't need a remote mail server, then, because you have a local one.
    – user16764
    Commented Feb 25, 2014 at 18:44
  • Precisely. I suppose you could say that the old Microsoft Mail MTA was "serverless" in the sense that the Microsoft Mail clients all individually dealt with getting and putting messages to/from the central mail store on the file system (on a file server, by the way). But even then the only way to get mail off that system onto someone else's was via a gateway, which always involves servers. Are you sure you've asked the question you intended? Commented Apr 24, 2014 at 13:24

3 Answers 3

11

You can deliver an email directly to one of the SMTP servers mentioned in the MX record of the address's domain. Usually that is a bad idea,though:

Delivering the mail to a local server is fast - you can almost immediately continue and do something else and let the server handle the delivery.

The actual delivery can take some time, for instance the remote mail server might be slow. Or it might reject the mail first - it is a known strategy against spam to tell a sender to try sending a mail a second time, which some spam bots won't do.

Also the mail server can easily try again a day later or so when the remote server is down, this reduces risk of lost mail.

4
  • Thanks for the information regarding spam prevention. I can see how changing these strategies makes use of an e-mail server a good idea. Commented Feb 17, 2014 at 21:32
  • 2
    technically, doing that turns your application into a mailserver, so you're still using a mailserver, albeit a rather bare bones one.
    – jwenting
    Commented Feb 18, 2014 at 8:08
  • @jwenting how would it be a mail server if only acting as a client that connects to other servers? I rather thought to be a "server" you had to have at least one "client" connect to you... Commented Feb 26, 2014 at 0:08
  • Also, sending to remote mail servers directly from your app is likely to run afoul of SPF/DMARC anti-spam checks. Commented Apr 24, 2014 at 13:20
1

It all depends on what you mean by "use" exactly.

If "use" includes only the direct use of an email server, then you might pass the mail to send through some other kind of service indirectly to an email server. For practical use cases, this seems to be a "Good Enough™" solution to me.

If, however, your question means literally "without involving any email server at all", this would be only possible, if your software is able to put the mail into the inbox of the target client by some way of your choice, but except POP3 or IMAP or any other email-relevant protocol used in email context (because, if you would use such an protocol, that particular program would act as the mail server, which is not allowed)

To answer the question: Yes. At least in theory.

4
  • I'm asking whether I have to use an e-mail server to send the e-mail to the recipients e-mail server (2 e-mail servers). I am interested in sending directly to the recipients e-mail server from my code (1 e-mail server). I'm not trying to bypass the recipients e-mail server, just the sending e-mail server. Commented Feb 18, 2014 at 3:05
  • To send email, you need to be either a mail client or a mail server. In most cases, to send mail as a client, you need to be trusted, either by white-listing or by authorization. To send as a server, the authorization rules are often more strict. It really depends on the rules that the recipients' e-mail servers uses to authorize valid senders. Commented Feb 18, 2014 at 12:47
  • @BobDalgleish: If I can manage access to your machine, I can do anything. Including putting mails into your inbox. I agree that this is a merely academic option, but the question was not so clearly scoped.
    – JensG
    Commented Feb 18, 2014 at 13:42
  • @DeanSchulze: I think johannes already answered that particular question and why it is not such a good idea. If I were you, I would favour the service idea.
    – JensG
    Commented Feb 18, 2014 at 13:45
0

If we have to send a mail to somebody from Java code, we need to have access on some mail server credentials. Well, not always.

Google has provided free access to one of its mail servers and you can use it in Java code. Below written code if more like a note to my self. So, if I need it sometime, you can refer here: http://www.computerbuzz.in/2014/02/how-to-send-email-in-java-using-gmail.html

private void setMailServerProperties()
    {
        Properties emailProperties = System.getProperties();
        emailProperties.put("mail.smtp.port", "586");
        emailProperties.put("mail.smtp.auth", "true");
        emailProperties.put("mail.smtp.starttls.enable", "true");
        mailSession = Session.getDefaultInstance(emailProperties, null);
    }

    private MimeMessage draftEmailMessage() throws AddressException, MessagingException
    {
        String[] toEmails = { "[email protected]" };
        String emailSubject = "Test email subject";
        String emailBody = "This is an email sent by http://www.computerbuzz.in.";
        MimeMessage emailMessage = new MimeMessage(mailSession);
        /**
         * Set the mail recipients
         * */
        for (int i = 0; i < toEmails.length; i++)
        {
            emailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(toEmails[i]));
        }
        emailMessage.setSubject(emailSubject);
        /**
         * If sending HTML mail
         * */
        emailMessage.setContent(emailBody, "text/html");
        /**
         * If sending only text mail
         * */
        //emailMessage.setText(emailBody);// for a text email
        return emailMessage;
    }

    private void sendEmail() throws AddressException, MessagingException
    {
        /**
         * Sender's credentials
         * */
        String fromUser = "[email protected]";
        String fromUserEmailPassword = "*******";

        String emailHost = "smtp.gmail.com";
        Transport transport = mailSession.getTransport("smtp");
        transport.connect(emailHost, fromUser, fromUserEmailPassword);
        /**
         * Draft the message
         * */
        MimeMessage emailMessage = draftEmailMessage();
        /**
         * Send the mail
         * */
        transport.sendMessage(emailMessage, emailMessage.getAllRecipients());
        transport.close();
        System.out.println("Email sent successfully.");
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.