javamail is not working. MessagingException arises - java

I am doing following code to send a email on gmail with javamail.
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class JavaApplication1 {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("goods.ramesh","mypassword");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("goods.ramesh#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("goods.ramesh#yahoo.com"));
message.setSubject("Testing Subject");
message.setText("Dear Mail Crawler," +
"\n\n No spam to my email, please!");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
But I am getting the following error stacktrace :
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/MessagingException
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2442)
at java.lang.Class.getMethod0(Class.java:2685)
at java.lang.Class.getMethod(Class.java:1620)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:484)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:476)
Exception in thread "main" Java Result: 1
I am not getting what MessagingException is doing and why it occurred.
Can any body help me to get rid of this exception?

I had the same ClassNotFound problem. I have solved with following POM set-up
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.main.extras</groupId>
<artifactId>glassfish-embedded-all</artifactId>
<version>3.1.2</version>
</dependency>
I have used this version of glassfish-embedded-all in my EJB UnitTesting, so my understanding is that this jar contains all classes required by Java EE API

You are probably using the wrong version of javaee.jar that does not contains anything.
Have a look to this post: http://www.mkyong.com/hibernate/java-lang-classformaterror-absent-code-attribute-in-method-that-is-not-native-or-abstract-in-class-file/
M.

Related

JavaMail to minecraft

My friends and I have minecraft server and we want to add JavaMail plugin with Maven , We added 2 jar files:
Mail.jar
Activation.jar
With this code:
package com.parlagames;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class App {
public void AppVoid(String host, String port,final String userName,final String password, String[] toAddress, String subject, String message) {
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port",port);
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
try {
Message SendMessage = new MimeMessage(session);
SendMessage.setFrom(new InternetAddress(userName));
for(int i=0;i<toAddress.length;i++) {
SendMessage.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toAddress[i]));
SendMessage.setSubject(subject);
SendMessage.setContent(message, "text/html; charset=utf-8");
Transport.send(SendMessage);
}
System.out.println("Sent");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
When we start the plugin in his server it shows an error that it doesn't identify the class
java.lang.NoClassDefFoundError: com/parlagames/App
at java.lang.ClassLoader.defineClass1(Native Method) ~[?:1.8.0_161]
at java.lang.ClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.security.SecureClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader.defineClass(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader.access$100(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_161]
at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.8.0_161]
at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_161]
at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_161]
at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:101) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
Why does it happend? we need to have the maven by the way
It seems that you need to add this line of code to the plugin:
public void onEnable()
and this code
public void onDisable()
It also seems that you don't have a main class. A main class is declared at plugin.yml. Try finding the part that says "main:" and change it to the class that has the "onEnable()" and "onDisable()". Also add extends JavaPlugin as someone said before
Is App the main class of your plugin ? If so, it you need to make it extend the JavaPlugin class like this :
public class MyPlugin extends JavaPlugin {
public void onEnable() {
}
public void onDisable() {
}
}
If you have trouble understanding of the bukkit/spigot API, I would suggest to start learning from the docs (here is a reference guide for the basics).

Java Email SMTP & TSL, Error Can't sending email

