Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

Failed to send mail from Java Application

Thread view: 
joshanjoe@gmail.com - 20 Jun 2006 07:19 GMT
Hi,

I need to send email from my java application into internet
server(gmail or yahoo. etc). I tried with gmail smtp but they gives
some time out. please send code related into that .

by
Joshan
joshanjoe@gmail.com
ickz - 20 Jun 2006 13:59 GMT
Checkout :
http://www.javacommerce.com/destination65673/18274/SendMailUsingAuthentication.java

That has all the code you need. Then you just need to be sure you have
a SMTP server you can use. The server may require authentication, but
that class includes how to use a username/pass.

If you have problems, it may also be worth trying the IP of the server
rather than its host name.
IchBin - 21 Jun 2006 00:56 GMT
> Checkout :
> http://www.javacommerce.com/destination65673/18274/SendMailUsingAuthentication.java
[quoted text clipped - 5 lines]
> If you have problems, it may also be worth trying the IP of the server
> rather than its host name.

Seems that all of the classes are not available for this
SendMailUsingAuthentication program.

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor,  Regular Guy (1952-)
ickz - 21 Jun 2006 09:20 GMT
Ok well here is a better example then:

=================================================

import javax.mail.*;
import javax.mail.internet.*;

import java.util.*;

/**
* Sends an email
* @author ****
*
*/
public final class EmailSender
{

  /**
   * Initialize
   * @param theUser The user the mail is from (e.g. admin)
   * @param theSMTPHost The SMTP server
   * @param theFromAddr The email address the mail is sent from (e.g.
admin@mydomain.com)
   * @throws Exception if problem encountered
   */
   public EmailSender(String theUser, String theSMTPHost, String
theFromAddr) throws Exception
   {

      //Set the host smtp address
      Properties theProperties = new Properties();

      theProperties.put("mail.smtp.user", theUser);
      theProperties.put("mail.smtp.host", theSMTPHost);
      try
      {
         myFromAddr = new InternetAddress(theFromAddr);
      }
      catch(AddressException e)
      {
         throw new Exception("Failed to obtain sender address");
      }

       mySession = Session.getDefaultInstance(theProperties, null);
   }

   /**
    * Sends an Email
    * @param theRecipients the recipients
    * @param theSubject the email subject
    * @param theMessage the email message
    * @throws MessagingException on exception
    */
   public void sendMail( String[] theRecipients, String theSubject,
String theMessage) throws MessagingException
   {
      if(theRecipients.length>0)
      {
          // create a message
          Message msg = new MimeMessage(mySession);

          // set the from and to address
          msg.setFrom(myFromAddr);

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

          // Setting the Subject and Content Type
          msg.setSubject(theSubject);
          msg.setContent(theMessage, "text/plain");
          Transport.send(msg);
      }
      //else nothing to send
   }

   /** Session **/
   private Session mySession;
   /** the from address */
   private InternetAddress myFromAddr;

}
=============================================================

You will need the java mail package (
http://java.sun.com/products/javamail/ )

The class can be ran easily, something like:

            EmailSender myEmailSender = new EmailSender("admin",
"127.0.0.1", "admin@somewhere.com");
       //Email body
            String body="This is an example email from java.";
            String[] theRecipients = {"myemail@somewhere.com"};
            myEmailSender.sendMail(theRecipients, "My Email Test",
body);

So it is just a case of having a SMTP server you can use really.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.