Two Laptop Bag*
The moose likes EJB and other Java EE Technologies and the fly likes java mail Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "java mail " Watch "java mail " New topic
Author

java mail

ramchander yshetti
Ranch Hand

Joined: Apr 22, 2006
Posts: 97
hai
this is my java mail program it is compiling successfuly but runtime exception is coming.
please clear me.


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

public class SimpleSendMessage
{
public static void main(String[] args)
{
String host="gmail.com";
String to="ys_ramchander@yahoo.co.in";
String from="y.ramchander@gmail.com";

String subject=" THUSHARA PG COLLEGE..";

String messageText=" i am sending a message using the JAVA API \n";

boolean sessionDebug=false;

Properties prop=System.getProperties();
prop.put("mail.host",host);
prop.put("mail.transport.protocol","smtp");

Session session=Session.getDefaultInstance(prop,null);

session.setDebug(sessionDebug);

try
{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));

InternetAddress []address={new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);

Transport.send(msg);
}
catch(MessagingException e)
{
e.printStackTrace();
}

System.out.println("yes...............................!");
}
}
------------------------
javax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Could not connect to SMTP host: gmail.com
, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at javax.mail.Transport.send0(Transport.java:219)
at javax.mail.Transport.send(Transport.java:81)
at SimpleSendMessage.main(SimpleSendMessage.java:40)

---------------
i connected to net but why this problem is coming please clear me
i will be thankfull to you.
bye
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41109
    
  45
I doubt that gmail.com is running a mail server; it's probably smtp.gmail.com or something similar.

You'll also need to use authentication. Check this JavaMail introduction for how that works.


Ping & DNS - my free Android networking tools app
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3703
    
    5

smtp.gmail.com does indeed work, although you have to check which port to use as well as providing your full g-mail login info for authentication.


My Blog: Down Home Country Coding with Scott Selikoff
kaan h.
Greenhorn

Joined: Apr 06, 2008
Posts: 5
Sending mail from a Java application is so easy.
1.first download mail API from SUN's website (mail_api.rar)
2.Include mail.jar and activation.jar to your IDE's libraries
3.Use following code to send email(example configured for gmail)
enjoy...

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.security.Security;
import java.util.Properties;

public class GoogleMailSender {

private static final String
SMTP_HOST_NAME = "smtp.gmail.com";
private static final String
SMTP_PORT = "465";
private static final String
emailMsgTxt = " test mail sent";
private static final String
emailSubjectTxt = "Javaaaaaaaaaa";
private static final String
// mailin kimden gittiğini g�steren adres
emailFromAddress = "from@address.com";
private static final String
SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
private static final String[]
// mail g�ndermek istediğimiz adresler
sendTo = {"to@address.com"};

public void sendSSLMessage(String recipients[], String subject,
String message, String from)
throws MessagingException {
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(){
// buraya gmail mail adresinizi ve sifrenizi girmelisiniz.
return new PasswordAuthentication("username@gmail.com", "password here");
}
});

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

msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}




public static void main(String args[]) throws Exception {
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
new GoogleMailSender().sendSSLMessage(
sendTo, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}//end main





}//end class
 
Consider Paul's rocket mass heater.
 
subject: java mail
 
Similar Threads
JSP using javamail
Java Mail Send Failed error
i have given code.Pl find the error
please clear me
urgent help in java mail