1 package org.tatan.mail;
2
3 import javax.mail.Session;
4 import javax.mail.MessagingException;
5 import javax.mail.Multipart;
6 import javax.mail.Transport;
7 import javax.mail.internet.InternetAddress;
8 import javax.mail.internet.MimeMessage;
9 import javax.mail.internet.MimeBodyPart;
10 import javax.mail.internet.MimeMultipart;
11 import javax.activation.FileDataSource;
12 import javax.activation.DataHandler;
13
14
15 public class SendAttachMail {
16 public static void sendMessage(String smtpHost,
17 String from, String to,
18 String subject, String messageText,
19 String fileName)
20 throws MessagingException {
21
22 // Step 1: Configure the mail session
23 java.util.Properties props = new java.util.Properties();
24 props.setProperty("mail.smtp.auth", "true");//指定是否需要SMTP验证
25 props.setProperty("mail.smtp.host", smtpHost);//指定SMTP服务器
26 props.put("mail.transport.protocol", "smtp");
27 Session mailSession = Session.getDefaultInstance(props);
28 mailSession.setDebug(true);//是否在控制台显示debug信息
29
30 // Step 2: Construct the message
31 System.out.println("Constructing message - from=" + from + " to=" + to);
32 InternetAddress fromAddress = new InternetAddress(from);
33 InternetAddress toAddress = new InternetAddress(to);
34
35 MimeMessage testMessage = new MimeMessage(mailSession);
36 testMessage.setFrom(fromAddress);
37 testMessage.addRecipient(javax.mail.Message.RecipientType.TO, toAddress);
38 testMessage.setSentDate(new java.util.Date());
39 testMessage.setSubject(subject);
40
41 // Step 3: Create a body part to hold the "text" portion of the message
42 System.out.println("Constructing 'text' body part");
43 MimeBodyPart textBodyPart = new MimeBodyPart();
44 textBodyPart.setContent(messageText,"text/html;charset=gb2312");
45
46 // Step 4: Create a body part to hold the "file" portion of the message
47 System.out.println("Attaching 'file' body part: " + fileName);
48 MimeBodyPart fileBodyPart = new MimeBodyPart();
49 FileDataSource fds = new FileDataSource("c:\\a.rar");
50 fileBodyPart.setDataHandler(new DataHandler(fds));
51 fileBodyPart.setFileName(fds.getName());
52 System.out.println("Finished attaching file");
53
54 // Step 5: Create a Multipart/container and add the parts
55 Multipart container = new MimeMultipart();
56 container.addBodyPart(textBodyPart);
57 container.addBodyPart(fileBodyPart);
58
59 // Step 6: Add the Multipart to the actual message
60 testMessage.setContent(container);
61 System.out.println("Message constructed");
62
63 // Step 7: Now send the message
64 Transport transport = mailSession.getTransport("smtp");
65 transport.connect(smtpHost, "webmaster", "password");
66 transport.sendMessage(testMessage, testMessage.getAllRecipients());
67 transport.close();
68
69
70 System.out.println("Message sent!");
71 }
72
73 public static void main(String[] args) {
74
75 String fileName = "a.rar";
76 String smtpHost = "localhost";
77 String from = "webmaster@mymail.com";
78 String to = "mfc42d@sohu.com";
79 String subject = "html邮件附件测试"; //subject javamail自动转码
80 StringBuffer theMessage = new StringBuffer();
81 theMessage.append("<h2><font color=red>这倒霉孩子</font></h2>");
82 theMessage.append("<hr>");
83 theMessage.append("<i>年年失望年年望</i>");
84
85 try {
86 SendAttachMail.sendMessage(smtpHost, from, to, subject, theMessage.toString(), fileName);
87 }
88 catch (javax.mail.MessagingException exc) {
89 exc.printStackTrace();
90 }
91 }
92 }
93
94
95
96
posted on 2005-11-03 19:34
老妖 阅读(415)
评论(0) 编辑 收藏