aspose file tools*
The moose likes Other JSE/JEE APIs and the fly likes javamail question! 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 "javamail question!" Watch "javamail question!" New topic
Author

javamail question!

jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
package com.blinco.wave3x.email;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* This class help us to send out an e-mail with the subject and content
*/
public final class Email {

public static void main( String... aArguments ){
Email emailer = new Email();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail(
"fromblah@blah.com", "toblah@blah.com",
"Testing 1-2-3", "blah blah blah"
);
}


/**
* the private static variable
*/
private static Properties fMailServerConfig = new Properties();

/**
* Send a single email.
*/
public void sendEmail(
String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody
){
Session session = Session.getDefaultInstance( fMailServerConfig, null );
MimeMessage message = new MimeMessage( session );
try {
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );
message.setText( aBody );
Transport.send( message );

}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}

/**
* Allows the config to be refreshed at runtime, instead of
* requiring a restart.
*/
public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}

static {
fetchConfig();
}

/**
* Open a specific text file containing mail server
* parameters, and populate a corresponding Properties object.
*/
private static void fetchConfig() {
fMailServerConfig.put("mail.smtp.auth", "true");

}
}

i got exception: javax.mail.AuthenticationFailedException


can anyone give me some suggestions?

thank you in advance
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Have you read this?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Matteo Di Furia
Ranch Hand

Joined: Jun 20, 2008
Posts: 102
I think the problem is in line

<blockquote>code:
<pre name="code" class="core">
Session session = Session.getDefaultInstance( fMailServerConfig, null );
</pre>
</blockquote>

You're creating a Session specifing "null" as the authenticator, which only works if your SMTP server supports anonym access.
It seems it does not. To overcome this problem, make sure you have an account on the SMTP server, create and setup an Authenticator accordingly to that account (basically you set username and password), and get Session instance passing the Authenticator instance.

P.S. Else yes, you could do as Paul suggests you
[ July 17, 2008: Message edited by: Matteo Di Furia ]
jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
hi

i am totally new to this part of programming:

so can you tell me what is username and password?

is it like ("exei@yahoo.ca", "easseafe")


thankyou
jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
package com.blinco.wave3x.email;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* This class help us to send out an e-mail with the subject and content
*/
public final class Email {

public static void main( String... aArguments ){
Email emailer = new Email();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail(
"xibrian@yahoo.ca", "xibrian@yahoo.ca",
"Testing 1-2-3", "blah blah blah"
);
}


/**
* the private static variable
*/
private static Properties fMailServerConfig = new Properties();

/**
* Send a single email.
*/
public void sendEmail(
String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody
){
Session session = Session.getDefaultInstance( fMailServerConfig, null );
MimeMessage message = new MimeMessage( session );
try {
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );
message.setText( aBody );

String protocol = "smtp";
fMailServerConfig.put("mail." + protocol + ".auth", "true");

Transport t = session.getTransport(protocol);
try {
t.connect("ppbrian@yahoo.ca", "rtrew");
t.sendMessage(message, message.getAllRecipients());
} finally {
t.close();
}

}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}

/**
* Allows the config to be refreshed at runtime, instead of
* requiring a restart.
*/
public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}

static {
fetchConfig();
}

/**
* Open a specific text file containing mail server
* parameters, and populate a corresponding Properties object.
*/
private static void fetchConfig() {
InputStream input = null;
try {
//If possible, one should try to avoid hard-coding a path in this
//manner; in a web application, one should place such a file in
//WEB-INF, and access it using ServletContext.getResourceAsStream.
//Another alternative is Class.getResourceAsStream.
//This file contains the javax.mail config properties mentioned above.
input = new FileInputStream( "C:\\mail\\MyMailServer.txt" );
fMailServerConfig.load( input );
}
catch ( IOException ex ){
System.err.println("Cannot open and load mail server properties file.");
}
finally {
try {
if ( input != null ) input.close();
}
catch ( IOException ex ){
System.err.println( "Cannot close mail server properties file." );
}
}
}
}

