473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Java mail

karthickkuchanur
156 100+
Hi,

I am trying to send mail using java api.But i am getting exception.Please find the below code.
Expand|Select|Wrap|Line Numbers
  1. package com;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.mail.Message;
  6. import javax.mail.MessagingException;
  7. import javax.mail.Session;
  8. import javax.mail.Transport;
  9. import javax.mail.internet.AddressException;
  10. import javax.mail.internet.InternetAddress;
  11. import javax.mail.internet.MimeMessage;
  12. import javax.mail.internet.MimeMessage.RecipientType;
  13.  
  14. public class SendMail
  15. {
  16.   private String from;
  17.   private String to;
  18.   private String subject;
  19.   private String text;
  20.   public SendMail(String from, String to, String subject, String text)
  21.   {
  22.     this.from = from;
  23.     this.to = to;
  24.     this.subject = subject;
  25.     this.text = text;
  26.   }
  27.   public void send()
  28.   {
  29.     Properties props = new Properties();
  30.     props.put("mail.smtp.host", "smtp.gmail.com");
  31.     props.put("mail.smtp.port", "9000");
  32.     Session mailSession = Session.getDefaultInstance(props);
  33.     Message simpleMessage = new MimeMessage(mailSession);
  34.     InternetAddress fromAddress = null;
  35.     InternetAddress toAddress = null;
  36.     try
  37.     {
  38.       fromAddress = new InternetAddress(from);
  39.       toAddress = new InternetAddress(to);
  40.     }
  41.     catch (AddressException e)
  42.     {
  43.       // TODO Auto-generated catch block
  44.       e.printStackTrace();
  45.     }
  46.     try
  47.     {
  48.       simpleMessage.setFrom(fromAddress);
  49.       simpleMessage.setRecipient(RecipientType.TO, toAddress);
  50.       simpleMessage.setSubject(subject);
  51.       simpleMessage.setText(text);
  52.       Transport.send(simpleMessage);
  53.       System.out.println("Hi");
  54.     }
  55.     catch (MessagingException e)
  56.     {
  57.       // TODO Auto-generated catch block
  58.       e.printStackTrace();
  59.     }
  60.   }
  61.   public static void main(String[] args)
  62.   {
  63.     SendMail mail = new SendMail("nk.karthick@gmail.com", "karthickrajkumar.n@cognizant.com",
  64.         "TEst", "fsdf");
  65.     mail.send();
  66.   }
  67. }
  68.  
Error :

javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 9000;
nested exception is:
java.net.ConnectException: Connection refused: connect
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.SendMail.send(SendMail.java:52)
at com.SendMail.main(SendMail.java:65)
Mar 19 '13 #1
1 2542
r035198x
13,262 8TB
It says it can't connect to smtp.gmail.com, port: 9000; so confirm what the right port is and try it. Most documents say you should use 465 as the port.
Mar 19 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hal Vaughan | last post by:
I'm working with javax.mail.*. I have no problem with reading in messages. I'm not using multi-part messages or anything, I just use this setup: Session oSession =...
2
by: vigupta12 | last post by:
Hi List, I am using java mail API. I have to embed an image with my html content. Here is the sample code that I am using for this. MimeMultipart multipart = new...
1
dmjpro
by: dmjpro | last post by:
i am newbei in java mail program .... recently i came to know that without SMTP specification of a server i can't run the java mail program succcfully ..... is that true ..... if possible plz...
3
dmjpro
by: dmjpro | last post by:
i have been trying to do mail programming in java.... al in vain one thing still irritating that SMTP ..... the server must have the SMTP service provider. how can i know that the server is...
3
dmjpro
by: dmjpro | last post by:
i have JDK 1.2 and which version of JAVA-MAIL and JAF ll be compatible??? plz help. kind regards. dmjpro.
0
jayam
by: jayam | last post by:
Hi I m developing a software for sending and receiving the mail using the java mail api . when i receive the mail the sender name will be display like this...
1
by: sugard | last post by:
This time my task is to send a real message to the specified email address. The signatures for my method is void sendEmail (EmailAddress emailAddress, String message) I have gone through a...
1
dmjpro
by: dmjpro | last post by:
I am using SMTP mail server. I am sending a mail from SMTP client using Java Mail. But now i am getting this exception... Relaying denied. IP name possibly forged The debug informations given...
3
pradeepjain
by: pradeepjain | last post by:
Hii, I want to send a html mail with java variables in middle using java.is there a best example to show it. example name: java variable having name item and so on Thanks, ...
2
by: popprem | last post by:
Hi, I'm trying to send mail from our application using java mail api. There is a .css file which i need to use for it. How can i do it????? The problems i faced are : I tried to get the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.