what's the easiest way to send email with SMTP authentication using java?

N

networm

Hi all,

In Linux/Unix I can have as simple as a command "mail" to send out an
email...

In Matlab I can use "sendmail" to send out an email with or without an
attachment.

I also know that in Java there are also simple ways of sending out email...

But what if the SMTP server needs authentication, like Yahoo SMTP, and even
my school email system, needs to first SSH log onto the server, then send
email from the server...

Are there any simple ways of sending out email for the above two cases?

Thanks a lot
 
R

Rob Shepherd

networm said:
Hi all,

In Linux/Unix I can have as simple as a command "mail" to send out an
email...

In Matlab I can use "sendmail" to send out an email with or without an
attachment.

I also know that in Java there are also simple ways of sending out email...

But what if the SMTP server needs authentication, like Yahoo SMTP, and even
my school email system, needs to first SSH log onto the server, then send
email from the server...

Are there any simple ways of sending out email for the above two cases?

Thanks a lot

http://www.coolservlets.com/Email/README.html

I am unsure if this will handle secure smtp.

hth

Rob
 
N

Nigel Wade

networm said:
Hi all,

In Linux/Unix I can have as simple as a command "mail" to send out an
email...

In Matlab I can use "sendmail" to send out an email with or without an
attachment.

I also know that in Java there are also simple ways of sending out email...

But what if the SMTP server needs authentication, like Yahoo SMTP, and even
my school email system, needs to first SSH log onto the server, then send
email from the server...

Are there any simple ways of sending out email for the above two cases?

Thanks a lot

For authenticated smtp you should be able to use javax.mail. I think a
Session can be given PasswordAuthenticion (I've never tried it...).

If you actually need to be logged onto the mail server in order to send mail
there's not a lot you can do from Java. You might be able to setup the ssh
session to use public key authentication so you can ssh to the mail server
without having to respond to a password prompt. That would allow you to run
the necessary ssh commands from Java. Otherwise you need some kind of
service on the server with which your Java client can communicate.
 
D

Dan

Here is a very basic SendMail thread. You simply need to pass in the
appropriate values. If you don't want authentication, you can turn it
off by setting the "mail.smtp.auth" to "false":

private class SendMail extends Thread {
private String to_mail, subj, content, host, user, pass;
public SendMail(String to_mail, String subj, String content,
String host, String user, String pass) {
this.to_mail = to_mail;
this.subj = subj;
this.content = content;
this.host = host;
this.user = user;
this.pass = pass;
}

public void run () {
try {
SMTPAuthenticator auth = new SMTPAuthenticator(user, pass);
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new
InternetAddress(to_mail));
message.setSubject(subj);
message.setText(content);
Transport.send(message);
}
catch (Exception e) { System.out.println(e); }
}
}//end of sendmail
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top