I have problem with Java Email using SMTP & TSl, please help me resolve this problem. I think the problem was Transport, but I'm not sure. Below is my code and error snippets, please help me find my mistake and solution.
TSLEmail.class
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
public class TSLEmail
{
public static void main(String[] args)
{
final String fromEmail = "mail#gmail.com"; //requires valid
gmail id
final String password = "mypass12"; // correct password for gmail id
final String toEmail = "recipients#gmail.com"; // can be any email id
System.out.println("TLSEmail Start");
Properties props = new Properties();
props.put("mail.smtp.host", "10.20.200.220"); //SMTP Host
props.put("mail.user", "user12");
//props.put("mail.password", password);
props.put("mail.smtp.port", "587"); //TLS Port
props.put("mail.smtp.auth", "true"); //enable authentication
props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
//create Authenticator object to pass in Session.getInstance argument
Authenticator auth = new Authenticator() {
//override the getPasswordAuthentication method
protected PasswordAuthentication
getPasswordAuthentication() {
return new
PasswordAuthentication(fromEmail, password);
}
};
Session session = Session.getInstance(props, auth);
EmailUtil.sendEmail(session, toEmail,"TLSEmail Testing Subject",
"TLSEmail Testing Body");
}
}
EmailUtil.class
import java.util.Date;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailUtil
{
public static void sendEmail(Session session, String toEmail, String
subject, String body)
{
try
{
MimeMessage msg = new MimeMessage(session);
//set message headers
msg.addHeader("Content-type", "text/HTML; charset=UTF-8");
msg.addHeader("format", "flowed");
msg.addHeader("Content-Transfer-Encoding", "8bit");
msg.setFrom(new InternetAddress("mail#gmail.com"));
msg.setReplyTo(InternetAddress.parse("recipients#gmail.com"));
msg.setSubject(subject, "UTF-8");
msg.setText(body, "UTF-8");
msg.setSentDate(new Date());
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(toEmail, false));
System.out.println("Message is ready");
Transport.send(msg);
System.out.println("EMail Sent Successfully!!");
}
catch (Exception e) {
e.printStackTrace();
}
}
}
The Results(ERROR):
TLSEmail Start
Message is ready
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1564)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1551)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:935)
at
com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:426)
at javax.mail.Service.connect(Service.java:310)
at javax.mail.Service.connect(Service.java:169)
at javax.mail.Service.connect(Service.java:118)
at javax.mail.Transport.send0(Transport.java:188)
at javax.mail.Transport.send(Transport.java:118)
at com.bca.controller.EmailUtil.sendEmail(EmailUtil.java:35)
at com.bca.controller.TSLEmail.main(TSLEmail.java:34)
Caused by: javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find
valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Unknown Source)
at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
at sun.security.ssl.Handshaker.processLoop(Unknown Source)
at sun.security.ssl.Handshaker.process_record(Unknown Source)
at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown
Source)
at sun.security.ssl.SSLSocketImpl.writeRecord(Unknown Source)
at sun.security.ssl.AppOutputStream.write(Unknown Source)
at com.sun.mail.util.TraceOutputStream.write(TraceOutputStream.java:114)
at java.io.BufferedOutputStream.flushBuffer(Unknown Source)
at java.io.BufferedOutputStream.flush(Unknown Source)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1562)
... 10 more
Caused by: sun.security.validator.ValidatorException: PKIX path building
failed: sun.security.provider.certpath.SunCertPathBuilderException: unable
to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
at sun.security.validator.Validator.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown
Source)
... 22 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException:
unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown
Source)
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown
Source)
at java.security.cert.CertPathBuilder.build(Unknown Source)
... 28 more
Please help me find an simple and certain solution for my problem.
Thank You
It sounds like you need to add the certificates returned by the Server into your JRE's truststore,
There are many good articles that explain how to do this, including:
Java Keytool Essentials: Working with Java Keystores
If this is a web app (for example, Tomcat or JBoss), then do this at the server level (instead of your JRE).
Add this property too. It worked for me.
props.put("mail.smtp.ssl.trust", Smtp_host);

java.lang.LinkageError: loader constraint violation:previously initiated loading for a different type with name "javax/mail/Session"

