3.2.0

java mail api - can anyone help me out ?

hi.....
I have a program on mail api.But I donot know how to use it for my program.
I have a form to be filled by an employee.Once the employee clicks the submit button, the form should be sent to his functional head.
but here, I do not want the employee to fill in the "mail to" field. the email id of functional head is stored in the database. so, it must be retrieved from the database.

Here is my program................

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;

/*
To use this program, change values for the following three constants,

SMTP_HOST_NAME -- Has your SMTP Host Name
SMTP_AUTH_USER -- Has your SMTP Authentication UserName
SMTP_AUTH_PWD -- Has your SMTP Authentication Password

Next change values for fields

emailMsgTxt -- Message Text for the Email
emailSubjectTxt -- Subject for email
emailFromAddress -- Email Address whose name will appears as "from" address

Next change value for "emailList".
This String array has List of all Email Addresses to Email Email needs to be sent to.


Next to run the program, execute it as follows,

SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();

*/

public class SendMailUsingAuthentication
{

private static final String SMTP_HOST_NAME = "myserver.smtphost.com";
private static final String SMTP_AUTH_USER = "myusername";
private static final String SMTP_AUTH_PWD = "mypwd";

private static final String emailMsgTxt = "Online Confirmation Message. Also include the Tracking Number.";
private static final String emailSubjectTxt = " Confirmation Subject";
private static final String emailFromAddress = "yyy@yahoo.com";

// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"xxx@yahoo.com", "yyy@yahoo.com"};

public static void main(String args[]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}

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

//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");

Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);

session.setDebug(debug);

// create a message
Message msg = new MimeMessage(session);

// set the from and to address
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/plain");
Transport.send(msg);
}


/**

vasudha
February 8,
here also same one prbm...pls frwrd me aftr getting tat solution
Ashok.S
February 8,
vasudha,
here its very urgent,...
pls try to come up my clarification.if u knw html , java script and OLE concept..pls contact me ...if u dnt mind.
my id is ashok@heartmail.com
Ashok.S
February 8,
www.google.com
Pc (from Brazil)
February 9,

This topic is archived.

See also:


Back to support forum