Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Failed to send mail from Java Application

Reply
Thread Tools

Failed to send mail from Java Application

 
 
joshanjoe@gmail.com
Guest
Posts: n/a
 
      06-20-2006
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


 
Reply With Quote
 
 
 
 
ickz
Guest
Posts: n/a
 
      06-20-2006
Checkout :
http://www.javacommerce.com/destinat...ntication.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.

 
Reply With Quote
 
 
 
 
IchBin
Guest
Posts: n/a
 
      06-20-2006
ickz wrote:
> Checkout :
> http://www.javacommerce.com/destinat...ntication.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.
>


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-)
 
Reply With Quote
 
ickz
Guest
Posts: n/a
 
      06-21-2006
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.
)
* @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", "");
//Email body
String body="This is an example email from java.";
String[] theRecipients = {""};
myEmailSender.sendMail(theRecipients, "My Email Test",
body);



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deploying application in domain failed; WSGEN FAILED in Netbeans6.1with Glassfish kiran Java 5 07-09-2008 09:17 PM
Syntax for System.Net.Mail send mail Mike P ASP .Net 1 03-01-2007 06:58 PM
Send a mail with System.Net.Mail.SmtpClient refv8 ASP .Net 2 10-13-2006 07:28 PM
Send mail using System.Web.Mail fails w/ m.BodyFormat=MailFormat.H =?Utf-8?B?Unlhbg==?= ASP .Net 2 02-28-2006 01:50 AM
Mail.SmtpMail.Send won't send to one of my addresses Nathan Sokalski ASP .Net 3 11-23-2005 09:25 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57