I've been copying this code http://www.tutorialspoint.com/java/java_sending_email.htm
and I get the error
javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 465;
nested exception is:
java.net.SocketException: Connection reset
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1963)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
at javax.mail.Service.connect(Service.java:345)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at racetiming.MailSender.send(MailSender.java:68)
All the solutions I'm looking up are using authentication to log in to a mail server but I'm trying to do it without a login. Is this not possible? Seems like the tutorials are trying to do it without credentials.
Here's the entirety of my code.
public class MailSender
{
public static void send(Racer r, String filename){
// Recipient's email ID needs to be mentioned.
String to = r.getEmail();
// Sender's email ID needs to be mentioned
String from = "donotreplyRFID@gmail.com";
// 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);
properties.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// 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("Report for " + r.getName());
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("See attachment");
// Create a multipart message
Multipart multipart = new MimeMultipart();
// Set text message part
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport transport = session.getTransport("smtps");
transport.connect();
transport.sendMessage(message, message.getAllRecipients());
System.out.println("Sent message successfully....");
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}
It's up to your server whether it requires authentication or not. Some intranet mail servers might not. Most internet mail servers will, to prevent spamming.
You'll also want to read this JavaMail FAQ entry about common mistakes.
It is possible, but you need to lookup yourself the MX records in DNS. You can use dnsjava-2.16.jar library for that. Add dnsjava and javax.mail libraries to build path to compile the following code sample. Check your spam folder after trying to deliver the message this way: because there is no DKIM signature, and the IP address you use to send the email does not belong to addresses allowed for this host ("yourhost.org"), the receiving email server will likely put the email into spam folder.