Programm to Send Proof and Upload New Version

Java - Sending Email


To ship an east-mail using your Java Application is simple enough simply to commencement with yous should take JavaMail API and Coffee Activation Framework (JAF) installed on your machine.

  • Y'all can download latest version of JavaMail (Version 1.ii) from Java's standard website.

  • You can download latest version of JAF (Version 1.1.1) from Java's standard website.

Download and unzip these files, in the newly created top level directories you lot will notice a number of jar files for both the applications. You need to add mail.jar and activation.jar files in your CLASSPATH.

Send a Simple Due east-mail

Hither is an example to send a simple electronic mail from your machine. It is assumed that your localhost is connected to the Net and capable enough to transport an e-mail.

Example

// File Name SendEmail.java  import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;  public class SendEmail {     public static void main(String [] args) {           // Recipient's email ID needs to be mentioned.       String to = "abcd@gmail.com";        // Sender's electronic mail ID needs to exist mentioned       String from = "spider web@gmail.com";        // Assuming yous are sending email from localhost       String host = "localhost";        // Get system properties       Properties properties = System.getProperties();        // Setup mail server       backdrop.setProperty("mail.smtp.host", host);        // Get the default Session object.       Session session = Session.getDefaultInstance(backdrop);        endeavor {          // Create a default MimeMessage object.          MimeMessage message = new MimeMessage(session);           // Set From: header field of the header.          message.setFrom(new InternetAddress(from));           // Set To: header field of the header.          message.addRecipient(Bulletin.RecipientType.TO, new InternetAddress(to));           // Set Subject area: header field          bulletin.setSubject("This is the Field of study Line!");           // Now set the bodily message          message.setText("This is bodily message");           // Ship message          Transport.send(message);          Arrangement.out.println("Sent bulletin successfully....");       } catch (MessagingException mex) {          mex.printStackTrace();       }    } }        

Compile and run this program to send a simple electronic mail −

Output

$ java SendEmail Sent bulletin successfully....        

If you want to ship an email to multiple recipients then the post-obit methods would be used to specify multiple email IDs −

void addRecipients(Message.RecipientType type, Accost[] addresses)    throws MessagingException        

Hither is the description of the parameters −

  • blazon − This would be set to TO, CC or BCC. Hither CC represents Carbon Copy and BCC represents Blackness Carbon Copy. Example: Message.RecipientType.TO

  • addresses − This is an array of e-mail ID. You would need to utilize InternetAddress() method while specifying email IDs.

Ship an HTML Eastward-mail

Hither is an case to transport an HTML e-mail from your machine. Here it is causeless that your localhost is connected to the Internet and capable plenty to transport an e-mail.

This case is very similar to the previous i, except here nosotros are using setContent() method to set content whose second statement is "text/html" to specify that the HTML content is included in the message.

Using this example, you tin can send as big equally HTML content you like.

Example

// File Proper noun SendHTMLEmail.java  import coffee.util.*; import javax.mail.*; import javax.mail.net.*; import javax.activation.*;  public course SendHTMLEmail {     public static void chief(Cord [] args) {       // Recipient's electronic mail ID needs to exist mentioned.       String to = "abcd@gmail.com";        // Sender's e-mail ID needs to be mentioned       String from = "web@gmail.com";        // Assuming you are sending email from localhost       String host = "localhost";        // Get system properties       Properties backdrop = Arrangement.getProperties();        // Setup mail server       properties.setProperty("postal service.smtp.host", host);        // Get the default Session object.       Session session = Session.getDefaultInstance(properties);        try {          // Create a default MimeMessage object.          MimeMessage message = new MimeMessage(session);           // Prepare From: header field of the header.          message.setFrom(new InternetAddress(from));           // Set To: header field of the header.          message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));           // Set Subject: header field          message.setSubject("This is the Discipline Line!");           // Send the actual HTML message, as big as yous similar          bulletin.setContent("<h1>This is actual bulletin</h1>", "text/html");           // Transport bulletin          Send.send(message);          System.out.println("Sent message successfully....");       } grab (MessagingException mex) {          mex.printStackTrace();       }    } }        

Compile and run this program to send an HTML e-mail −

Output

$ java SendHTMLEmail Sent bulletin successfully....        

Ship Attachment in Email

Here is an instance to ship an e-mail with attachment from your machine. Here information technology is causeless that your localhost is connected to the internet and capable enough to ship an e-post.

Case

// File Name SendFileEmail.coffee  import java.util.*; import javax.mail.*; import javax.mail.internet.*; import javax.activation.*;  public form SendFileEmail {     public static void main(String [] args) {            // Recipient's email ID needs to be mentioned.       String to = "abcd@gmail.com";        // Sender'southward email ID needs to be mentioned       String from = "web@gmail.com";        // Bold you are sending e-mail from localhost       String host = "localhost";        // Get system properties       Properties backdrop = Organization.getProperties();        // Setup mail server       properties.setProperty("mail.smtp.host", host);        // Get the default Session object.       Session session = Session.getDefaultInstance(properties);        try {          // Create a default MimeMessage object.          MimeMessage bulletin = new MimeMessage(session);           // Set From: header field of the header.          message.setFrom(new InternetAddress(from));           // Set To: header field of the header.          message.addRecipient(Bulletin.RecipientType.TO,new InternetAddress(to));           // Set Subject area: header field          message.setSubject("This is the Subject Line!");           // Create the bulletin office           BodyPart messageBodyPart = new MimeBodyPart();           // Make full the message          messageBodyPart.setText("This is message torso");                    // Create a multipar bulletin          Multipart multipart = new MimeMultipart();           // Prepare text message part          multipart.addBodyPart(messageBodyPart);           // Part two is attachment          messageBodyPart = new MimeBodyPart();          Cord filename = "file.txt";          DataSource source = new FileDataSource(filename);          messageBodyPart.setDataHandler(new DataHandler(source));          messageBodyPart.setFileName(filename);          multipart.addBodyPart(messageBodyPart);           // Send the complete bulletin parts          message.setContent(multipart );           // Send message          Ship.send(message);          System.out.println("Sent bulletin successfully....");       } catch (MessagingException mex) {          mex.printStackTrace();       }    } }        

Compile and run this programme to send an HTML email −

Output

$ coffee SendFileEmail Sent message successfully....        

User Authentication Part

If it is required to provide user ID and Password to the eastward-mail server for hallmark purpose, and so you can set these backdrop as follows −

props.setProperty("mail service.user", "myuser"); props.setProperty("mail.password", "mypwd");        

Residue of the email sending mechanism would remain as explained above.

Useful Video Courses


Java Date and Time Online Training

Video

Java Servlet Online Training

Video

JavaScript Online Training

Video

Java Essential Training

Video

Java Essentials Online Training

Video

egbertshorms97.blogspot.com

Source: https://www.tutorialspoint.com/java/java_sending_email.htm

0 Response to "Programm to Send Proof and Upload New Version"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel