小白教程

 找回密码
 立即注册
小白教程 首页 系列教程 Java系列教程 查看内容

Java 发送邮件

发布者: 小白教程



发送一封HTML E-mail

下面是一个发送HTML E-mail的例子。假设你的localhost已经连接到网络。

和上一个例子很相似,除了我们要使用setContent()方法来通过第二个参数为"text/html",来设置内容来指定要发送HTML内容。

  1. // 文件名 SendHTMLEmail.java
  2. import java.util.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import javax.activation.*;
  6. public class SendHTMLEmail
  7. {
  8. public static void main(String [] args)
  9. {
  10. // 收件人电子邮箱
  11. String to = "abcd@gmail.com";
  12. // 发件人电子邮箱
  13. String from = "web@gmail.com";
  14. // 指定发送邮件的主机为 localhost
  15. String host = "localhost";
  16. // 获取系统属性
  17. Properties properties = System.getProperties();
  18. // 设置邮件服务器
  19. properties.setProperty("mail.smtp.host", host);
  20. // 获取默认的 Session 对象。
  21. Session session = Session.getDefaultInstance(properties);
  22. try{
  23. // 创建默认的 MimeMessage 对象。
  24. MimeMessage message = new MimeMessage(session);
  25. // Set From: 头部头字段
  26. message.setFrom(new InternetAddress(from));
  27. // Set To: 头部头字段
  28. message.addRecipient(Message.RecipientType.TO,
  29. new InternetAddress(to));
  30. // Set Subject: 头字段
  31. message.setSubject("This is the Subject Line!");
  32. // 发送 HTML 消息, 可以插入html标签
  33. message.setContent("<h1>This is actual message</h1>",
  34. "text/html" );
  35. // 发送消息
  36. Transport.send(message);
  37. System.out.println("Sent message successfully....");
  38. }catch (MessagingException mex) {
  39. mex.printStackTrace();
  40. }
  41. }
  42. }

编译并运行此程序来发送HTML e-mail:

  1. $ java SendHTMLEmail
  2. Sent message successfully....

发送带有附件的 E-mail

下面是一个发送带有附件的 E-mail的例子。假设你的localhost已经连接到网络。

  1. // 文件名 SendFileEmail.java
  2. import java.util.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import javax.activation.*;
  6. public class SendFileEmail
  7. {
  8. public static void main(String [] args)
  9. {
  10. // 收件人电子邮箱
  11. String to = "abcd@gmail.com";
  12. // 发件人电子邮箱
  13. String from = "web@gmail.com";
  14. // 指定发送邮件的主机为 localhost
  15. String host = "localhost";
  16. // 获取系统属性
  17. Properties properties = System.getProperties();
  18. // 设置邮件服务器
  19. properties.setProperty("mail.smtp.host", host);
  20. // 获取默认的 Session 对象。
  21. Session session = Session.getDefaultInstance(properties);
  22. try{
  23. // 创建默认的 MimeMessage 对象。
  24. MimeMessage message = new MimeMessage(session);
  25. // Set From: 头部头字段
  26. message.setFrom(new InternetAddress(from));
  27. // Set To: 头部头字段
  28. message.addRecipient(Message.RecipientType.TO,
  29. new InternetAddress(to));
  30. // Set Subject: 头字段
  31. message.setSubject("This is the Subject Line!");
  32. // 创建消息部分
  33. BodyPart messageBodyPart = new MimeBodyPart();
  34. // 消息
  35. messageBodyPart.setText("This is message body");
  36. // 创建多重消息
  37. Multipart multipart = new MimeMultipart();
  38. // 设置文本消息部分
  39. multipart.addBodyPart(messageBodyPart);
  40. // 附件部分
  41. messageBodyPart = new MimeBodyPart();
  42. String filename = "file.txt";
  43. DataSource source = new FileDataSource(filename);
  44. messageBodyPart.setDataHandler(new DataHandler(source));
  45. messageBodyPart.setFileName(filename);
  46. multipart
上一篇:Java 网络编程下一篇:Java 多线程编程

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 06:17 , Processed in 0.018509 second(s), 18 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

返回顶部