Send a Simple E-mail:
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 email ID needs to be mentioned      String from = "web@gmail.com";      // Assuming you are sending email from localhost      String host = "localhost";      // Get system properties      Properties properties = System.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 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(Message.RecipientType.TO,                                  new InternetAddress(to));         // Set Subject: header field         message.setSubject("This is the Subject Line!");         // Now set the actual message         message.setText("This is actual message");         // Send message         Transport.send(message);         System.out.println("Sent message successfully....");      }catch (MessagingException mex) {         mex.printStackTrace();      }   }}


Send Attachment in E-mail:

import java.util.*;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class SendFileEmail{   public static void main(String [] args)   {            // Recipient's email ID needs to be mentioned.      String to = "abcd@gmail.com";      // Sender's email ID needs to be mentioned      String from = "web@gmail.com";      // Assuming you are sending email from localhost      String host = "localhost";      // Get system properties      Properties properties = System.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 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(Message.RecipientType.TO,                                  new InternetAddress(to));         // Set Subject: header field         message.setSubject("This is the Subject Line!");         // Create the message part          BodyPart messageBodyPart = new MimeBodyPart();         // Fill the message         messageBodyPart.setText("This is message body");                  // Create a multipar message         Multipart multipart = new MimeMultipart();         // Set text message part         multipart.addBodyPart(messageBodyPart);         // Part two is attachment         messageBodyPart = new MimeBodyPart();         String filename = "file.txt";         DataSource source = new FileDataSource(filename);         messageBodyPart.setDataHandler(new DataHandler(source));         messageBodyPart.setFileName(filename);         multipart.addBodyPart(messageBodyPart);         // Send the complete message parts         message.setContent(multipart );         // Send message         Transport.send(message);         System.out.println("Sent message successfully....");      }catch (MessagingException mex) {         mex.printStackTrace();      }   }}

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. MySQL DEFINER具体使用详解
  2. 一篇文章带你了解SQL之CASE WHEN用法详解
  3. MySQL隔离级别和锁机制的深入讲解
  4. 浅析MySQL 主键使用数字还是uuid查询快
  5. MySQL之权限以及设计数据库案例讲解
  6. MySQL为何不建议使用默认值为null列
  7. Node-Red实现MySQL数据库连接的方法
  8. MySQL如何解决幻读问题
  9. 浅谈MySQL之select优化方案
  10. SQL实现LeetCode(197.上升温度)