Problem using Javax.Mail API
Hi Experts,
I am using Javax.mail API to send an email through java application. But i am facing an issue when i am deploying the java application in SAP WAS.
I have assigned values in SMTP Host ,Port, and InternetAddress and deployed to WAS. But when i deploy this java class again in WAS with changing the values for SMTP Host ,Port and InternetAddress and putting in Properties object ,its always picking the values set at first time.
Can anyone tell me whats wrong with following code .
Properties prpt = new Properties();
if (host == null)
host=token[0];
prpt.put("mail.smtp.host",host);
prpt.put("mail.smtp.sendpartial", "true");
if (port == null)
port=token[1];
prpt.put("mail.smtp.port", port);
Session session1=Session.getDefaultInstance(prpt, null);
MimeMessage message=new MimeMessage(session1);
message.setFrom(new InternetAddress(emailId"));
Thanks in advance.
Hi
I hope follow similar answered thread will help you.
1. [Javamail Client Service |Javamail Client Service;
2.[Sending Email |Sending Email;
Best Regard
Satish Kumar
Similar Messages
-
Problem using Java Mail API with WLS 7.0
Hi All,
I am trying to use the Java Mail API provided by WLS 7.0. I have made the
settings metioned in the WLS 7.0 docs. However when I try to run the program I
am getting the following error:
javax.naming.NoInitialContextException: Need to specify class name in environment
or system property, or as an applet parameter, or in an application resource file:
java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
46)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.jav
a:283)
at javax.naming.InitialContext.lookup(InitialContext.java:350)
The code that I have written is as follows
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
public class MailTo {
public static void main(String args[])
try
//Context ic = getInitialContext();
InitialContext ic = new InitialContext();
/* My jndi name is "testSession" */
Session session = (Session) ic.lookup("testSession"); /* THE PROBLEM IS SHOWN
IN THIS LINE */
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "XX.XX.XX.XX");
/* For security reasons I have written the ip add in this format */
props.put("mail.from", "[email protected]"); /* for security reasons i have
changed the mail address */
Session session2 = session.getInstance(props);
Message msg = new MimeMessage(session2);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]",
false));
msg.setSubject("Test Message");
msg.setSentDate(new Date());
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText("This is a mail sent to you using JAVA Mail API and Weblogic Server");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);
Transport.send(msg);
catch(Exception e)
e.printStackTrace();
}//end of main
public static Context getInitialContext()
throws NamingException
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7501/testWebApp");
p.put(Context.SECURITY_PRINCIPAL, "weblogic");
p.put(Context.SECURITY_CREDENTIALS, "weblogic");
return new InitialContext(p);
}//end of class
Can anyone please tell me what is the problem. I thought that we cannot directly
do
InitialContext ic = new InitialContext();
So I had written a method getInitialContext() as shown in the above piece of code,
but that too did not work.
Eagerly awaiting a response.
Jimmy ShahYou can use InitialContext ic = new InitialContext() only if you are using a startup class, servlet or a JSP i.e
server side code.
If you are using a java client you need to use Context ic = getInitialContext();
Try this code
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
public class MailTo {
public static void main(String args[])
try {
Properties h = new Properties();
h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");
Context ic = new InitialContext(h);
Session session = (Session) ic.lookup("testSession");
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "XX.XX.XX.XX");
props.put("mail.from", "[email protected]");
Session session2 = session.getInstance(props);
Message msg = new MimeMessage(session2);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]",false));
msg.setSubject("Test Message");
msg.setSentDate(new Date());
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText("This is a mail sent to you using JAVA Mail API and Weblogic Server");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);
Transport.send(msg);
catch(Exception e)
e.printStackTrace();
}//end of main
}//end of class
We have shipped a javamail example in the samples\server\src\examples\javamail folder.
Jimmy Shah wrote:
Hi All,
I am trying to use the Java Mail API provided by WLS 7.0. I have made the
settings metioned in the WLS 7.0 docs. However when I try to run the program I
am getting the following error:
javax.naming.NoInitialContextException: Need to specify class name in environment
or system property, or as an applet parameter, or in an application resource file:
java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:6
46)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:246
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.jav
a:283)
at javax.naming.InitialContext.lookup(InitialContext.java:350)
The code that I have written is as follows
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;
import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
public class MailTo {
public static void main(String args[])
try
//Context ic = getInitialContext();
InitialContext ic = new InitialContext();
/* My jndi name is "testSession" */
Session session = (Session) ic.lookup("testSession"); /* THE PROBLEM IS SHOWN
IN THIS LINE */
Properties props = new Properties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.host", "XX.XX.XX.XX");
/* For security reasons I have written the ip add in this format */
props.put("mail.from", "[email protected]"); /* for security reasons i have
changed the mail address */
Session session2 = session.getInstance(props);
Message msg = new MimeMessage(session2);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse("[email protected]",
false));
msg.setSubject("Test Message");
msg.setSentDate(new Date());
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText("This is a mail sent to you using JAVA Mail API and Weblogic Server");
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);
Transport.send(msg);
catch(Exception e)
e.printStackTrace();
}//end of main
public static Context getInitialContext()
throws NamingException
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
p.put(Context.PROVIDER_URL, "t3://localhost:7501/testWebApp");
p.put(Context.SECURITY_PRINCIPAL, "weblogic");
p.put(Context.SECURITY_CREDENTIALS, "weblogic");
return new InitialContext(p);
}//end of class
Can anyone please tell me what is the problem. I thought that we cannot directly
do
InitialContext ic = new InitialContext();
So I had written a method getInitialContext() as shown in the above piece of code,
but that too did not work.
Eagerly awaiting a response.
Jimmy Shah--
Rajesh Mirchandani
Developer Relations Engineer
BEA Support -
Setting Importance Level for a email message using javax.mail.* API
Setting Importance Level for a email message using javax.mail.* API
From what I understand we can set Flag on Email Message. How can we set Importance Leve: High/Low for an email message?
Thanks
PurviMost of the message Flags work only for IMAP mailboxes. POP3 supports only the DELETED flag. It must be understood that Javamail is a framework which provides all the features available in a standard mailing system. But whether or not a particular feature works is a functionality of the particular implementation being used.
Thus for example POP3 cannot differentiate read from unread messages in a mailbox though Javamail provides that feature. -
Using Java Mail API from Tomcat
Hello,
Purely as an academic exercise I have written a JSP page which, upon being requested from the client's browser, should send me a default email using Java Mail Api.
here is the code :
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class TestMail {
static String msgText1 = "success this time 12";
static String msgText2 = "This is the text in the message attachment.";
public String sendIt() {
String to = "<my email";
String from = "<anything>";
String host = "<my ip address of smtp server>";
boolean debug = false;
Properties props = new Properties();
props.put("mail.smtp.host", host);
Session session = Session.getInstance(props, null);
.....The code works fine as a stand alone app but when called from JSP page it hangs on the Session.getInstance line. I can only guess that this might be a security issue with the container not allowing access to the smtp server ?
Can anyone give me a clue ???Your Tomcat log files should spell out the problem for you.
My Tomcat installation does not come with the Java Mail API. I had to add the mail and activation jar files to the server/common.lib directory (or the server's shared/lib or the WEB-INF/lib of your application.)
HTH. -
Error using javax.mail package
Hi,
I have included the package javax.mail.* in my code.I have downloaded
jaf 1.0.2 and javamail 1.1.3 from the Sun Microsystems website.
When i try to compile my code i get the error message:
com/voxspectrum/ccvox/EmailServer.java:55: cannot resolve symbol
symbol : class MimeMessage
location: class com.voxspectrum.ccvox.EmailServer
javax.mail.Message msg = new MimeMessage(session);
I am using JDK ver 1.3.1_02. What should i set my path and classpath as, if i am using javax.mail package?
Please could you help me out with this problem!Hi Nisha
You need to include the path to the mail.jar and activation.jar files in your classpath.
How you do it depends on the OS you are running on
in Windows 95 /98 the easiest way is to edit the autoexec.bat file and add the settings to your already existing classpath statement i.e.
set CLASSPATH=%CLASSPATH%;C:\richard\javamail-1.3\mail.jar C:\richard\jaf-1.0.2\activation.jar
(excuse the word wrapping) then reboot
on Linux / Solaris edit the .profile file for your user and set the CLASSPATH variable to
CLASSPATH=$CLASSPATH:<pathtofile>/mail.jar:<pathtofile>/activation.jar
export CLASSPATH
and that should do it
Hope this helps -
Using Java mail API from JSPDynPage
Hi Experts,
I am working on a Portal Assignment that requiresto sent work flow mails on the basis of error conditions.
Can u please suggest if at all I can use Java Mail APIs from JSP page within the JSP DYN Page Framework.
If at all Java Mail can be used could u please suugest some help docs on the same.
Thanks for the help.
Manab C Ghosh
EP Consultant
Kolkata INDIA
+919830603327Hi Experts,
Thanks for all the responses to my Mail question(mailing from JSPDynPage).
I have found the solution.
Here is how I have got the things: (pls note there are other solns)
Using Java Mail APIs;
Create a Java file in the scr.core / src.api
MailSender.java
* Created on Jul 21, 2005
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
package com.mailsend.test;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
* Edited on Jul 24, 2005
* @author Manab C Ghosh
public class MailSender {
public String sendMessage(){
String msg ="Hello mail Test";
String smtpServer ="mySMTPServer";
String smtpSender = "senderemailaddress";
String smtpRecipient="receipientemailaddress";
String stBody = msg ;
//String stDate = new Date().toString() ;
String stSubject = "Mail Test ";
Send( smtpServer, //SMTPServer
smtpSender, //Sender
smtpRecipient, //Recipient
stSubject, //Subject
stBody //Body
); //Attachments
return "Mail Success";
public static void Send(String SMTPServer,
String From,
String To,
String Subject,
String msgText1
// Error status;
int ErrorStatus = 0;
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", SMTPServer);
Session session = Session.getDefaultInstance(props, null);
try {
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(From));
InternetAddress[] address = {new InternetAddress(To)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(Subject);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
//mp.addBodyPart(mbp2);
// add the Multipart to the message
msg.setContent(mp);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport.send(msg);
} catch (MessagingException mex) {
Call this file from the JSP page (which is set at JSPDynPage controller)
one important thing-----
Create a dir under PORTAL-INF and import the following jars-- activation.jar, mail.jar,imap.jar,smtp.jar, mailapi.jar.
This works..
Thanks once again to the Experts.
happy mailing
Manab Ghosh.
INDIA (+919830603327) -
How to use Java Mail API in Unix
I am trying to write some code using Java mail API. I want to execute it in Unix. I downloaded the mail API to windows machine and ftped the mail.jar file to a Unix machine. Then I set the class path as below:
export CLASSPATH=$CLASSPATH:/home.../mail.jar
Then I tried to compile my Java program. The output is as below:
error: error reading /home.../mail.jar; invalid END header (bad central directory offset)
mail.java:1: package javax.mail does not exist
import javax.mail.*;
Can any one please help me out.You should also include the "activation.jar" file that you obtained from
downloading the Java Activation Framework, in your CLASSPATH.
For example:
export CLASSPATH=$CLASSPATH:/urPath/activation/activation.jar
Besides, assuming you unzipped javamail-1_4_1.zip in home/download the following should work
export CLASSPATH=$CLASSPATH:home/download/javamail-1.4.1/mail.jar:. -
How can i access gmail's smtp server using java mail api
i m using java mail api to access gmails pop and smtp service to receive and send mail from ur gmail account. I m able to access gmails pop server using the ssl and port 995 , but i can not use its smtp server to which i m connecting using ssl on 465 port. It requires authentication code.
if anybody can help me in this regard i m thnkful to him/her.
thnks in advance.
jogin desaiHere's an example of using SSL + Authentication
http://onesearch.sun.com/search/onesearch/index.jsp?qt=ssl+authentication&subCat=siteforumid%3Ajava43&site=dev&dftab=siteforumid%3Ajava43&chooseCat=javaall&col=developer-forums -
Using java mail api without servlets, I ve sent an html mail. In that Ive
Using java mail api without servlets, I ve sent an html mail. In that Ive specified action to the servlet. On click, it shows url as file:///C:/Documents%20and%20Settings/sirivanig/Local%20Settings/Temporary%20Internet%20Files/Content.IE5/CNFHMDIT/updatemailerform%5B2%5D.htm instead of www.servername.com/....
May I know the reason why it shows like this.
Do I ve to send htmlmail through servlet so that it does show proper url?Possibly your mailer is restricting your access to URLs to prevent
various scams. Without the details, it's hard to know what's going
wrong.
Have you tried with a different mailer? -
How to do SAVE AS DRAFT using java mail api
Hello,
I want to know how to do SAVE AS DRAFT using Java mail Api.
thanksHello,
I want to know how to do SAVE AS DRAFT using Java
mail Api.
thanksI don't think you can. That sounds like a feature of a mail client app itself. The Java mail API is for when you're ready to actually send the thing. Saving drafts is storing them somewhere like you would any other data; nothing to do with the mail API. -
Can i use java mail APIs to read Unix mail
hi
can i use java mail APIs to read Unix mail?
Unix has "mail" command which can be used to access mails on the Unix system.
is it possible to execute mail & access unix mails remotely using POP/IMAP
thanks in advanceThere are JavaMail providers that allow you to access the Unix mail spool files directly. Alternatively the standard JavaMail API can access a POP or IMAP server, if one is installed.
-
Problem in retrieving email using java mail api
hi,
In my project,i am retrieving mails from a particular email id.
I am able to retrieve the latest mails and save it in a folder in my system.
The problem is whenever i run the program eventhough the most recently received mail in inbox is retrieved and saved,again it is retrieving the same one and saving it in the same folder(not repeating).
I tried to check the newmessages in the inbox using the folder.hasNewMessage() method in java mail api,but the method is returning false only regardless new mail is there in inbox or not.
I want to read the unread messages only.Dont want to retrieve the already read mails.
I got the mail retrieving code from the below site.(sorry not posting the code because it is so long and having 4 classes)
http://www.builderau.com.au/program/java/soa/Getting_the_mail_in_receiving_in_JavaMail/0,39024620,39228060,00.htm
Can anyone tell me how to read unread mails in the inbox?
Thanks a lothi parvathi
i think your mail program is receving mails using imap
the imap is only receve the mail from server but the pop is deleting the mails after receving
use the following sample code
package com.sfrc.mail.pop;
import javax.mail.*;
import javax.mail.internet.*;
import com.sun.mail.handlers.message_rfc822;
import java.util.*;
import java.io.*;
* Owner: SFRC IT Solutions Pvt Ltd
* Author:Arunkumar Subramaniam
* Date :12-06-2006
* File Name: AttachRecive.java
public class AttachRecive
public static void main(String args[])
try
String popServer="192.168.1.1";
String popUser="pl";
String popPassword="password";
// Create empty properties
Properties props = new Properties();
// Get session
Session session = Session.getDefaultInstance(props, null);
// Get the store
Store store = session.getStore("pop3");
store.connect(popServer, popUser, popPassword);
// Get folder
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
// Get directory
Message message =folder.getMessages();
Multipart mp = (Multipart)message.getContent();
for (int i=0, n=mp.getCount(); i<n; i++) {
Part part = mp.getBodyPart(i);
String disposition = part.getDisposition();
// Close connection
folder.close(false);
store.close();
catch (Exception ex)
System.out.println("Usage: "
+" popServer popUser popPassword");
System.exit(0);
Regards
Arunkumar Subramaniam
SFRC IT Solutions Pvt Ltd
Chennai -
Problem in sending messages using java mail api
Hi All,
I have a problem in sending messages via java mail api.
MimeMessage message = new MimeMessage(session);
String bodyContent = "ñSunJava";
message.setText (bodyContent,"utf-8");using the above code its not possible for me to send the attachment. if i am using the below code means special characters like ñ gets removed or changed into some other characters.
MimeBodyPart messagePart = new MimeBodyPart();
messagePart.setText(bodyText);
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource("C:/sunjava.txt") {
public String getContentType() {
return "application/octet-stream";
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messagePart);
multipart.addBodyPart(attachmentPart);
message.setContent(multipart);
Transport.send(message);is there any way to send the file attachment with the body message without using MultiPart java class.Taken pretty much straight out of the Javamail examples the following works for me (mail read using Thunderbird) // Define message
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
// Set the 'to' address
for (int i = 0; i < to.length; i++)
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the 'cc' address
for (int i = 0; i < cc.length; i++)
message.addRecipient(Message.RecipientType.CC, new InternetAddress(cc[i]));
// Set the 'bcc' address
for (int i = 0; i < bcc.length; i++)
message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bcc[i]));
message.setSubject("JavaMail With Attachment");
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message
messageBodyPart.setText("Here's the file ñSunJava");
// Create a Multipart
Multipart multipart = new MimeMultipart();
// Add part one
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
for (int count = 0; count < 5; count++)
String filename = "hello" + count + ".txt";
String fileContent = " ñSunJava - Now is the time for all good men to come to the aid of the party " + count + " \n";
// Create another body part
BodyPart attachementBodyPart = new MimeBodyPart();
// Get the attachment
DataSource source = new StringDataSource(fileContent, filename);
// Set the data handler to this attachment
attachementBodyPart.setDataHandler(new DataHandler(source));
// Set the filename
attachementBodyPart.setFileName(filename);
// Add this part
multipart.addBodyPart(attachementBodyPart);
// Put parts in message
message.setContent(multipart);
// Send the message
Transport.send(message); -
Problem in sending mail using java mail api
This is the pogram I am using as of now to send a mail to yahoo id.
import javax.mail.*;
import javax.mail.internet.*;
public class SendingMail2
public SendingMail2()
try
String from = "ravikiran_sunrays";
String to = "[email protected]";
String subject = "the subject u wanna send ";
String cc="[email protected]";
String bcc="[email protected]";
String text="the matter that u wanna send ";
java.util.Properties prop = System.getProperties();
prop.put("mail.smtp.host","mail.yahoo.com");
//prop.put("http.proxyHost",System.getProperty("http.proxyHost"));
//prop.put("http.proxyPort","8080");
//prop.put("http.proxyPort",System.getProperty("http.proxyPort"));
//prop.put("http.proxyHost","172.19.48.201");
//System.getProperties().setProperty("http.proxyPort","8080");
//System.getProperties().setProperty("http.proxyHost","172.19.48.201");
Session ses = Session.getInstance(prop,null);
MimeMessage message = new MimeMessage(ses);
try
Address fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
message.setSubject(subject);
Address[] toAddress = InternetAddress.parse(to);
Address[] cc_address=InternetAddress.parse(cc);
Address[] bcc_address=InternetAddress.parse(bcc);
message.setRecipients(Message.RecipientType.TO,toAddress);
message.setRecipients(Message.RecipientType.CC,cc_address);
message.setRecipients(Message.RecipientType.BCC,bcc_address);
message.setSentDate(new java.util.Date());
message.setText(text);
Transport.send(message);
System.out.println("Mail Successfully Sent");
catch(Exception e)
System.out.println("Problem " + e);
catch(Exception e)
System.out.println("Problem " + e);
public static void main(String[] args)
SendingMail2 sendingMail2 = new SendingMail2();
This is the exception I am getting when I try 2 execute that program.
avax.mail.SendFailedException: Sending failed;
nested exception is:
javax.mail.MessagingException: Unknown SMTP host: mail.yahoo.com;
nested exception is:
java.net.UnknownHostException: mail.yahoo.comlisten buddy
this is a class i made it is easy to understand it sends mails and check inbox just adduser from telnet with remote manager in james create the three accounts i am using and then use this class and its methods
also the next class that contains the mails test my class and what i am saying
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class MailClient
extends Authenticator
public static final int SHOW_MESSAGES = 1;
public static final int CLEAR_MESSAGES = 2;
public static final int SHOW_AND_CLEAR =
SHOW_MESSAGES + CLEAR_MESSAGES;
protected String from;
protected Session session;
protected PasswordAuthentication authentication;
public MailClient(String user, String host)
this(user, host, false);
public MailClient(String user, String host, boolean debug)
from = user + '@' + host;
authentication = new PasswordAuthentication(user, user);
Properties props = new Properties();
props.put("mail.user", user);
props.put("mail.host", host);
props.put("mail.debug", debug ? "true" : "false");
props.put("mail.store.protocol", "pop3");
props.put("mail.transport.protocol", "smtp");
session = Session.getInstance(props, this);
public PasswordAuthentication getPasswordAuthentication()
return authentication;
public void sendMessage(
String to, String subject, String content)
throws MessagingException
System.out.println("SENDING message from " + from + " to " + to);
System.out.println();
MimeMessage msg = new MimeMessage(session);
msg.addRecipients(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setText(content);
Transport.send(msg);
public void checkInbox(int mode)
throws MessagingException, IOException
if (mode == 0) return;
boolean show = (mode & SHOW_MESSAGES) > 0;
boolean clear = (mode & CLEAR_MESSAGES) > 0;
String action =
(show ? "Show" : "") +
(show && clear ? " and " : "") +
(clear ? "Clear" : "");
System.out.println(action + " INBOX for " + from);
Store store = session.getStore();
store.connect();
Folder root = store.getDefaultFolder();
Folder inbox = root.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
if (msgs.length == 0 && show)
System.out.println("No messages in inbox");
for (int i = 0; i < msgs.length; i++)
MimeMessage msg = (MimeMessage)msgs;
if (show)
System.out.println(" From: " + msg.getFrom()[0]);
System.out.println(" Subject: " + msg.getSubject());
System.out.println(" Content: " + msg.getContent());
if (clear)
msg.setFlag(Flags.Flag.DELETED, true);
inbox.close(true);
store.close();
System.out.println();
====================================
testing this class
=======================================
public class JamesConfigTest
public static void main(String[] args)
throws Exception
// CREATE CLIENT INSTANCES
MailClient redClient = new MailClient("red", "localhost");
MailClient greenClient = new MailClient("green", "localhost");
MailClient blueClient = new MailClient("blue", "localhost");
// CLEAR EVERYBODY'S INBOX
redClient.checkInbox(MailClient.CLEAR_MESSAGES);
greenClient.checkInbox(MailClient.CLEAR_MESSAGES);
blueClient.checkInbox(MailClient.CLEAR_MESSAGES);
Thread.sleep(500); // Let the server catch up
// SEND A COUPLE OF MESSAGES TO BLUE (FROM RED AND GREEN)
redClient.sendMessage(
"blue@localhost",
"Testing blue from red",
"This is a test message");
greenClient.sendMessage(
"blue@localhost",
"Testing blue from green",
"This is a test message");
Thread.sleep(500); // Let the server catch up
// LIST MESSAGES FOR BLUE (EXPECT MESSAGES FROM RED AND GREEN)
blueClient.checkInbox(MailClient.SHOW_AND_CLEAR);
=======================================================
it suppose to print this
Clear INBOX for red@localhost
Clear INBOX for green@localhost
Clear INBOX for blue@localhost
SENDING message from red@localhost to blue@localhost
SENDING message from green@localhost to blue@localhost
Show and Clear INBOX for blue@localhost
From: green@localhost
Subject: Testing blue from green
Content: This is a test message
From: red@localhost
Subject: Testing blue from red
Content: This is a test message
thanks a lot
but i need ur help plzzzzzzzzzzzz
i can create account from telnet
but how i can create a new account from java .. a jsp page that i made to create a new account on the server
plzzzzzzzz help me
bye -
Problems sending email using javax.mail.*
I need to send an email from an application I am working on. I am using the features of the javax.mail package to do so. In looking at the code I am unsure why this is not working. This is my first time using this package so it might be something silly I am missing so any of your thoughts are appreciated. The code is as follows:
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailTester {
public static void main(String[] args) {
try {
//Set the smtp address
Properties props = new Properties();
props.put("mail.smtp.host", args[0] );
// get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(true);
// create a message for this session
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress from =
new InternetAddress( args[1] );
InternetAddress[] to = new InternetAddress[1];
to[0] = new InternetAddress( args[2] );
msg.setFrom(from);
msg.setRecipients(Message.RecipientType.TO, to);
// set the subject and content type
msg.setSubject("subject");
msg.setContent("this is my test email", "text/plain");
// send the email
Transport.send(msg);
catch (MessagingException me) {I have an EMail class that I use at:
http://www.discoverteenergy.com/files/EMail.java
Feel free to use it or compare against your code to see what is different.
Maybe you are looking for
-
How can i get the footer bar to stay, whilst page scrolls?
how can i get the footer bar to stay, whilst page scrolls? at the moment, on my site, if the page is longer than the screen, it goes behind the footer, but the foot bar moves up the page as yo scroll down to see the rest of the content. any way of ge
-
Audio Skimming not in sync with actual Audio
Hi Guys, Since about a week or so, my Audio Skimming seems a little off. When turned on, and actually skimming, it let's me hear something that's at least 4 seconds off. I have no idea how to fix this, so all help is needed Thank you so much, Nikkie
-
How can I watch purchased movies on apple tv?
How can I watch purchased movies on apple tv? I recently purchased an apple tv and cannot play the movies I have bought on my itunes account? Can anyone help?
-
I'm new to After Effects and I'm using AE6. I'm working on a video clip. After drawing a Region of Interest I selected Crop Comp to Region of Interest in the Composition drop down menu. Nothing happens! The original bounding box of the comp stays t
-
Procedure o/p in the excel file
Hi, Oracle9i Written a batch script which calls the oracle procedure. But, want the o/p of the procedure in the excel file columnwise with the column headings ? How it can be done ? Regards