• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

javamail question!

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read this?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
# 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
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other APIs is a better place for JavaMail questions, so I'm moving this thread.
 
jim li
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
reply
    Bookmark Topic Watch Topic
  • New Topic