Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 449
  • Last Modified:

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.
0
ethar1
Asked:
ethar1
  • 9
  • 7
  • 3
1 Solution
 
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
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.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 7
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now