Java Mailing

cheenu78 0 Tallied Votes 200 Views Share

A program that can send mails. This requires that you have activation.jar and mail.jar in you classpath.

/*
to run this example u may require activation.jar and mail.jar in ur classpath
*/

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
import java.io.*;
public class SimpleSender
{
  	public static void main(String args[])
  	{
    		try
    		{
      			String smtpServer=args[0];
      			String to=args[1];
      			String from=args[2];
      			String body=args[3];
      			send(smtpServer, to, from,body);
    		}
    		catch (Exception ex)
    		{
      		System.out.println("Usage :\njava SimpleSender server to from body");
    		}
   	System.exit(0);
  	}

	public static void send(String smtpServer, String to, String from, String body)
  	{
   		try
    		{
      		Properties props = System.getProperties();
      		props.put("mail.smtp.host", smtpServer);
      		Session session = Session.getDefaultInstance(props, null);
      		MimeMessage msg = new MimeMessage(session);
      		msg.setFrom(new InternetAddress(from));
      		msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to, false));
      		msg.setSubject("Test Mail thru java");
			msg.setContent(body,"text/plain");
      		msg.setSentDate(new Date());
      		Transport.send(msg);
      		System.out.println("Message sent OK.");
    	}
    	catch (Exception ex)
    	{
      		ex.printStackTrace();
    	}
}


}

Dani AI

Generated

A few practical updates and tips that fill gaps in the thread and keep the example working today.

The jars: the JavaMail project moved to the Eclipse/Jakarta ecosystem (now “Jakarta Mail”) and the activation framework has corresponding Jakarta artifacts. For new projects prefer the Jakarta/Maven artifacts; if you want to keep the old javax.mail imports use the 1.x artifact. See the Jakarta Mail download/overview for details. (jakartaee.github.io) (repo.maven.apache.org)

What the command-line args mean (answering ): args[0] is the SMTP host (e.g. smtp.example.com), args[1] the recipient, args[2] the sender, args[3] the message body. To run with the required jars on the classpath (Unix / Windows examples):

# Unix/mac
java -cp .:lib/* SimpleSender smtp.example.com recipient@example.com sender@example.com "Hello"

# Windows
java -cp .;lib/* SimpleSender smtp.example.com recipient@example.com sender@example.com "Hello"

If your SMTP server uses a non-default port, STARTTLS or authentication you must set mail.smtp.port, mail.smtp.starttls.enable and mail.smtp.auth (and use an Authenticator) rather than only mail.smtp.host. The JavaMail docs explain the relevant properties. (oracle.com)

Troubleshooting the “The import javax.mail cannot be resolved” error (): that means the mail/activation jars aren’t on the project classpath. Add them to your IDE’s build path (or run with -cp), or declare them as Maven/Gradle dependencies; the Eclipse docs show how to add external JARs to the build path. (help.eclipse.org)

Sending links / HTML (): send HTML content, for example:

msg.setContent("<a href=\"https://example.com\">Click here</a>", "text/html; charset=UTF-8");

or use msg.setText(body, "UTF-8", "html") to set the subtype. The MimeMessage API documents these methods. (docs.oracle.com)

Quick tips: prefer dependency management (Maven/Gradle) to manual jars; enable mail.debug=true while troubleshooting; and remember many providers require TLS/auth (or OAuth2) so plain unauthenticated sends may be refused.

monga monga 0 Newbie Poster

I do not know where I can get activation.jar and mail.jar

AhmedHan 0 Junior Poster in Training

Yes, it is not explained where to get these jar files.

peter_budo 2,532 Code tags enforcer Team Colleague Featured Poster

mail.jar is part of JavaMail 1.4 and you can download mail.jar from here http://java.sun.com/products/javamail/downloads/index.html

activation.jar is part of JavaBeans Activation Framework 1.1 and can be download here http://java.sun.com/products/javabeans/jaf/downloads/index.html#download

brittoonline 0 Newbie Poster

What do i give as command line arguments for arg[0],arg[1],arg[2] can u be more specific

Jocamps 14 Junior Poster

How do i solve this error?

"The import.javax.mail cannot be resolved"

balagangadharm 0 Light Poster

how to send links in the above code

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.