Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sending Email using Java mail API in J2EE

 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to develop an application in java
which will send the text mail messages through SMTP intenet protocol.

Pls, anybody help me in doing that.
I have already tried javax.mail.* package API, but got runtime errors.

bye

Amit Pandit
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What errors are you getting? TellTheDetails

An extensive tutorial with many examples can be found at http://java.sun.com/developer/onlineTraining/JavaMail/contents.html

I'll move this to the Other Java APIs forum, where JavaMail is discussed.
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am giving my code sample over here : -

try {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "mail.google.com");
Session mailSession = Session.getDefaultInstance(props, null);
MimeMessage message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress("xyz@gmail.com"));
message.setSubject("Testing javamail plain");
message.setText("jhello");
message.setRecipient(Message.RecipientType.TO,new InternetAddress("pqr@gmail.com"));
Transport.send(message);
System.out.println("Completed");
}catch(Exception e){}

The output of the above code is ---> Completed
now without any errors

but mail not received at receipient....
code running without correct ouput

thanks

Amit Pandit
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I doubt that you can send email through Google's mail server without using any authentication. Lack of it should cause an exception, though. By the way, don't get in the habit of having empty catch clauses - exceptions need to be properly dealt with, and at the very least be logged somewhere.
 
Hongli Li
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you need to implement Authenticator in order to send mail with Gmail account.
When you create session, you need to use the two arguments Contructor method.
the properties should include <prop key="mail.smtps.auth">true</prop>.
 
Nishant Modak
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I will give you a sample code that you can use with GMAIL [using SMTP Authentication] to send emails.



Originally posted by amit pandit:
Hi,

I want to develop an application in java
which will send the text mail messages through SMTP intenet protocol.

Pls, anybody help me in doing that.
I have already tried javax.mail.* package API, but got runtime errors.

bye

Amit Pandit
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

I got error in above code ->

javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
 
amit pandit
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Finally I got a Solution

Here is my whole working code for sending email through gmail server (for reference)

public class SimpleMail
{
public static void main(String[] args) throws Exception
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "smtp.gmail.com");

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");

props.put("mail.debug", "true");
props.put("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");

Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("abc@gmail.com","pwd");
}
});
session.setDebug(true);
Transport transport = session.getTransport();
InternetAddress addressFrom = new InternetAddress("abc@gmail.com");
MimeMessage message = new MimeMessage(session);
message.setSender(addressFrom);

for(int j=0;j<20;j++)
{
message.setSubject("Testing javamail plain"+Math.random());
message.setContent("This is a test", "text/plain");
String sendTo [] = {"abc@gmail.com"};
if (sendTo != null) {
InternetAddress[] addressTo = new InternetAddress[sendTo.length];
for (int i = 0; i < sendTo.length; i++) {
addressTo[i] = new InternetAddress(sendTo[i]);
}
message.setRecipients(Message.RecipientType.TO, addressTo);

}
transport.connect();
transport.send(message);
transport.close();
//Transport.send(message);
System.out.println("DONE");
}
}
}

Thnks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic