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(
"
[email protected]", "
[email protected]",
"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","
[email protected]", "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("
[email protected]", "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 ]