Go Premium for a chance to win a PS4. Enter to Win

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

Emailing form based on selected options

What is the best way to email a form based on a selected radio button in Frontpage?
0
ITHelper80
Asked:
ITHelper80
  • 5
  • 3
1 Solution
 
jagadeesh_motamarriCommented:
....u mean send email from the data that is collected from the form using radio button.

Can u please explain more clearly

____Jags
0
 
ITHelper80Author Commented:
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
 
jagadeesh_motamarriCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ITHelper80Author Commented:
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
 
jagadeesh_motamarriCommented:
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
 
gugfuzCommented:
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
 
ITHelper80Author Commented:
I need it to be invisible to the user.
0
 
jagadeesh_motamarriCommented:
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
 
jagadeesh_motamarriCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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