各位仁兄,那有java mail的例子

dql_77 2003-08-22 03:07:08
各位仁兄,那有java mail的例子
各位仁兄,那有java mail的例子
各位仁兄,那有java mail的例子
各位仁兄,那有java mail的例子
...全文
46 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dql_77 2003-08-25
  • 打赏
  • 举报
回复
以下的包

* JAF 1.0.2

* JavaMail 1.3.1

*在 http://java.sun.org怎么找不到下载的,
请问能说详细一点吗?谢谢!!!
icecloud 2003-08-23
  • 打赏
  • 举报
回复
使用方法:

SendMail sm = new SendMail();
sm.setHost("smtp.vip.sina.com","user","pass");
sm.setSubject("xxx");
sm.setTo("xx");
....随便选择几个set写就行...
....
out.println(sm.send()); // true or false
icecloud 2003-08-23
  • 打赏
  • 举报
回复
我今天中午刚写的:

package nona.util;


/**
* 以下的包需要下载
* JAF 1.0.2
* JavaMail 1.3.1
* http://java.sun.org
*/

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.Date;
import javax.activation.*;
import java.io.*;

/**
* 邮件发送类,用于发送邮件及附件
* 支持mime格式及Html,支持多附件
* <p>Title: 冰云工作室</p>
* <p>Description: 邮件发送</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: icecloud@sina.com</p>
* @author 冰云
* @version 1.0 August.23rd 2003
* @todo 使用者请修改Logger.log为System.out.println
*/
public class SendMail {

private MimeMessage mimeMsg; //MIME邮件对象
private Session session; //邮件会话对象
private Properties props; //系统属性
private Multipart mp; //Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象

private String authentication = "false"; //smtp是否需要认证
private String username = ""; //smtp认证用户名和密码
private String password = "";
private String from = "";
private String to = "";
private String cc = "";
private String bcc = "";
private String body = "";
private String subject = "";
private String host = "";
private String reply = "";
private String sender = "";
private String date = "";
private boolean textmail = true;

private String mailtype = PLAIN;

private static final String HTML = "text/html;charset=GB2312";
private static final String PLAIN = "text/plain; charset=GB2312";

private ArrayList attachment = new ArrayList();
/**
*
*/
public SendMail() {
this(true);
}

public SendMail(boolean textmail) {
this.textmail = textmail;
this.mailtype = textmail ? PLAIN : HTML;

}

public SendMail(String from, String to, String subject, String body) {
this();
loadDefault();
}


private boolean createMimeMessage() {
try {
// Logger.log(this,"createMimeMessage():准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); //获得邮件会话对象
}
catch (Exception e) {
Logger.logerr(this, "createMimeMessage():获取邮件会话对象时发生错误!");
Logger.logerr(this, e);
return false;
}

try {
mimeMsg = new MimeMessage(session); //创建MIME邮件对象
mp = new MimeMultipart();
return true;
}
catch (Exception e) {
Logger.logerr(this, "createMimeMessage():创建MIME邮件对象失败!");
Logger.logerr(this, e);
return false;
}
}

private boolean initMail() {
if (props == null) {
props = System.getProperties(); //获得系统属性对象
}
if ("".equals(host)) { // 设置主机
loadDefault();
}
props.put("mail.smtp.host", host); //设置SMTP主机
if (!this.createMimeMessage())
return false;


props.put("mail.smtp.auth", authentication); //设置认证
try {
if (!"".equals(subject)) // 设置标题
mimeMsg.setSubject(subject);
if (!"".equals(from)) //设置发信人
mimeMsg.setFrom(new InternetAddress(from));
if (!"".equals(to)) // 设置收信人
mimeMsg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
if (!"".equals(cc)) // 设置抄送
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(cc));
if (!"".equals(bcc)) // 设置密件抄送
mimeMsg.setRecipients(Message.RecipientType.BCC,
(Address[]) InternetAddress.parse(bcc));

if (!"".equals(reply)) //设置回复
mimeMsg.setReplyTo( (Address[]) InternetAddress.parse(reply));
// if (!"".equals(date)) //设置日期
// mimeMsg.setSentDate(new Date(date));

if (!"".equals(body)) { // 设置内容
if (!this.textmail) {
BodyPart bp = new MimeBodyPart();
bp.setContent(body, mailtype);
mp.addBodyPart(bp);
}
else {
mimeMsg.setText(body);
}
}
if (!attachment.isEmpty()) { // 设置附件
for (Iterator it = attachment.iterator(); it.hasNext(); ) {
BodyPart bpr = new MimeBodyPart();
FileDataSource fileds = new FileDataSource( (String) it.next());
bpr.setDataHandler(new DataHandler(fileds));
bpr.setFileName(fileds.getName());
mp.addBodyPart(bpr);
}
}

return true;
}
catch (Exception e) {
Logger.logerr(this, "initMail():设置邮件发生错误!");
Logger.logerr(this, e);
return false;
}
}

