1
votes

Through my application using Javamail API if I want to send email between any two external email addresses say gmail->yahoo or yahoo->gmail or any other email account without using authentication mechanism how should I configure mail.smtp.host property?

What is the correct way of configuring javamail properties for sending emails between any two external email addresses ?

Sample code to send mail is given below:

Session session = Session.getDefaultInstance(new Properties(),null);
MimeMessage message = new MimeMessage(session);   
message.setFrom(new InternetAddress("[email protected]"));  
InternetAddress[] toAddress = {new InternetAddress("[email protected]")};  
message.setRecipients(Message.RecipientType.TO, toAddress);  
message.setSubject("test mail");  message.setText("test body");  
Transport.send(message);
2

2 Answers

0
votes

Most public mail servers require authentication. If you want to do it without authentication, you'll need to run your own mail server.

0
votes

This is for gmail, try it. You need mail.jar

public static void main(String[] args) {
    final String username = "[email protected]";
    final String password = "your-pwd";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
            new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(username, password);
                }
            });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("A Mail Subject");
        message.setText("Hey I'm sending mail using java api");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }

}

Edit :

Link to download Java mail Api along with mail.jar