Meaningless Drivel is fun!*
The moose likes Swing / AWT / SWT and the fly likes sending mail via java mail from gmail.com 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 gmail.com" Watch "sending mail via java mail from gmail.com" New topic
Author

sending mail via java mail from gmail.com

pawan mishra
Greenhorn

Joined: Dec 26, 2011
Posts: 3
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 = "smtp.gmail.com";
private static String USER = "xyz@gmail.com";
private static String PASSWORD = "****";
private static String PORT = "465";
private static String FROM = "xyz@gmail.com";
private static String TO = "xyz@gmail.com";

private static String STARTTLS = "true";
private static String AUTH = "true";
private static String DEBUG = "true";
private static String SOCKET_FACTORY = "javax.net.ssl.SSLSocketFactory";
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("mail.smtp.host", 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);
session.setDebug(true);

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

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

} catch (Exception e) {
e.printStackTrace();
}

}

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


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

run:
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(ClassLoader.java:631)
at java.lang.ClassLoader.defineClass(ClassLoader.java:615)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
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
Sheriff

Joined: Oct 13, 2005
Posts: 38478
    
  23
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sending mail via java mail from gmail.com