File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes sending mail via java mail from Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "sending mail via java mail from" Watch "sending mail via java mail from" New topic

sending mail via java mail from

pawan mishra

Joined: Dec 26, 2011
Posts: 6
package mailsend;

import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.internet.MimeMessage.RecipientType;

import java.util.*;

* Simple Class to send an email using JavaMail API (javax.mail) and Gmail SMTP server
* @author Dunith Dhanushka, deleted
* @version 1.0
public class Main {

private static String HOST = "";
private static String USER = "";
private static String PASSWORD = "****";
private static String PORT = "465";
private static String FROM = "";
private static String TO = "";

private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "";
private static String SUBJECT = "Testing JavaMail API";
private static String TEXT = "This is a test message from my java application. Just ignore it";

public static synchronized void send() {
//Use Properties object to set environment properties
Properties props = new Properties();

props.put("", HOST);
props.put("mail.smtp.port", PORT);
props.put("mail.smtp.user", USER);

props.put("mail.smtp.auth", AUTH);
props.put("mail.smtp.starttls.enable", STARTTLS);
props.put("mail.smtp.debug", DEBUG);

props.put("mail.smtp.socketFactory.port", PORT);
props.put("mail.smtp.socketFactory.class", SOCKET_FACTORY);
props.put("mail.smtp.socketFactory.fallback", "false");

try {

//Obtain the default mail session
Session session = Session.getDefaultInstance(props, null);

//Construct the mail message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(FROM));
message.addRecipient(RecipientType.TO, new InternetAddress(TO));

//Use Transport to deliver the message
Transport transport = session.getTransport("smtp");
transport.connect(HOST, USER, PASSWORD);
transport.sendMessage(message, message.getAllRecipients());

} catch (Exception e) {


public static void main(String[] args) {
System.out.println("Mail sent successfully!");

///////////////////////////////////////////////////////////////////////////////Exception are error not understand please anyone help///////////////////////

java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/Address
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(
at java.lang.ClassLoader.defineClass(
at Method)
at java.lang.ClassLoader.loadClass(
at sun.misc.Launcher$AppClassLoader.loadClass(
at java.lang.ClassLoader.loadClass(
Could not find the main class: mailsend.Main. Program will exit.
Exception in thread "main" Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> * @author Dunith Dhanushka, deleted

1) do you have permission to post the above person's code and email address?
2) Is Swing the best forum for your query?
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46352
Michael Dunn wrote:> * @author Dunith Dhanushka, deleted

1) do you have permission to post the above person's code and email address?
2) Is Swing the best forum for your query?
Good point. I have taken the liberty of removing that e-mail address from both posts. The discussion might be moved elsewhere.

From the error message it looks as if there may be a problem with the Address class. Please send us details of that class.
subject: sending mail via java mail from
It's not a secret anymore!