send email on button click

hi guys

I am using Flex UI with java in the backend using spring Blazeds integration.

When i click on a flex button i need to submit an email with subject and some message body. If anyone has worked on such a requirment, I would greatly appreciate any help.

thanks.
royjaydAsked:
Who is Participating?
 
dgofmanCommented:
Yes, this is simple implementation I tested using my  Gmail account

import java.security.Security;
import java.util.Properties;

//http://www.oracle.com/technetwork/java/javamail/index.html
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class GoogleTest {

	private static final String SMTP_HOST_NAME = "smtp.gmail.com";
	private static final String SMTP_PORT = "465";
	private static final String emailMsgTxt = "Test Message Contents";
	private static final String emailSubjectTxt = "A test from gmail";
	private static final String emailFromAddress = "from_email@gmail.com";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	private static final String[] sendTo = { "to_email@yahoo.com" };

	public static void main(String args[]) throws Exception {

		Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

		new GoogleTest().sendSSLMessage(sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
	}

	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@gmail.com", "********");
					}
				});

		session.setDebug(debug);

		Message msg = new MimeMessage(session);
		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/plain");
		Transport.send(msg);
	}
}

Open in new window

0
 
dgofmanCommented:
Try this solution
SmtpSocket.as
0
 
dgofmanCommented:
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
royjaydAuthor Commented:
let me look..thx

I have a button
<mx:Button click= what function should i call ? />

also, can you tell me what connection information i need to send the mail. I am working in a small company, for testing i can use my yahoo email, but i think eventually i will need the mail server information from my company?

thanks for help.
0
 
dgofmanCommented:
private function send():void{
      var smtp:SmtpSocket = new SmtpSocket();
      var Host:String = 'smtp.yourcompanydomain.com';
      var Username:String ='mail@yourcompanydomain.com';
      var Password:String = 'xxxxxxx';  
      var frmLbl:String = 'SMTP Mailer <mail@yourcompanydomain.com>';

      smtp = new SmtpSocket()
      smtp.Host = Host;
      smtp.Username =Username;
      smtp.Password = Password;
      smtp.frmLbl =frmLbl;
      smtp.Port = 25;
      smtp.connect(smtp.Host,      smtp.Port );
      smtp.recepient = "mail@yourcompanydomain.com";
      smtp.QuickSubject = "My Test";
      smtp.msgBody = "Hello World";
}
0
 
royjaydAuthor Commented:
great!

so if my company email is jay.ronda@benziga.com (company domain is benziga.com)

 var smtp:SmtpSocket = new SmtpSocket();
      var Host:String = 'smtp.benziga.com';
      var Username:String ='jay.ronda@benziga.com';
      var Password:String = 'xxxxxxx';  --my password
      var frmLbl:String = 'SMTP Mailer <jay.ronda@benziga.com>';  --is this right ?
      smtp = new SmtpSocket()
      smtp.Host = Host;
      smtp.Username =Username;
      smtp.Password = Password;
      smtp.frmLbl =frmLbl;
      smtp.Port = 25;  -- will this change for my company?    
      smtp.connect(smtp.Host,      smtp.Port );
      smtp.recepient = "jackie.oliver@benziga.com";  
      smtp.QuickSubject = "My Test";
      smtp.msgBody = "Hello World";
0
 
dgofmanCommented:
smtp.Port = 25;  -- will this change for my company?    
It's standard SMTP port number but you have to confirm with your System Administrator what SMTP port are you using.
0
 
royjaydAuthor Commented:
hi
In smtpsocket.as , i see this line

