ITeye博客

用JavaMail发送HTML格式的邮件

unser · 2007-04-12 22:30 · Java · Markdown

java 代码

  1. import java.security.Security;

  2. import java.util.Date;

  3. import java.util.HashMap;

  4. import java.util.Map;

  5. import java.util.Properties;

  6. import javax.mail.Authenticator;

  7. import javax.mail.Message;

  8. import javax.mail.Multipart;

  9. import javax.mail.PasswordAuthentication;

  10. import javax.mail.Session;

  11. import javax.mail.Transport;

  12. import javax.mail.internet.InternetAddress;

  13. import javax.mail.internet.MimeBodyPart;

  14. import javax.mail.internet.MimeMessage;

  15. import javax.mail.internet.MimeMultipart;

  16. import org.apache.commons.logging.Log;

  17. import org.apache.commons.logging.LogFactory;

  18. import cn.soyu.util.MailClient;

  19. import cn.soyu.shopping.view.orders;

  20. /**

  21. * 使用Gmail发送邮件

  22. *

  23. * @author Rain Chen

  24. */

  25. public class GmailSender {

  26. private static final Log logger = LogFactory.getLog(GmailSender.class);

  27. private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

  28. private static final String USERNAME = "gdunser";//设定邮箱的用户名

  29. private static final String PASSWORD = "123456";//设定邮箱的密码

  30. private static final String TO_EMAIL = "gdunser@gmail.com";//设定收件人的信箱

  31. private static final String SUBJECT = "你有新订单!";//设定邮件标题

  32. private static final String FROM = "gdunser@gmail.com";//设定发件件的人

  33. private static final String SMTP = "smtp.gmail.com";

  34. public static void sender(String subject, String content) throws Exception {

  35. Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

  36. // Get a Properties object

  37. Properties props = System.getProperties();

  38. props.setProperty("mail.smtp.host", SMTP);

  39. props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);

  40. props.setProperty("mail.smtp.socketFactory.fallback", "false");

  41. props.setProperty("mail.smtp.port", "465");

  42. props.setProperty("mail.smtp.socketFactory.port", "465");

  43. props.put("mail.smtp.auth", "true");

  44. Session session = Session.getDefaultInstance(props,

  45. new Authenticator() {

  46. protected PasswordAuthentication getPasswordAuthentication() {

  47. return new PasswordAuthentication(USERNAME, PASSWORD);

  48. }

  49. });

  50. Message msg = new MimeMessage(session);

  51. InternetAddress[] address = null;

  52. // 设定发邮件的人

  53. msg.setFrom(new InternetAddress(FROM));

  54. // 设定收信人的信箱

  55. address = InternetAddress.parse(TO_EMAIL, false);

  56. msg.setRecipients(Message.RecipientType.TO, address);

  57. // 设定信中的主题

  58. msg.setSubject(subject);

  59. // 设定送信的时间

  60. msg.setSentDate(new Date());

  61. Multipart mp = new MimeMultipart();

  62. MimeBodyPart mbp = new MimeBodyPart();

  63. // 设定邮件内容的类型为 text/plain 或 text/html

  64. mbp.setContent(content, "text/html;charset=GB2312");

  65. mp.addBodyPart(mbp);

  66. msg.setContent(mp);

  67. Transport transport = session.getTransport("smtp");

  68. transport.connect(SMTP, USERNAME, PASSWORD);

  69. transport.sendMessage(msg, msg.getAllRecipients());

  70. transport.close();

  71. }

  72. public static void main(String[] args) throws Exception {

  73. sender(

  74. SUBJECT,

  75. "

    恭喜你!收到邮件了!

    www.3km.com.cn 

    "

    );

  76. System.out.println("Message sent.");

  77. }

  78. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。