my dog learned polymorphism*
The moose likes Other JSE/JEE APIs and the fly likes Sending Email using Java mail API in J2EE 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 "Sending Email using Java mail API in J2EE" Watch "Sending Email using Java mail API in J2EE" New topic
Author

Sending Email using Java mail API in J2EE

amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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.


Ping & DNS - updated with new look and Ping home screen widget
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
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
Marshal

Joined: Mar 22, 2005
Posts: 39549
    
  27
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

Joined: Oct 29, 2006
Posts: 124
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>.


Do you know why this cup is useful? Because it is empty.
Nishant Modak
Greenhorn

Joined: Sep 29, 2006
Posts: 27
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

Joined: Mar 01, 2007
Posts: 23
Thanks,

I got error in above code ->

javax.mail.NoSuchProviderException: Unable to locate provider for protocol: smtp
amit pandit
Greenhorn

Joined: Mar 01, 2007
Posts: 23
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sending Email using Java mail API in J2EE
 
Similar Threads
Automated mail from the recepient
Java Mail
Email
providing email functionality using struts
Help me to download java Mail API (urgent)