Notice: Undefined index: HTTP_ACCEPT_ENCODING in C:\server\web\www.showyounger.com\s-system.php on line 2
发送带图片邮件例子 | 小样儿(ShowYounger)
小样儿,顶呱呱!!![http://www.showyounger.com]
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.util.Date;
- import java.util.Properties;
-
- import javax.activation.DataHandler;
- import javax.activation.DataSource;
- import javax.activation.FileDataSource;
- import javax.mail.BodyPart;
- import javax.mail.Folder;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.SendFailedException;
- import javax.mail.Session;
- import javax.mail.Store;
- import javax.mail.URLName;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeBodyPart;
- import javax.mail.internet.MimeMessage;
- import javax.mail.internet.MimeMultipart;
-
- import com.sun.mail.smtp.SMTPAddressFailedException;
- import com.sun.mail.smtp.SMTPAddressSucceededException;
- import com.sun.mail.smtp.SMTPSendFailedException;
- import com.sun.mail.smtp.SMTPTransport;
-
- public class smtpsend {
-
- public static void main(String[] argv) {
- String from = "name@example.com", url = null;
- String mailhost = "smtp.example.com";
- String mailer = "smtpsend";
- String protocol = null, host = "smtp.example.com", user = "name", password = "psd";
- String record = null;
- boolean debug = false;
- boolean verbose = false;
- boolean auth = true;
- String prot = "smtp";
-
- try {
-
- Properties props = System.getProperties();
- if (mailhost != null)
- props.put("mail." + prot + ".host", mailhost);
- if (auth)
- props.put("mail." + prot + ".auth", "true");
-
- Session session = Session.getInstance(props, null);
- if (debug)
- session.setDebug(true);
-
- Message msg = new MimeMessage(session);
- if (from != null)
- msg.setFrom(new InternetAddress(from));
- else
- msg.setFrom();
-
- msg.setRecipients(Message.RecipientType.TO,
- InternetAddress.parse("name@example.com", false));
-
- msg.setSubject("dd");
- MimeMultipart multipart = new MimeMultipart("related");
-
- BodyPart messageBodyPart = new MimeBodyPart();
- String htmlText = "<H1>Hello</H1><img src=\"cid:image\">ddddddd";
- String htmlText1 = "<H1>Hello</H1><img src=\"cid:image1\">ddddddd";
- messageBodyPart.setContent(htmlText + htmlText1, "text/html");
-
- multipart.addBodyPart(messageBodyPart);
-
- messageBodyPart = new MimeBodyPart();
- DataSource fds = new FileDataSource
- ("E:\\aa\\bb\\icons\\incomingLinksNavigatorGroup.gif");
- messageBodyPart.setDataHandler(new DataHandler(fds));
- messageBodyPart.setHeader("Content-ID","<image>");
- multipart.addBodyPart(messageBodyPart);
- messageBodyPart = new MimeBodyPart();
- fds = new FileDataSource
- ("E:\\aa\\bb\\icons\\incomingLinksNavigatorGroup.gif");
- messageBodyPart.setDataHandler(new DataHandler(fds));
- messageBodyPart.setHeader("Content-ID","<image1>");
-
-
- multipart.addBodyPart(messageBodyPart);
-
-
- msg.setContent(multipart);
- SMTPTransport t =
- (SMTPTransport)session.getTransport(prot);
- try {
- if (auth)
- t.connect(mailhost, user, password);
- else
- t.connect();
- t.sendMessage(msg, msg.getAllRecipients());
- } finally {
- if (verbose)
- System.out.println("Response: " +
- t.getLastServerResponse());
- t.close();
- }
-
- System.out.println("\nMail was sent successfully.");
-
- if (record != null) {
-
- Store store = null;
- if (url != null) {
- URLName urln = new URLName(url);
- store = session.getStore(urln);
- store.connect();
- } else {
- if (protocol != null)
- store = session.getStore(protocol);
- else
- store = session.getStore();
-
-
- if (host != null || user != null || password != null)
- store.connect(host, user, password);
- else
- store.connect();
- }
- Folder folder = store.getFolder(record);
- if (folder == null) {
- System.err.println("Can't get record folder.");
- System.exit(1);
- }
- if (!folder.exists())
- folder.create(Folder.HOLDS_MESSAGES);
-
- Message[] msgs = new Message[1];
- msgs[0] = msg;
- folder.appendMessages(msgs);
-
- System.out.println("Mail was recorded successfully.");
- }
-
- } catch (Exception e) {
- if (e instanceof SendFailedException) {
- MessagingException sfe = (MessagingException)e;
- if (sfe instanceof SMTPSendFailedException) {
- SMTPSendFailedException ssfe =
- (SMTPSendFailedException)sfe;
- System.out.println("SMTP SEND FAILED:");
- if (verbose)
- System.out.println(ssfe.toString());
- System.out.println(" Command: " + ssfe.getCommand());
- System.out.println(" RetCode: " + ssfe.getReturnCode());
- System.out.println(" Response: " + ssfe.getMessage());
- } else {
- if (verbose)
- System.out.println("Send failed: " + sfe.toString());
- }
- Exception ne;
- while ((ne = sfe.getNextException()) != null &&
- ne instanceof MessagingException) {
- sfe = (MessagingException)ne;
- if (sfe instanceof SMTPAddressFailedException) {
- SMTPAddressFailedException ssfe =
- (SMTPAddressFailedException)sfe;
- System.out.println("ADDRESS FAILED:");
- if (verbose)
- System.out.println(ssfe.toString());
- System.out.println(" Address: " + ssfe.getAddress());
- System.out.println(" Command: " + ssfe.getCommand());
- System.out.println(" RetCode: " + ssfe.getReturnCode());
- System.out.println(" Response: " + ssfe.getMessage());
- } else if (sfe instanceof SMTPAddressSucceededException) {
- System.out.println("ADDRESS SUCCEEDED:");
- SMTPAddressSucceededException ssfe =
- (SMTPAddressSucceededException)sfe;
- if (verbose)
- System.out.println(ssfe.toString());
- System.out.println(" Address: " + ssfe.getAddress());
- System.out.println(" Command: " + ssfe.getCommand());
- System.out.println(" RetCode: " + ssfe.getReturnCode());
- System.out.println(" Response: " + ssfe.getMessage());
- }
- }
- } else {
- System.out.println("Got Exception: " + e);
- if (verbose)
- e.printStackTrace();
- }
- }
- }
-
- public static String collect(BufferedReader in) throws IOException {
- String line;
- StringBuffer sb = new StringBuffer();
- while ((line = in.readLine()) != null) {
- sb.append(line);
- sb.append("\n");
- }
- return sb.toString();
- }
- }