|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Re: send Email using java through internet [message #92470 is a reply to message #92382] |
Mon, 02 August 2004 23:54 |
Deepak
Messages: 111 Registered: December 1999
|
Senior Member |
|
|
/*
Some SMTP servers require a username and password authentication before you
can use their Server for Sending mail. This is most common with couple
of ISP's who provide SMTP Address to Send Mail.
This Program gives any example on how to do SMTP Authentication
(User and Password verification)
This is a free source code and is provided as it is without any warranties and
it can be used in any your code for free.
Author : Sudhir Ancha
*/
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
/*
To use this program, change values for the following three constants,
SMTP_HOST_NAME -- Has your SMTP Host Name
SMTP_AUTH_USER -- Has your SMTP Authentication UserName
SMTP_AUTH_PWD -- Has your SMTP Authentication Password
Next change values for fields
emailMsgTxt -- Message Text for the Email
emailSubjectTxt -- Subject for email
emailFromAddress -- Email Address whose name will appears as "from" address
Next change value for "emailList".
This String array has List of all Email Addresses to Email Email needs to be sent to.
Next to run the program, execute it as follows,
SendMailUsingAuthentication authProg = new SendMailUsingAuthentication();
*/
public class SendMailUsingAuthentication
{
private static final String SMTP_HOST_NAME = "myserver.smtphost.com";
private static final String SMTP_AUTH_USER = "deepak";
private static final String SMTP_AUTH_PWD = "mypassword";
private static final String emailMsgTxt = "hi Deepak How are u.";
private static final String emailSubjectTxt = "Checking of Email Program";
private static final String emailFromAddress = "deepak@worldonetel.com";
// Add List of Email address to who email needs to be sent to
private static final String[[]] emailList = {"deepak_thakral007@yahoo.co.in", "deepak_thakral007@rediffmail.com"};
public static void main(String args[[]]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail( String recipients[[ ]], String subject,
String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[[]] addressTo = new InternetAddress[[recipients.length]];
for (int i = 0; i < recipients.length; i++)
{
addressTo[[i]] = new InternetAddress(recipients[[i]]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PWD;
return new PasswordAuthentication(username, password);
}
}
}
|
|
|
|
|
|
|
|
Re: send Email using java through internet [message #92714 is a reply to message #92694] |
Wed, 15 December 2004 01:43 |
Debasish Bhadra
Messages: 1 Registered: December 2004
|
Junior Member |
|
|
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
// Send a simple, single part, text/plain e-mail
public class TestEmail {
public static void main(String[[]] args) {
// SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
String to = "tabs_4u@yahoo.com";
String from = "Mithun";
// SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
String host = "smtp.clicksaltlake.info";
// Create properties, get Session
Properties props = new Properties();
// If using static Transport.send(),
// need to specify which host to send it to
props.put("mail.smtp.host", host);
// To see what is going on behind the scene
//props.put("mail.debug", "true");
Session session = Session.getInstance(props);
try {
// Instantiatee a message
Message msg = new MimeMessage(session);
//Set message attributes
msg.setFrom(new InternetAddress(from));
InternetAddress[[]] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject("Test E-Mail through Java");
msg.setSentDate(new Date());
// Set message content
msg.setText("How are you? " +
"What are you doing?n" +
"Waiting for your reply.n" +
"Byen" +
"Mithun");
//Send the message
Transport.send(msg);
}
catch (MessagingException mex) {
// Prints all nested (chained) exceptions as well
mex.printStackTrace();
}
}
}//End of class
|
|
|
|