当前位置: 技术问答>java相关
如何实现打开页面即发送邮件
来源: 互联网 发布时间:2015-01-11
本文导语: 起初,我在页面上放置一个按钮,点击按钮即发送邮件。可是我想改为页面打开即发送,于是将new sendMsg(getParameter("from"),getParameter("to"), getParameter("subject"))放在init()里,可是没有用。有没有人能告诉我应该怎样调用...
起初,我在页面上放置一个按钮,点击按钮即发送邮件。可是我想改为页面打开即发送,于是将new sendMsg(getParameter("from"),getParameter("to"), getParameter("subject"))放在init()里,可是没有用。有没有人能告诉我应该怎样调用它。不胜感激。
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class MailApplet extends Applet
{ private int SMTP_PORT = 25;
//邮件服务器缺省端口号
private String appletSource = "mail.hnair.com";
//作者邮件服务器IP
private TextArea MsgArea;
private TextField senderField, recipientField, SubjectField;
public void init()
{
setLayout(new BorderLayout());
add("South", new Button("SEND"));
}
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(e);
}
public boolean action(Event e, Object arg)
{
if (arg.equals("SEND"))
new sendMsg(getParameter("from"),getParameter("to"), getParameter("subject"));
else
return super.action(e, arg);
return true;
}
}
class sendMsg
{
public void sendMas(){}
public sendMsg(String sender, String recipient, String Subject)
{ try
{
Properties props = System.getProperties();
props.put("mail.smtp.host","mail.hnair.com");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Message msg = new MimeMessage(session);
//sender=getParameter("from");
if (sender != null)
msg.setFrom(new InternetAddress(sender));
else
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
msg.setSubject(Subject);
msg.setText("content");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date());
Transport.send(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
import java.awt.*;
import java.applet.*;
import java.net.*;
import java.io.*;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
public class MailApplet extends Applet
{ private int SMTP_PORT = 25;
//邮件服务器缺省端口号
private String appletSource = "mail.hnair.com";
//作者邮件服务器IP
private TextArea MsgArea;
private TextField senderField, recipientField, SubjectField;
public void init()
{
setLayout(new BorderLayout());
add("South", new Button("SEND"));
}
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY)
System.exit(0);
return super.handleEvent(e);
}
public boolean action(Event e, Object arg)
{
if (arg.equals("SEND"))
new sendMsg(getParameter("from"),getParameter("to"), getParameter("subject"));
else
return super.action(e, arg);
return true;
}
}
class sendMsg
{
public void sendMas(){}
public sendMsg(String sender, String recipient, String Subject)
{ try
{
Properties props = System.getProperties();
props.put("mail.smtp.host","mail.hnair.com");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false);
Message msg = new MimeMessage(session);
//sender=getParameter("from");
if (sender != null)
msg.setFrom(new InternetAddress(sender));
else
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(recipient, false));
msg.setSubject(Subject);
msg.setText("content");
msg.setHeader("X-Mailer", "msgsend");
msg.setSentDate(new Date());
Transport.send(msg);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
|
给你个例子看看就知道了……
function start()
{
document.MailApplet.start();
}
function start()
{
document.MailApplet.start();
}