• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with javamail  RSS feed

 
kishore nerella
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Check that program of javamail
Its giving some nestedexception.
Please check that and give me correct reason and solution.


import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class javamail_send extends Object
{
public static void main(String args[])
{
String smtpServer=null;
String toEmail=null;
String fromEmail=null;
String body=null;

//parse the command line parameters
for(int x=0;x<args.length-1;x++)
{
if(args[x].equalsIgnoreCase("-S"))
{
smtpServer=args[x+1];
}
else if(args[x].equalsIgnoreCase("-T"))
{
toEmail=args[x+1];
}
else if(args[x].equalsIgnoreCase("-F"))
{
fromEmail=args[x+1];
}
else if(args[x].equalsIgnoreCase("-B"))
{
body=args[x+1];
}
}

if(smtpServer==null||toEmail==null||fromEmail==null||body==null)
{
System.out.println("usage: javamail_send -S <server> -T <toemail> -F <from> -B <body>");
System.exit(1);
}

try
{
//set up the default parameters
Properties props=new Properties();
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.host",smtpServer);
props.put("mail.smtp.port","25");

//create the session and create a new mail message
Session mailSession=Session.getInstance(props);
Message msg=new MimeMessage(mailSession);

//set the From,to,Date,and subject fields
msg.setFrom(new InternetAddress(fromEmail));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(toEmail));
msg.setSentDate(new Date());
msg.setSubject("Test Mail");

//create the body of the mail
msg.setText(body);

Transport.send(msg);

msg.writeTo(System.out);
}
catch (Exception e)
{
System.out.println(e);
}
}
};
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, your first step should be to read the exception. You haven't posted it here so that makes it a bit hard for anybody else to suggest what you should do about it.

You will also get better information about the exception if you useinstead of what you have,And if you still are having a problem in understanding what you see there, then don't hesitate to post the stack trace here.
 
kishore nerella
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please send me the exact code for javamail

If possible try to send javamail with attachments and reading mail also.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our JavaRanch FAQ page has many links for articles on JavaMail some of them even have code examples. Try them out.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the JavaMail download itself contains examples of all the things you asked for examples of. (In the "demo" directory if I recall correctly.)

But you should be aware that the environment you run JavaMail code in can cause exceptions that cannot be debugged just by looking at the code. That's why I asked for information about the exception you were having.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!