I am getting the following error when I try to send email using javax.mail-api:
Caused by: java.lang.LinkageError: loader constraint violation: loader (instance of org/apache/felix/framework/BundleWiringImpl$BundleClassLoaderJava5) previously initiated loading for a different type with name "javax/mail/Session"
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2279)
at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1501)
at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:75)
at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:1955)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.sun.mail.util.PropUtil.getBooleanSessionProperty(PropUtil.java:86)
at javax.mail.internet.MimeMessage.initStrict(MimeMessage.java:320)
at javax.mail.internet.MimeMessage.<init>(MimeMessage.java:195)
at sendEmail(manage.java:216)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
Code:
Public void sendEmail() {
String to = "abc#abc.com";
String from = "efg#efg.com";
final String username = "abc#abc.com";
final String password = "password";
Properties properties = new Properties();
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.host", "smtp.office365.com");
properties.put("mail.smtp.port", "587");
Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
System.out.println("Inside test");
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Testing Subject");
message.setText("This is message body");
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
My maven dependency:
<dependency>
<groupId>javax.mail</groupId>
<artifactId>javax.mail-api</artifactId>
<version>1.5.5</version>
</dependency>
Please help.
As suggested in comments, add your dependency to javamail as provided dependency:
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.1</version>
<scope>provided</scope>
</dependency>
This will skip adding duplicate jars which would then be loaded by different classloaders.
If not somehow forced to use old version of javamail you should update to latest which is currently
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.5.5</version>
<scope>provided</scope>
</dependency>
I had a very similar problem (with org.keycloak.keycloak-saml-adapter-api-public), and solved it by editing my standalone.xml, and removing this line:
<profile>
<subsystem xmlns="urn:jboss:domain:ee:4.0">
<global-modules>
<module name="name.of.offending.package"/><!--REMOVE THIS LINE-->
</global-modules>
...
You'll have to restart your server after this change. Good luck.

exception while sending mail through java code in netbeans while not in eclipse

using below code i can send mail through eclipse but using same code in netbeans gives exception as below.
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
public class SendMailSSL {
public static void main(String[] args) {
String to="shahjoy**#gmail.com";//change accordingly
final String user="shahjoy***#gmail.com"; //change accordingly
final String password="***";//change accordingly
//1) get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com"); // for gmail use smtp.gmail.com
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user,password);
}
});
//2) compose message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Message Alert");
//3) create MimeBodyPart object and set your message text
BodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setText("This is message body");
//4) create new MimeBodyPart object and set DataHandler object to this object
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
// String filename = "SendMailSSL.txt";//change accordingly
// DataSource source = new FileDataSource(filename);
// messageBodyPart2.setDataHandler(new DataHandler(source));
//messageBodyPart2.setFileName(filename);
//5) create Multipart object and add MimeBodyPart objects to this object
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart1);
//multipart.addBodyPart(messageBodyPart2);
// 6) set the multipart object to the message object
message.setContent(multipart );
//) send message
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {
ex.printStackTrace();
}
} }
Exception in netbeans:
javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
at javax.mail.Service.connect(Service.java:295)
at javax.mail.Service.connect(Service.java:176)
at sendmailssl.SendMailSSL.send(SendMailSSL.java:61)
at sendmailssl.SendMailSSL.main(SendMailSSL.java:73)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1884)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1439)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:209)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:878)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:814)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:507)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)
... 5 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1421)
... 15 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 21 more
A certificate seems to be missing in the classpath. Please check - perhaps Netbeans is using a different version of JDK/JRE than eclipse?

Java program working on Windows 7, but not windows 8?

You people are making me self conscious.. I will try to make this one better.
Okay, so this program (Don't kill me.. I downloaded it) is only working on Windows 7 and Ubuntu as far as I can tell. When you open it on Windows 8 it says "Java exception Error."
I'm thinking this has something to do with catch(messagingException ex) at the end of the file. I admit, I don't know a whole lot about java, but you have to start somewhere.. don't you? I do know java is for all platforms!
I have also tried this program with multiple files and multiple Gmail accounts... I even tried it with my Comcast email address.
I'm using the "JavaMailAPI" (http://www.oracle.com/technetwork/java/javamail/index.html) for the actual mailing part.
When I open it on terminal in Windows 8 it gives me this:
Exception in thread "Main" java.lang.noclassdeffounderror: java/mail/mailexception
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.langf.Class.privateGetDdecLaredMethods(Unkown source)
at java.lang.Class.getMethod(unknown source)
at sun.launcher.LauncherHelper.getMainMethod(Unknown source)
caused by: java.lang.classnotfoundexception: java.mail.messagingException
at java.net.URLCLassLoader$1.run(unknown source)
at java.net.URLClassLoader$1.run(Unknown source)
at java.security.AccessController.doPrivaleged(native Method)
at java.net.URLClassLoader.findClass(Unknown source)
at java.lang.ClassLoader.findClass(Unkown source)
at jaa.lang.CLassLoader.loadClass(Unknown source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown source)
at java.lang.ClassLoader.loadClass(Unknown source)
... 6 more
Here is the code:
package testing;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Testing{
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("Email","Password");
}
});
try {
BodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart();
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("Email Address"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("Email Address to send "));
message.setSubject("Subject");
message.setText("Message");
String filename = "attachment location";
DataSource source = new FileDataSource(filename);
message.setDataHandler(new DataHandler(source));
message.setFileName(filename);
multipart.addBodyPart(messageBodyPart);
Transport.send(message);
System.out.println("Done");
} catch (MessagingException ex) {
Logger.getLogger(Testing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
If you need anything else about the program please ASK!
You have to download Java Mail
If JavaMail is needed by this application you should build an installer to package and install the needed JARs as JavaMail.

Categories

Resources