BOBBY
BOBBY

Reputation: 61

How to send an email from a PC by a Java program?

What is required for sending mail from my computer by a Java program? I mean any changes, like enabling or disabling options, should be done from the PC.

Upvotes: 1

Views: 2348

Answers (4)

Crollster
Crollster

Reputation: 2771

You should have access to an SMTP server through which you mail can be sent. Also you will need to check that any firewall you have installed allows outgoing traffic on port 25 to communicate with the SMTP server.

Edit: if, as you mention below, you have no SMTP server access, you could sign up for a gmail account for your application and make use of the Gmail SMTP server (obviously not ideal for a business app, but perfectly fine as a personal app. For instruction on how to set this up, read this Lifehacker post.

Upvotes: 2

kasavbere
kasavbere

Reputation: 6003

Just change the email address and password. This example uses gmail. Also, you can have as many recipients as you wish.

 import java.util.Properties;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;


 public class AnotherMail {

public static void main(String... args) {
    String host = "smtp.gmail.com";
    String from = "[email protected]";
    String pass = "MyPassword";
    Properties props = System.getProperties();
    props.put("mail.smtp.starttls.enable", "true"); // added this line
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    String[] to = {"[email protected]"}; // added this line
    try {
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));

        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for (int i = 0; i < to.length; i++) { // changed from a while loop
            toAddress[i] = new InternetAddress(to[i]);
        }
        System.out.println(Message.RecipientType.TO);

        for (int i = 0; i < toAddress.length; i++) { // changed from a while loop
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }
        message.setSubject("sending in a group");
        message.setText("Welcome to JavaMail");
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();

    } catch (MessagingException mx) {
        mx.printStackTrace();
    }

}
 }

Upvotes: 0

The canonical way of creating and sending MIME-based email messages from Java (so it can contain HTML and images), is using JavaMail which is a very capable package, and which can even be taught to send mail through GMail over SSL if you do not have an internal SMTP-server available.

See http://www.mkyong.com/java/javamail-api-sending-email-via-gmail-smtp-example/ for two examples of how to do it.

Upvotes: 0

Magesh khanna
Magesh khanna

Reputation: 193

Java has built in libraries for that.

import javax.mail.*;
import javax.mail.internet.*;

are the libraries you will need.

You need to have mail.jar in your classpath because it is not part of core Java.

Upvotes: 2

Related Questions