?
Solved

send email (from ASP to JSP)

Posted on 2005-04-01
19
Medium Priority
?
432 Views
Last Modified: 2008-02-20
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.
0
Comment
Question by:ethar1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 3
19 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13679983
http://www.jsptut.com/Email.jsp

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

Tim
0
 

Author Comment

by:ethar1
ID: 13680023
I am using component there and I want to use it it jsp
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13680053
>>   I am using component there and I want to use it it jsp

I don't understand what you mean...
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:ethar1
ID: 13680061
I want same function as it is in jsp, ( for educational purposes ).
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13680108
<%@ 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
 
LVL 35

Expert Comment

by:TimYates
ID: 13680113
Sorry:

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

should be:

        props.put("mail.smtp.host", mailServer );
0
 
LVL 4

Expert Comment

by:orhanbaba
ID: 13680160
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13680191
>>  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
 
LVL 4

Expert Comment

by:orhanbaba
ID: 13680301
sometimes servers uses port instead of mail.domain.com or ip.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13680319
But what would:

props.put(128.56.54.8:25, mailServer );

do?

I don't believe that's valid java...
0
 
LVL 4

Expert Comment

by:orhanbaba
ID: 13680518
i developed an applicaiton and it didnt work until i fix it i described before
0
 

Author Comment

by:ethar1
ID: 13680562
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13680610
>> 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
 

Author Comment

by:ethar1
ID: 13691115
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13691464
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
 

Author Comment

by:ethar1
ID: 13692406
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
 
LVL 35

Accepted Solution

by:
TimYates earned 2000 total points
ID: 13692492
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
 

Author Comment

by:ethar1
ID: 13693253
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
 

Author Comment

by:ethar1
ID: 13693639
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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
This month, Experts Exchange’s free Course of the Month is focused on CompTIA IT Fundamentals.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month13 days, 16 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question