用 apache commons email 发送带附件,HTML 格式的 邮件
whaosoft
2009-01-08
正好要用就写上了!
1.发送普通纯文本邮件: SimpleEmail email = new SimpleEmail(); email.setHostName("SMTP服务器"); email.setAuthentication("用户名","密码"); email.addTo("收件人", "收件人名字"); email.setFrom("发件人邮件", "发件人名字"); email.setSubject("Test message"); email.setMsg("This is a simple test of commons-email"); email.send(); 2。发送带附件的邮件 EmailAttachment attachment = new EmailAttachment(); attachment.setPath("D:\\123.jpg"); attachment.setDisposition(EmailAttachment.ATTACHMENT); attachment.setDescription(MimeUtility.encodeWord("附件","UTF-8",null)); attachment.setName(MimeUtility.encodeWord("李翔李总管","UTF-8",null)); // Create the email message MultiPartEmail email = new MultiPartEmail(); email.setHostName("SMTP服务器"); email.setAuthentication("用户名","密码"); email.addTo("jdoe@somewhere.org", "John Doe"); email.setFrom("me@apache.org", "Me"); email.setSubject("The picture"); email.setMsg("Here is the picture you wanted"); // add the attachment email.attach(attachment); // send the email email.send(); 3.HTML格式邮件 HtmlEmail email = new HtmlEmail(); email.setHostName("SMTP服务器"); email.setAuthentication("用户名","密码"); email.addTo("收件人", "收件人名字"); email.setFrom("发件人邮件", "发件人名字"); email.setSubject("The picture"); URL url = new URL("http://****.gif"); String cid = email.embed(url, "Apache logo"); // set the html message email.setHtmlMsg("<html>The apache logo - <img src=\"cid:"+cid+"\"></html>"); // set the alternative message email.setTextMsg("Your email client does not support HTML messages"); // send the email email.send(); By whaosoft |
|
only_xxp
2010-06-10
学习中, 经常会用到
|