Hi I tried your final code in Eclips and i still get the following error could anyone help me with it:
java.lang.ClassFormatError: Absent Code attribute in method that is not native or abstract in class file javax/mail/BodyPart
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Here is the code i tried:
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class sendmail
{
private static final
String hostName = "smtp.gmail.com";
private static final int hostPort = 465;
private static final String senderAddress = "
[email protected]";
private static final String password = "mypassword";
public void sendEmail(String company, String licence, String remdate, String expdate, String attachments, String supportcontract, String signedcontract, String licenceorigin) throws Exception
{
//Properties
Properties props = new Properties();
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.host", hostName);
props.put("mail.smtps.auth", "true");
//Session
Session mailSession = Session.getDefaultInstance(props);
mailSession.setDebug(true);
//Text
MimeBodyPart text = new MimeBodyPart();
text.setText("Company: " + company + "\n Licence: " + licence + "\n Expiration Date: " + expdate + "\n Support Contract: " + supportcontract +
"\n Signed Contract: " + signedcontract + "\n Licence Origin: " + licenceorigin);
//Attachment
FileDataSource fds = new FileDataSource("C:\\temp.png");
MimeBodyPart attachment = new MimeBodyPart();
attachment.setDataHandler(new DataHandler(fds));
attachment.setFileName(fds.getName());
//Create Multipart & Add Parts
Multipart content = new MimeMultipart();
content.addBodyPart(text);
content.addBodyPart(attachment);
//Compile Message
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("Testing Email");
message.setContent(content);
message.addRecipient(Message.RecipientType.TO, new InternetAddress("
[email protected]"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("
[email protected]"));
//Send Message
Transport transport = mailSession.getTransport();
transport.connect(hostName, hostPort, senderAddress, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
}
public static void main(String[] args) throws Exception
{
new sendmail().sendEmail("a","b","c","d","e","f","g","h");
}
}