send email (from ASP to JSP)

Dear all,
I have this in asp code:
Function SendEmail(Contents, Subject, From, ToEmail, BBCEmail)
  MailServer = "mail"  
  MailServerUserName = "MailServerUserName"
  MailServerPassWord = "MailServerPassword"
  IF BBCEmail = "" Then BBCEmail = "x@y.com"  
  ' Popup before SMTP
   Set pop3 = Server.CreateObject("JMail.POP3")             
   on error resume next
   pop3.Connect MailServerUserName , MailServerPassWord , MailServer             
   pop3.Disconnect
   on error goto 0
   set Message = Server.CreateObject("Persits.MailSender")
   Message.Host = MailServer
            Message.Charset ="windows-1256"
            Message.IsHTML = True
            Message.From = From
            Message.Subject = Subject
            Message.Body = Contents
            Message.AddAddress ToEmail
             If BBCEmail <> "" then
             Message.AddBcc BBCEmail
          End If         
            on error resume next
            Message.Send
            on error goto 0
          Set Message = Nothing          
End Function

I want to rewrite it in jsp,

please help.

Thanks.
ethar1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TimYatesCommented:
http://www.jsptut.com/Email.jsp

goes through how to send main in a JSP :-)

Tim
0
ethar1Author Commented:
I am using component there and I want to use it it jsp
0
TimYatesCommented:
>>   I am using component there and I want to use it it jsp

I don't understand what you mean...
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

ethar1Author Commented:
I want same function as it is in jsp, ( for educational purposes ).
0
TimYatesCommented:
<%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%!
    public void sendEmail( String contents, String subject, String fromAddr, String toAddr, String bccAddr )
    {
        String mailServer = "mail" ;
        String mailServerUserName = "MailServerUserName" ;
        String mailServerPassWord = "MailServerPassword" ;

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.mail.example.com");
        props.put("mail.smtp.auth", "true");

        Session s = Session.getInstance(props);
        Transport transport =  s.getTransport( "smtp" );
        transport.connect( mailServer, mailServerUserName, mailServerPassWord ) ;

        MimeMessage message = new MimeMessage(s);

        InternetAddress from = new InternetAddress( fromAddr );
        message.setFrom(from);

        InternetAddress to = new InternetAddress( toAddr );
        message.addRecipient(Message.RecipientType.TO, to);

        InternetAddress bcc = new InternetAddress( bccAddr );
        message.addRecipient(Message.RecipientType.BCC, bcc);

        message.setSubject( subject );
        message.setText( contents );
       
        transport.send( message );
    }
%>

or something..
0
TimYatesCommented:
Sorry:

        props.put("mail.smtp.host", "smtp.mail.example.com");

should be:

        props.put("mail.smtp.host", mailServer );
0
orhanbabaCommented:
props.put(128.56.54.8:25, mailServer ); if smtp server not specified put port (ip is fake)
if you plan to use pop account you need to check user/pass
0
TimYatesCommented:
>>  props.put(128.56.54.8:25, mailServer ); if smtp server not specified put port (ip is fake)

??

You mean:

        String mailServer = "128.56.54.8:25" ;

in my example?
0
orhanbabaCommented:
sometimes servers uses port instead of mail.domain.com or ip.
0
TimYatesCommented:
But what would:

props.put(128.56.54.8:25, mailServer );

do?

I don't believe that's valid java...
0
orhanbabaCommented:
i developed an applicaiton and it didnt work until i fix it i described before
0
ethar1Author Commented:
any thing fixed in java?
even the java compailer can't define the line of code that has an error.

can anybody give the same exact code ( above) in java?

can I use the same component?

I am a newbie and I will appreciate the EXACT answer...
0
TimYatesCommented:
>> can anybody give the same exact code ( above) in java?

No.  Java is a different language with different format

>> can I use the same component?

No

>> I am a newbie and I will appreciate the EXACT answer...

Can't be done..  You can just rewrite it to work in java but the code will be different.
0
ethar1Author Commented:
when I try to execute that code i got those errors:
Session cannot be resolved or is not a type
Session cannot be resolved
Transport cannot be resolved or is not a type
MimeMessage cannot be resolved or is not a type
MimeMessage cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
Message.RecipientType.TO cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
InternetAddress cannot be resolved or is not a type
Message.RecipientType.BCC cannot be resolved or is not a type
0
TimYatesCommented:
You need the javamail api:

http://java.sun.com/products/javamail/downloads/index.html

and the activation framework:

http://java.sun.com/products/javabeans/glasgow/jaf.html

Then put mail.jar and activation.jar in WEB-INF/lib of your webapp
0
ethar1Author Commented:
done.
( restart the server)
but still got those error:
Unhandled exception type NoSuchProviderException
Unhandled exception type MessagingException
Unhandled exception type AddressException
Unhandled exception type MessagingException
Unhandled exception type AddressException
Unhandled exception type MessagingException
Unhandled exception type AddressException
Unhandled exception type MessagingException
Unhandled exception type MessagingException
Unhandled exception type MessagingException
Unhandled exception type MessagingException

0
TimYatesCommented:
Ahhh yes, sorry... do:

 <%@ page import="java.util.*, javax.mail.*, javax.mail.internet.*" %>
<%!
    public void sendEmail( String contents, String subject, String fromAddr, String toAddr, String bccAddr )
    {
      try
      {
        String mailServer = "mail" ;
        String mailServerUserName = "MailServerUserName" ;
        String mailServerPassWord = "MailServerPassword" ;

        Properties props = new Properties();
        props.put("mail.smtp.host", mailServer );
        props.put("mail.smtp.auth", "true");

        Session s = Session.getInstance(props);
        Transport transport =  s.getTransport( "smtp" );
        transport.connect( mailServer, mailServerUserName, mailServerPassWord ) ;

        MimeMessage message = new MimeMessage(s);

        InternetAddress from = new InternetAddress( fromAddr );
        message.setFrom(from);

        InternetAddress to = new InternetAddress( toAddr );
        message.addRecipient(Message.RecipientType.TO, to);

        InternetAddress bcc = new InternetAddress( bccAddr );
        message.addRecipient(Message.RecipientType.BCC, bcc);

        message.setSubject( subject );
        message.setText( contents );
       
        transport.send( message );
      }
      catch( Exception ex )
      {
          ex.printStackTrace() ;
      }
    }
%>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ethar1Author Commented:
yes , working without error after 20 days of fighting with this language...
but never got any emails :)

I have same code in ASP and can send using same setting and same server.
wot do u think the problem?
0
ethar1Author Commented:
I correcte the code :
Inestead of
        props.put("mail.smtp.host", mailServer );
        props.put("mail.smtp.auth", "true");
I use:
        props.put("mail.host", mailServer );
        props.put("mail.auth", "true");

Now another smal thing, I want to send html msgs, wot to do?
thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.