Code for
Java-Mail
I am sending the code for Java-Mail.
sendmail.java
---------------
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.activation.*;
import javax.swing.text.*;
import javax.swing.*;
import java.io.*;
import java.awt.*;
public class SendMail{
public SendMail(){ }
private class SMTPAuthenticator
extends javax.mail.Authenticator{
public PasswordAuthentication getPasswordAuthentication(){
String username = "Muni";
String password = "Raj";
return new PasswordAuthentication(username, password);
}
}
public void postMail() throws MessagingException {
boolean debug
= false;
String recipients[]
= {"java.muni@rediffmail.com"};
String recipientsCC[] = {"likhis_007@yahoo.com"};
String subject
= "Mail";
String message
= "This is body for the message";
String from
= "pop_muni@yahoo.com";
Properties props
= new Properties();
props.put("mail.smtp.host","208.220.245.70");//mail.yahoo.com/68.142.225.208
props.put("mail.smtp.auth","true");
Authenticator auth = new SMTPAuthenticator();
Session session
= Session.getDefaultInstance(props, auth);
session.setDebug(debug);
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);
//recipientsCC[]
InternetAddress[] addressCC = new InternetAddress[recipientsCC.length];
for(int j = 0; j < recipientsCC.length; j++){
addressCC[j] = new InternetAddress(recipientsCC[j]);
}
msg.setRecipients(Message.RecipientType.CC, addressCC);
// Optional : You can also set your custom headers in the Email if you
Want
msg.addHeader("MyHeaderName", "myHeaderValue");
msg.setSubject(subject);
msg.setContent(message,"text/html");
Transport.send(msg);
System.out.println("Mail Successfully Sent");
}//End of postMail()
}//End of Class
~~ Muni
Related:
Java Books
Java Certification,
Programming, JavaBean and Object Oriented Reference Books
Return to : Java
Programming Hints and Tips
All the site contents are Copyright © www.erpgreat.com
and the content authors. All rights reserved.
All product names are trademarks of their respective
companies.
The site www.erpgreat.com is not affiliated with or endorsed
by any company listed at this site.
Every effort is made to ensure the content integrity.
Information used on this site is at your own risk.
The content on this site may not be reproduced
or redistributed without the express written permission of
www.erpgreat.com or the content authors.
|