用Commons-Email来发邮件

红薯 发布于 2008/11/21 13:17
阅读 2K+
收藏 11

Commons-Email项目旨在提供邮件发送的API,它是在JavaMail API的基础上构建的。

Some of the mail classes that are provided are as follows:

  • SimpleEmail - This class is used to send basic text based emails.
  • MultiPartEmail - This class is used to send multipart messages. This allows a text message with attachments either inline or attached.
  • HtmlEmail - This class is used to send HTML formatted emails. It has all of the capabilities as MultiPartEmail allowing attachments to be easily added. It also supports embedded images.
  • EmailAttachment - This is a simple container class to allow for easy handling of attachments. It is for use with instances of MultiPartEmail and HtmlEmail.

下面是几个使用例子:

1. 发送简单的文本邮件

SimpleEmail email = new SimpleEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
email.setSubject("Test message");
email.setMsg("This is a simple test of commons-email");
email.send();

2. 发送附件

// Create the attachment
EmailAttachment attachment = new EmailAttachment();
attachment.setPath("mypictures/john.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription("Picture of John");
attachment.setName("John");

// Create the email message
MultiPartEmail email = new MultiPartEmail();
email.setHostName("mail.myserver.com");
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邮件

// Create the email message
HtmlEmail email = new HtmlEmail();
email.setHostName("mail.myserver.com");
email.addTo("jdoe@somewhere.org", "John Doe");
email.setFrom("me@apache.org", "Me");
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("<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();

加载中
0
尚睿软件
尚睿软件

请教老大有没有接收邮件封装的开源包?

0
小黑小别姬
小黑小别姬

引用来自“尤胜荣”的答案

请教老大有没有接收邮件封装的开源包?

这个已经很简单了,还封装干什么啊?

0
尚睿软件
尚睿软件

引用来自“花花花花朵”的答案

引用来自“尤胜荣”的答案

请教老大有没有接收邮件封装的开源包?

这个已经很简单了,还封装干什么啊?

这个只是用来发送邮件的,不是接收
0
小黑小别姬
小黑小别姬

引用来自“尤胜荣”的答案

引用来自“花花花花朵”的答案

引用来自“尤胜荣”的答案

请教老大有没有接收邮件封装的开源包?

这个已经很简单了,还封装干什么啊?

这个只是用来发送邮件的,不是接收
jodd
0
jack520009
jack520009

java使用企业邮箱发送老是失败是因为什么?

/**
* 简单邮件发送
* @param toUser
* @param subject
* @param msg
*/
public static void send(String toUser, String subject,String msg) {
System.out.println("发送邮件给:" + toUser);
System.out.println("邮件主题为:" + subject);
System.out.println("邮件内容为:" + msg);
config = PropertiesFactory.getPropertiesHelper(PropertiesFile.APP);

SimpleEmail email = new SimpleEmail();
email.setHostName("smtp.exmail.qq.com");
email.setAuthentication("sss@iduo.tv","12345");
//email.setSSL(Boolean.TRUE);
//email.setSslSmtpPort("465");
email.setCharset("UTF-8");
try {
email.addTo(toUser);
email.setFrom("sss@iduo.tv");// 必须和Authentication使用的用户相同,否则失败
email.setSubject(subject);
email.setMsg(msg);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}


错误信息:

org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp.exmail.qq.com:25
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1410)
at org.apache.commons.mail.Email.send(Email.java:1437)
at com.csmy.my.center.util.mail.EmailUtil.send(EmailUtil.java:44)
at com.csmy.my.center.util.mail.EmailUtil.main(EmailUtil.java:53)
Caused by: javax.mail.AuthenticationFailedException: 454 Error: authentication failed, system busy


at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:823)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:756)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:673)
at javax.mail.Service.connect(Service.java:317)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1400)
... 3 more

bolatu836125
bolatu836125
端口不是25吧
返回顶部
顶部