Be the change you want to see in the world
  • 2009-06-09使用apache.commons.mail 发送邮件(2)

    版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
    http://aisiteru.blogbus.com/logs/40780919.html

    3.添加附件:

    package mailTest;
    import org.apache.commons.mail.EmailAttachment;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.MultiPartEmail;
    public class SendingEmailsWithAttachments {
    /**
    * @param args
    * @throws EmailException
    */

    public static void main(String[] args) throws EmailException {
    // Create the attachment
    EmailAttachment attachment = new EmailAttachment();
    attachment.setPath("D:\\workspace\\JavaMail\\src\\mailTest\\Motion_Stripes.jpg");
    attachment.setDisposition(EmailAttachment.ATTACHMENT);
    attachment.setDescription("Picture of John");
    attachment.setName("John.jpg");
    // Create the email message
    MultiPartEmail email = new MultiPartEmail();
    email.setDebug(true);
    email.setHostName("smtp.163.com");
    email.addTo("xxx@163.com", "John Doe");
    email.setFrom("xxx@163.com", "Me");
    email.setAuthentication("xxx", "xxx");
    email.setSubject("The picture");
    email.setMsg("Here is the picture you wanted");
    // add the attachment
    email.attach(attachment);
    // send the email
    email.send();
    System.out.println("The SimpleEmail send sucessful!!!");
    }
    }

    4.自定义格式(?)

    package mailTest;
    import java.net.MalformedURLException;
    import java.net.URL;
    import org.apache.commons.mail.EmailException;
    import org.apache.commons.mail.HtmlEmail;
    public class SendingHTMLFormattedEmail {
    /**
    * @param args
    * @throws EmailException
    * @throws MalformedURLException
    */

    public static void main(String[] args) throws EmailException,
    MalformedURLException {
    // Create the email message
    HtmlEmail email = new HtmlEmail();
    email.setHostName("smtp.163.com");
    email.addTo("xxx@163.com", "John Doe");
    email.setFrom("xxx@163.com", "Me");
    email.setAuthentication("xxx", "xxx");
    email.setSubject("Test email with inline image");
    // embed the image and get the content id
    URL url = new URL("http://www.apache.org/images/asf_logo_wide.gif");
    String cid = email.embed(url, "Apache logo");
    // set the html message
    email.setHtmlMsg("The apache logo - <img src="\" alt="" />");
    // set the alternative message
    email.setTextMsg("Your email client does not support HTML messages");
    // send the email
    email.send();
    System.out.println("The SimpleEmail send sucessful!!!");
    }
    }

    收藏到:Del.icio.us




    Subscribe

    Creative Commons License本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。