Solved

Emailing form based on selected options

Posted on 2006-06-12
11
187 Views
Last Modified: 2011-04-14
What is the best way to email a form based on a selected radio button in Frontpage?
0
Comment
Question by:ITHelper80
  • 5
  • 3
11 Comments
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 16922225
....u mean send email from the data that is collected from the form using radio button.

Can u please explain more clearly

____Jags
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 16922662
Sure....I guess I should have done that in the first place.

Ok here is the deal. I want to create an email form with lets say 5 radio buttons. If a user selects the first radio button then an email will be sent to a group with all the details of that radio button, if the second radio button is selected then it goes to a different group, etc, etc. Does that help?
0
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 16922733
ok....you might want to use some server-side tech...what are u using java or .net. A have a sample code that does something similar to that done using servlets and jsp .

_____Jags.
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 16922785
Well thats the deal, Im not a web developer, this has just been dumped into my lap. Thats why I want something simple, I know right now we have tomcat running and use jsp, but I dont know how to use or code ethier.....
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 16922821
ok...when u say u are not a web developer its a lot of work for you now. It really takes time for me to customize the code inorder to satisfy your needs. Instead i can give fragments of code that does your work . its a jsp page with 2 other servlets and a class file.

____Jags.
0
 
LVL 3

Expert Comment

by:gugfuz
ID: 16942345
Should this proces of emailing be invisible for the user, or don't you mind that the form opens an email with your data that the user has to send?
0
 
LVL 6

Author Comment

by:ITHelper80
ID: 16942961
I need it to be invisible to the user.
0
 
LVL 10

Expert Comment

by:jagadeesh_motamarri
ID: 16943052
ok...Sample Code for sending the email with out attachments -

CODE:

package njit.capstone.proj;

import java.io.File;
import java.security.Security;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class mailingPackage {

      private static final String SMTP_HOST_NAME = "smtp.gmail.com";

      private static final String SMTP_PORT = "465";

      private static final String emailSubjectTxt = "A test from gmail";

      private static final String emailFromAddress = ""; // use from address here

      private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

      //private static final String[] sendTo = { "jagadeesh_motamarri@yahoo.com" };  
      private static String[] sendTo = null;

      public boolean sendMail1() {

            return true;
      }

      public static void main(String args[]) throws Exception {
            StringBuffer msg = new StringBuffer();
            String emailMsgTxt = msg.append(" "
                                    "<html><body>Your html message or other message goes here </body></html>").toString();
            Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            new mailingPackage().sendSSLMessage(sendTo, emailSubjectTxt,emailMsgTxt, emailFromAddress);
            System.out.println("Sucessfully Sent mail to All Users");
      }

      public void sendSSLMessage(String recipients[], String subject,      String message, String from) throws MessagingException {
            boolean debug = true;

            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
            props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

            Session session = Session.getDefaultInstance(props,
                        new javax.mail.Authenticator() {

                              protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication("username for gmail", "password for gmail"); // enter your gmail username and password - or any other as per the smpt
                              }
                        });

            session.setDebug(debug);

            Message msg = new MimeMessage(session);
            InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);

            InternetAddress[] addressTo = new InternetAddress[recipients.length];
            for (int i = 0; i < recipients.length; i++) {
                  addressTo[i] = new InternetAddress(recipients[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);

            // Setting the Subject and Content Type
            msg.setSubject(subject);
            msg.setContent(message, "text/html");

            Transport.send(msg);
            System.out.println("Mail sent out Successfully");
      }

      // send mail to each person along with multiple attachments

      public void sendMailAttachments(String recipients, String subject, String message, String from, String filenames[]) throws MessagingException {
            
            System.out.println("Now in sendMailAttachments");
            
            boolean debug = true;

            Properties props = new Properties();
            props.put("mail.smtp.host", SMTP_HOST_NAME);
            props.put("mail.smtp.auth", "true");
      //      props.put("mail.debug", "true");
            props.put("mail.smtp.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.port", SMTP_PORT);
            props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
            props.put("mail.smtp.socketFactory.fallback", "false");

            Session session = Session.getDefaultInstance(props,
                        new javax.mail.Authenticator() {

                              protected PasswordAuthentication getPasswordAuthentication() {
                                    return new PasswordAuthentication("username for gmail", "password for gmail"); // enter your gmail username and password - or any other as per the smpt
                              }
                        });

            session.setDebug(debug);
            
             // Get a Transport object to send e-mail
        Transport bus = session.getTransport("smtp");

        // Connect only once here
        // Transport.send() disconnects after each send
        // Usually, no username and password is required for SMTP
        bus.connect();
        //bus.connect("smtpserver.yourisp.net", "username", "password");

       
        // Instantiate a message
        Message msg = new MimeMessage(session);
            
        //From Address
        InternetAddress addressFrom = new InternetAddress(from);
            msg.setFrom(addressFrom);
          
        // To Address
            String[] sendTo = { recipients };
            InternetAddress[] addressTo = new InternetAddress[sendTo.length];
            for (int i = 0; i < sendTo.length; i++) {
                  addressTo[i] = new InternetAddress(sendTo[i]);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
            
            // Sent Date
            msg.setSentDate(new Date());
            
            //set the subject of the mail
            msg.setSubject(subject);
          
            
            MimeBodyPart p1 = new MimeBodyPart();
            p1.setContent(message,"text/html");
            
            
            
          MimeBodyPart messageBodyPart=null;
          Multipart mp = new MimeMultipart();
      
          // attach the files in parts
          if(filenames!=null)
          for( int j = 0; j < filenames.length; j++ )
             {
                
                      
                    try{
                    messageBodyPart = new MimeBodyPart();
                    FileDataSource fileDataSource =new FileDataSource(filenames[j]);
                    messageBodyPart.setDataHandler(new DataHandler(fileDataSource));
                    messageBodyPart.setFileName(filenames[j].substring(filenames[j].lastIndexOf(File.separator)+1));
                    mp.addBodyPart(messageBodyPart);
                    }catch(Exception e){
                          e.printStackTrace();
                    }
                }
        
          // attach the body of the mail
          mp.addBodyPart(p1);
          msg.saveChanges();
          
          msg.setContent(mp);
          
          Transport.send(msg);
      
      }
      
}

----------------
Many things are customizable here... Now you need a  form and a servlet for sending multiple emails to many people at the same time.

Main method is the above class is - public void sendMailAttachments(String recipients, String subject, String message, String from, String filenames[])

All you got to do is when u click on a radio button...collect the names of the users user that button and grad them into a string of recipients seperted by commas. and call this method with its required attributes. That will solve your problem.

You can use a servlet for collecting the recipient addresses from the form.

_____Jags.
0
 
LVL 10

Accepted Solution

by:
jagadeesh_motamarri earned 500 total points
ID: 16943072
small correction -

Actually the above class contains two methods

1. send mail without attachmets - public void sendSSLMessage(String recipients[], String subject,     String message, String from)
2. Send mail with attachments - public void sendMailAttachments(String recipients, String subject, String message, String from, String filenames[])

____Jags
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
error email attachment VB2010 5 28
SEO friendly way to have two domains on website 9 80
Magento Indexing 1 71
Grunt No Clean Targets 6 59
When you work with shopping cart / ecommerce relates web sites, you need to pass the certain form post details to the payment gateway process page with required details for the products items you give to order. Also you may need to track the ordered…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now