else if (code == "235") { //Authentication successful  
this.writeUTFBytes ("MAIL FROM: <devaraj@wp.pl>\r\n");

do you know what should i substitute devaraj@wp.pl  with ?

as i was suspecting , i am getting some port error
>>>
Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: http://localhost:9081/technoweb/techflex/main.swf cannot load data from smtp.benziga.com:25.
      
0
 
dgofmanCommented:
I cannot share my code I gave you example what I used 1 year ago. May be you need to download latest latest SMTPMailer.

http://www.bytearray.org/wp-content/projects/smtpmailer/SMTPMailer%200.6.zip
0
 
dgofmanCommented:
0
 
dgofmanCommented:
0
 
royjaydAuthor Commented:
no prob.
I down loaded smtpmailer from http://www.bytearray.org/wp-content/projects/smtpmailer/SMTPMailer%200.6.zip 

i see lot of .as files. smtpmailer.as , smtpevent.as , ect
so do you mean i need to replace smtpsocket.as with smtpmailer.as ?

thanks
0
 
dgofmanCommented:
No you can get latest project from Google

http://code.google.com/p/smtpmailer/downloads/detail?name=SMTPMailer 0.9.zip

or

http://code.google.com/p/as3maillib/


And find example or documentation on the internet

When I start working on SMTP client I used  SmtpSocket.as what I attached in my first email its small and very basic, but I modified for my server and added additional API's what may exists already in Open Source project.
0
 
royjaydAuthor Commented:
ok , got it. thanks
I am gonna talk to my admin and find the right smtp port, most problly thats the issue.

thx.
0
 
dgofmanCommented:
No problem, check what protocol you are using, if IMAP it may be hard to find exisitng code.
0
 
royjaydAuthor Commented:
we are using SMTP only, but there is most probably a firewall which is blocking me.
thx for all your help.
0
 
dgofmanCommented:
You may need to use SSH or VPN in this case also verify if you are using SMTP\SSL connection.
0
 
Suraj_MathewCommented:
i have a very simple code to send mail to outside via SMTP server from JAVA.
Try it. One can even send HTML messages too....

SendMail.java  SendMailTest.java
0
 
royjaydAuthor Commented:
thx suraj.

dgofman, have a question. spoke to my admin, he says keep the port  empty , if doesnt work use the port 25 and use this server  NJWEBX2408.msad.benziga.com

I am trying without port first,so my new configuration is

var smtp:SmtpSocket = new SmtpSocket();
      var Host:String = 'smtp.NJWEBX2408.msad.benziga.com';
      var Username:String ='jay.ronda@benziga.com';
      var Password:String = 'xxxxxxx';  --my password
      var frmLbl:String = 'SMTP Mailer <jay.ronda@benziga.com>';  
      smtp = new SmtpSocket()
      smtp.Host = Host;
      smtp.Username =Username;
      smtp.Password = Password;
      smtp.frmLbl =frmLbl;
     // smtp.Port = 25;    //comment out
      smtp.connect(smtp.Host, smtp.Port );  //do you know what should this be?connect function //doesnt take one parameter      
      smtp.recepient = "jackie.oliver@benziga.com";  
      smtp.QuickSubject = "My Test";
      smtp.msgBody = "Hello World";

thanks
0
 
dgofmanCommented:
Sorry, is he junior admin?
You cannot connect to any host without port configuration.
Some are default for example when you are login to any web site you are using port 80
if you are login to bank application using HTTPS its port 443
SSH - 22
Telnet - 23
SMTP - 25
POP3 -110

So, I cannot understand his answer, ask somebody else.
0
 
royjaydAuthor Commented:
>>Sorry, is he junior admin?
heee, yes. i put the question through senior managment though and response received

you are trying to send email directly from flex to the SMTP server because of which browser is connecting directly to the SMTP server and is running into issues. Its better to send the email from the java, so you have client(flex) --> java (send email)


any thoughts?
thanks

0
 
dgofmanCommented:
Ok,
in this case you have two choices

1) Use some public SMTP server such as GMAIL
2) Using Suraj_Mathew files you can write java servlet what will except your request parameters from Flex and send via internal SMTP server.

Sorry,
David

0
 
royjaydAuthor Commented:
yeah.

suraj, in your actual Sendmail.java class is there a way to figure out if the mail has been sucessfully sent ?

if(email sucessfull){
do something
}
else{
do something else
}

is that possible?
right now i see Transport.send(simpleMessage); which is probalby sending the email but i want to check the sucess.

thanks guys.
0
 
royjaydAuthor Commented:
works , thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.