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