public boolean Send() {
try {
if (initMail()) {
if (mp.getCount() > 0)
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Logger.log(this, "正在发送邮件....");

Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect( (String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg.getAllRecipients());

Logger.log(this, "发送邮件成功!");
transport.close();

return true;
}
else {
return false;
}
}
catch (Exception e) {
Logger.logerr(this, "邮件发送失败!");
Logger.logerr(this, e);
return false;
}

}

private void loadDefault() {

}

public void setCc(String cc) {
this.cc = cc;
}

public void setBcc(String bcc) {
this.bcc = bcc;
}

public void setHost(String host, boolean auth) {
this.host = host;
this.authentication = String.valueOf(auth);
}

public void setHost(String host, boolean auth, String username,
String password) {
setHost(host, auth);
setUsername(username);
setPassword(password);

}

public void setHost(String host, String username, String password) {
setHost(host, true);
setUsername(username);
setPassword(password);
}

public void setHost(String host) {
setHost(host, true);
}

public void setFrom(String from) {
this.from = from;
}

public void setPassword(String password) {
this.password = password;
}

public void setTo(String to) {
this.to = to;
}

public void setUsername(String username) {
this.username = username;
}

public void setAttachment(String filename) {
this.attachment.add(filename);
}

public void setBody(String body) {
this.body = body;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void setReply(String reply) {
this.reply = reply;
}

public void setSender(String sender) {
this.sender = sender;
}

public static void main(String[] argc) {
if (argc.length == 0) {
System.out.println("Useage:SendMail [smtp] [user] [password] [to] [body]");
}
else {

SendMail sm = new SendMail();
sm.setHost(argc[0], argc[1], argc[2]);
sm.setTo(argc[3]);
sm.setBody(argc[4]);
if (sm.Send()) {
System.out.print("Send successful.");
}
else {
System.out.print("Send failed.");
}
}

}
}
jkit 2003-08-23
  • 打赏
  • 举报
回复
说明你根本就不急,要是真急的话,去看看前面说的那个tomcat 自带的例子,保证很快搞定。
dql_77 2003-08-23
  • 打赏
  • 举报
回复
有比较详细的吗?
我真的是初学者,请各位仁兄帮帮忙,不然我会丢掉饭碗的,谢谢!万分的感谢,急急!!急急!!急急!!急急!!急急!!急急!!急急!!
wizardho 2003-08-22
  • 打赏
  • 举报
回复
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

public class javamail
{
public static void main(String[] args)
{
try{
Properties props=new Properties();
props.put("mail.smtp.auth","true");
props.put("mail.host","smtp.21cn.com");
String username = "";
String password = " ";
MailAuthenticator ah = new MailAuthenticator(username, password);
Session mailconnection=Session.getInstance(props,ah);
Message msg=new MimeMessage(mailconnection);
Address receiver=new InternetAddress("");
msg.setFrom(new InternetAddress(""));
msg.setRecipient(Message.RecipientType.TO,receiver);
msg.setSubject("你好");
msg.setSentDate(new Date());
msg.setText("hello");
msg.setContent("hello","text/plain");
Transport.send(msg);
System.out.println("success");
}catch(Exception e){e.printStackTrace();}
}
}

class MailAuthenticator extends Authenticator
{
private String UserName;
private String Password;
public MailAuthenticator(String UserName,String Password)
{
this.UserName = UserName;
this.Password = Password;
}

//服务器要密码验证时,自动调用这个函数
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(this.UserName,this.Password);
}
}
boat2002w 2003-08-22
  • 打赏
  • 举报
回复
接问一下如何测试examples里的这个例子?
jkit 2003-08-22
  • 打赏
  • 举报
回复
tomcat 自带的例子就有, webapps\examples\jsp\mail\

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