1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
package oracle.fod.storefront;
import ice.util.Settings;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
/*
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 = "smtp";
private static final String SMTP_AUTH_USER = "";
private static final String SMTP_AUTH_PWD = "";
static Date actuelle = new Date();
// * Definition du format utilise pour les dates
static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
// * Donne la date au format "aaaa-mm-jj"
static String dat = dateFormat.format(actuelle);
private static final String emailMsgTxt = "bonjour aujourd'huit c'est" +dat;
private static final String emailSubjectTxt = "test Avie de formation ";
private static final String emailFromAddress = "formation@com";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"middleware@com"};
public static void main(String args[]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
String code ="121155";
System.out.println("Sucessfully Sent mail to All Users");
System.err.println(code);
}
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("<p><img src=\"C:\\RRR\\Sanstitre3.GIF\"></p>", "text/html");
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);
}
}
} |
Partager