exception : Cannot send email. javax.mail.MessagingException: Could not connect to SMTP host: www.yahoo.ca, port: 25;
nested exception is:
java.net.ConnectException: Connection timed out: connect
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

Probably there isn't an SMTP server running on that machine. Its name (www.yahoo.ca) rather suggests that it's a web server and not an SMTP server. Did somebody tell you that was the right server name to use? If so, go back and have them confirm it.
jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
# Configuration file for javax.mail
# If a value for an item is not provided, then
# system defaults will be used. These items can
# also be set in code.

# Host whose mail services will be used
# (Default value : localhost)
mail.host= localhost

# Return address to appear on emails
# (Default value : username@host)
mail.from=xibrian@byahoo.ca

# Other possible items include:
# mail.user=
# mail.store.protocol=
# mail.transport.protocol=
# mail.smtp.host=
# mail.smtp.user=
# mail.debug=


this is my configuration file. did i miss something here?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18992
    
    8

Well, as long as you're running an SMTP server on the same machine where the JavaMail code will be running (that's what "localhost" means), and as long as that server doesn't object to sending messages that pretend to be from a different server, that's okay. If it sends the messages, other servers might not accept them or might put them into a spam or junk folder, though.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19792
    
  20

Other APIs is a better place for JavaMail questions, so I'm moving this thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
jim li
Ranch Hand

Joined: May 20, 2008
Posts: 180
package com.blinco.wave3x.email;

import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;

/**
* This class help us to send out an e-mail with the subject and content
*/
public final class Email {

public static void main( String... aArguments ){
Email emailer = new Email();
//the domains of these email addresses should be valid,
//or the example will fail:
emailer.sendEmail(
"myname@mydomain.com", "xibrian@yahoo.ca",
"Testing 1-2-3", "blah blah blah"
);
}


/**
* the private static variable
*/
private static Properties fMailServerConfig = System.getProperties();

/**
* Send a single email.
*/
public void sendEmail(
String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody
){

Authenticator auth = new MyAuthentication();
String protocol = "smtp";

Session session = Session.getDefaultInstance( fMailServerConfig, auth );
session.setDebug(true);



try {
MimeMessage message = new MimeMessage( session );

message.setFrom(new InternetAddress(aFromEmailAddr));
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(aBody);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

message.setContent(multipart);

Transport t = session.getTransport("smtp");

try {
t.connect("smtp.mail.yahoo.ca","xibrian@yahoo.ca", "777");
t.sendMessage(message, message.getAllRecipients());
} finally {
t.close();
}


System.err.print("done");
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}

private class MyAuthentication extends Authenticator {

public MyAuthentication() {
}
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("xibrian@yahoo.ca", "777");
}
}
}


errors:

DEBUG: setDebug: JavaMail version 1.4.1
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.ca", port 25, isSSL false
220 smtp106.mail.mud.yahoo.com ESMTP
DEBUG SMTP: connected to host "smtp.mail.yahoo.ca", port: 25

EHLO jim-summer
250-smtp106.mail.mud.yahoo.com
250-AUTH LOGIN PLAIN XYMCOOKIE
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XYMCOOKIE"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
eGlicmlhbkB5YWhvby5jYQ==
334 UGFzc3dvcmQ6
eWFuZ3Rhbw==
535 authorization failed (#5.7.0)
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "smtp.mail.yahoo.ca", port 25, isSSL false
220 smtp113.mail.mud.yahoo.com ESMTP
DEBUG SMTP: connected to host "smtp.mail.yahoo.ca", port: 25

EHLO jim-summer
250-smtp113.mail.mud.yahoo.com
250-AUTH LOGIN PLAIN XYMCOOKIE
250-PIPELINING
250 8BITMIME
DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XYMCOOKIE"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Attempt to authenticate
AUTH LOGIN
334 VXNlcm5hbWU6
eGlicmlhbkB5YWhvby5jYQ==
334 UGFzc3dvcmQ6
eWFuZ3Rhbw==
535 authorization failed (#5.7.0)
Cannot send email. javax.mail.AuthenticationFailedException

[ July 21, 2008: Message edited by: jim li ]
[ July 21, 2008: Message edited by: jim li ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javamail question!