This discussion is archived
6 Replies Latest reply: Jun 15, 2012 8:56 AM by 734840 RSS

generating the email

734840 Newbie
Currently Being Moderated
Hello.
and thanks
I am trying to understand the java mail concept. I was thinking that the
glassfish server need to handle HTTP or something because I am not using a IE connection
right now.

I have Outlook on the computer. It does not support SMTP . furthermore, I guess it is
not set up with the mail in and out settings or what ever it needs.

From what I understand I just call this class(above) and it should open Outlook
with a fake email in it. That is all I need for now. I am using jdk1.7.0_01

do I need to install anything to run this program. I am under the understanding that
jdk versions after jdk1.6 will handle the java mail api.

I am not sure of any properties I need to set. I have an error now. I have a var smtp
that is supposed to represent some kind of protocal and I am not sure exactly how
my sample program is to use the protocal var.
Thanks again for your time.


exceptions:
 Exception in thread "AWT-EventQueue-0" 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)
calling method:
 public static boolean printAndSendEmailMatrix(int element) throws MessagingException {


       boolean bSend=false;
           String thisTo=printEmailToList(element);
           String thisFrom= printEmailFromList(element);
            String thisMessage=printMessageList(element);
            String thisSubject=printEmailSubjectList(element);
             String thisProtocal= printEmailProtocalList(element);
          
            
          SendMail ss= new SendMail( thisFrom,thisTo,thisSubject,thisMessage);
          ss.send();

 bSend=true;
 return bSend;
         
   
    }
outlook info
Take advantage of Outlook's Internet protocol support for POP3/SMTP, IMAP4, LDAP, NNTP, S/MIME, HTML Mail, vCard, and iCalendar.



The sample program used to send:


package model.email;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


 private String from;
    private String to;
    private String subject;
    private String text;

    public SendMail(String from, String to, String subject, String text) {
       
    
        this.from = from;
        this.to = to;
        this.subject = subject;
        this.text = text;
    }

    public void send() throws MessagingException {
        
    
        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");//or "25"
        Session mailSession = Session.getDefaultInstance(props);
        Message simpleMessage = new MimeMessage(mailSession);
        InternetAddress fromAddress = null;
        InternetAddress toAddress = null;
        try {
            fromAddress = new InternetAddress(from);
            toAddress = new InternetAddress(to);
        } catch (AddressException e) {
            // TODO Auto-generated catch block          
            e.printStackTrace();
        }
        try {
            simpleMessage.setFrom(fromAddress);
            simpleMessage.setRecipient(RecipientType.TO, toAddress);
            simpleMessage.setSubject(subject);
            simpleMessage.setText(text);
            Transport.send(simpleMessage);
        } catch (MessagingException e) {

            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
/*
 * The above program accepts the sender,
 receiver,
 subject 
and the data in its constructor. 
After giving this information, the mail can be sent by calling the SendMail.send() method. 
Let us have a deeper look into the implementation of this method. 
The first thing is that a Mail Session has to be established with some properties for sending or receiving a mail.
 The mandatory property is the server name. In the above code, we have given the SMTP Mail server of Google. 
Then, it is optional to provide the port information, and it is needed if it is different from the default port of 25.
 Then we construct a Message object for the Mail session by populating the information like sender,
 receiver, subject and text. 
Then the message is sent by calling the Transport.send() method. 
 */
Thanks for your time.
  • 1. Re: generating the email
    bshannon Pro
    Currently Being Moderated
    I can't quite tell what you're trying to do, but there's a lot of confusion in your message.
    You probably need to spend some quality time with the javaMail FAQ:
    http://www.oracle.com/technetwork/java/javamail/faq/index.html

    Outlook is an email client. JavaMail is a library that allows you to write your own email client.

    Outlook is an email client with a graphical user interface. JavaMail doesn't require or support
    any particular user interface style.

    Outlook is usually used to talk to an email server such as Exchange. JavaMail talks to email
    servers using one of the standard internet protocols such as SMTP.

    If you want your Java program to invoke the Outlook user interface to allow the user to send
    mail, see the java.awt.Desktop class.
  • 2. Re: generating the email
    734840 Newbie
    Currently Being Moderated
    Thanks for your reply:
    I have written classes to provide my class with the "from,to,content,and subject. I just do not understand the exception.
    There that other variable setting dealing with protcall that does not make
    sense.
    I just thought it was using a 3rd party app to create emails.

    What is this exception about. I am not sure what to set the protocall variable to.
    Thank you again for your time.
  • 3. Re: generating the email
    bshannon Pro
    Currently Being Moderated
    I can't give you a tutorial on internet email or JavaMail here; read the FAQ.

    The exception is probably because you have javaee.jar or j2ee.jar on your CLASSPATH.
  • 4. Re: generating the email
    734840 Newbie
    Currently Being Moderated
    I have been looking at FAQ possibilities. Thanks.
    I have googled and found that netbeans may not handle the task . Eclipse does.
    Do you have a professonally written program to look at . I paid a lot of money at the college yet they do not
    deliver any profesional anything. I paid for info but got nothing. So here I am trying to teach myself.
  • 5. Re: generating the email
    bshannon Pro
    Currently Being Moderated
    I don't know what it is you think NetBeans won't handle, but it will certainly allow you to write a program using JavaMail.

    Since you looked at the JavaMail FAQ, you probably also saw the links to additional information, including tutorials, short courses, books, online articles, etc.:
    http://www.oracle.com/technetwork/java/javamail/index-138267.html

    You probably also saw the links to third party products, many of which would serve as larger examples of using JavaMail:
    http://www.oracle.com/technetwork/java/javamail/third-party-136965.html

    If that's not enough to allow you to learn JavaMail, it's time to pay someone to do the work for you.
  • 6. Re: generating the email
    734840 Newbie
    Currently Being Moderated
    Thanks. I did not see anything when I looked. I was clearly looking in the wrong FAQ. I will look through that info.
    thanks again.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points