当前位置: 代码迷 >> Java Web开发 >> 哪位高手有javamail的例子
  详细解决方案

哪位高手有javamail的例子

热度:93   发布时间:2016-04-17 16:52:19.0
谁有javamail的例子
找了一上午网上的例子测试了都不成功   javamail.jar和activation.jar都引了
还按了个Magic   Winmail......
谁有例子发上来个   谢谢了最好有注释
我的邮箱是killjim1942@sina.com

------解决方案--------------------
Commons Emails比javamail要简化很多

Commons Emails例子:http://www.blogjava.net/martinx/archive/2005/09/29/14386.html

下面这是另外一个JAVAMAIL发邮件的例子:(SMTP)
import java.util.Properties;
import java.util.Date;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;

public class HelloWorldJavaMail {
public static void main(String[] args) {
try {
Properties props = System.getProperties();

props.put( "mail.smtp.host ", "xxx.xxx.xxx.xxx ");
Session session=Session.getDefaultInstance(props,null);
MimeMessage mimeMessage=new MimeMessage(session);

mimeMessage.setFrom(new InternetAddress( "xx@xx.co.jp ", "AA ", "iso-8859-1 "));

mimeMessage.setRecipients(Message.RecipientType.TO, "xx@xx.xx.xx ");

mimeMessage.setSubject( "Hello World JavaMail ", "iso-8859-1 ");
mimeMessage.setText( " <h1> Hello World JavaMail </h1> ", "iso-8859-1 ");
mimeMessage.setHeader( "Content-Type ", "text/html ");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}