File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Other JSE/JEE APIs and the fly likes how to send mail using gmail server Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "how to send mail using gmail server" Watch "how to send mail using gmail server" New topic
Author

how to send mail using gmail server

ravi krishna muthireddy
Greenhorn

Joined: Apr 02, 2007
Posts: 9
Hello i am using gmail server for sending mail i dont have any server
getting javax.mail.NoSuchProviderException: smtp exception

here is my code
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class sendMail {
public static void main (String args[])
throws Exception {
String host = "smtp.gmail.com";
String from = "mravikrish@gmail.com";
String to = "mravikrish@yahoo.com";
String fileAttachment = "hello";
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Hello JavaMail Attachment");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Hi");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source =new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send( message );
}
}
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
Not sure what's wrong with the code, but you will definitely need to add authentication code (i.e., a password handler). No remote email server will let you connect these days without that.


Ping & DNS - my free Android networking tools app
ravi krishna muthireddy
Greenhorn

Joined: Apr 02, 2007
Posts: 9
i am using netbeans 6.1 activation.jar , mail.jar
i have tried another code using jauthencticate

package com.esd.Login;

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import javax.servlet.http.HttpServletRequest;

public class sendMail1{

public void send(HttpServletRequest req)
{
try
{
String host = "smtp.mail.com";
String from = "mymail@gmail.com";
String to = "mymail@yahoo.com";
System.out.println("Host ="+host);
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
JAuthenticate pAuth = new JAuthenticate("mymail@gmail.com", "mypwd");
Session session = Session.getInstance(props, pAuth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(" Multiple Attachments ");
MimeBodyPart messageBodyPart = new MimeBodyPart();
String mssg="Multiple Attachments Test Mail";
messageBodyPart.setText(mssg);
System.out.println(" Before senidng message to mail");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send( message );
System.out.println(" After senidng message to mail");

}
catch(Exception e)
{
e.printStackTrace();
}
}
}


for this code iam getting the following exception

javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.com, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1213)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:311)
at javax.mail.Service.connect(Service.java:255)
at javax.mail.Service.connect(Service.java:134)
at javax.mail.Service.connect(Service.java:86)
at com.sun.mail.smtp.SMTPTransport.connect(SMTPTransport.java:144)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at com.esd.Login.sendMail1.send(sendMail1.java:35)
at com.esd.Login.ServletMail.doGet(ServletMail.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:390)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:595)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41601
    
  55
javax.mail.MessagingException: Could not connect to SMTP host: smtp.mail.com, port: 25;

Notice something wrong?
Adinath Shirsath
Ranch Hand

Joined: Dec 04, 2007
Posts: 35
are you proxy server?
if yes try to set proxy in your code ....
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
How is this question related to JSF?
pradeep singh
Ranch Hand

Joined: Oct 23, 2007
Posts: 339
Hi
Add this line:
props.put("mail.smtp.port", "465");
Then it will work fine,if not then add authentication code.

My program is working fine .
Below is my code:


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

public class sms2
{
String d_email = "xxxx@gmail.com",
d_password = "xxxxx",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "xxxxx@gmail.com",
m_subject = "Testing",
m_text = "Hey, this is the testing email.";

public sms2()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}

public static void main(String[] args)
{
sms2 blah = new sms2();
}

private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}


SCJP 5.0(75%), SCWCD 5.0(88%)
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Follow this link


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
neelima neelima
Greenhorn

Joined: Sep 21, 2009
Posts: 3
hi
i had run this program but got the following exception







pradeep singh wrote:Hi
Add this line:
props.put("mail.smtp.port", "465");
Then it will work fine,if not then add authentication code.

My program is working fine .
Below is my code:


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

public class sms2
{
String d_email = "xxxx@gmail.com",
d_password = "xxxxx",
d_host = "smtp.gmail.com",
d_port = "465",
m_to = "xxxxx@gmail.com",
m_subject = "Testing",
m_text = "Hey, this is the testing email.";

public sms2()
{
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

SecurityManager security = System.getSecurityManager();

try
{
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
//session.setDebug(true);

MimeMessage msg = new MimeMessage(session);
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));
Transport.send(msg);
}
catch (Exception mex)
{
mex.printStackTrace();
}
}

public static void main(String[] args)
{
sms2 blah = new sms2();
}

private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(d_email, d_password);
}
}
}
neelima neelima
Greenhorn

Joined: Sep 21, 2009
Posts: 3
neelima neelima wrote:hi
i had run this program but got the following exception
javax.mail.MessagingException: Unknown SMTP host: smtp.gmail.com;
nested exception is:
java.net.UnknownHostException: smtp.gmail.com
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1543)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:453)
at javax.mail.Service.connect(Service.java:313)
at javax.mail.Service.connect(Service.java:172)
at javax.mail.Service.connect(Service.java:121)
at javax.mail.Transport.send0(Transport.java:190)
at javax.mail.Transport.send(Transport.java:120)
at mail.Msg.<init>(Msg.java:47)
at mail.Msg.main(Msg.java:57)
Caused by: java.net.UnknownHostException: smtp.gmail.com
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:177)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:550)
at com.sun.net.ssl.internal.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:141)
at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:267)
at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:201)
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1511)
... 8 more
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

your code is not working.... got following exception



No Kaustubh No Fun, Know Kaustubh Know Fun..
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19680
    
  18

Then check your network settings, because I can certainly resolve (and reach) smtp.gmail.com.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

Please provide me idea to check it...
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

Kaustubh G Sharma wrote:Please provide me idea to check it...


I would first google for "debug network issues" and then take it from there.

[My Blog] [JavaRanch Journal]
Kaustubh G Sharma
Ranch Hand

Joined: May 13, 2010
Posts: 1270

ohk thanks jaikiran
 
jQuery in Action, 2nd edition
 
subject: how to send mail using gmail server