Solved

Emailing form based on selected options

Posted on 2006-06-12
11
190 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to setup a password to enter my web page? 3 117
Problem to Popup 2 86
Randomize in Owl Carousel v1.3.2 6 70
Where does legacy ASP.NET initialize language? 2 27
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

821 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