Go Back   Wiki NewForum | Latest Entertainment News > Career Forum & Tips > Tech Forum & Tutorial > Java Forum & Tutorial


Code for Java-Mail


Reply
Views: 2107  
Thread Tools Rate Thread
  #1  
Old 05-25-2009, 04:45 PM
bholus7
Guest
 
Posts: n/a
Default Code for Java-Mail

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

Reply With Quote
Reply

New topics in Java Forum & Tutorial





Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
WikiNewForum)