我有原码逼我呀
Here is the code to send an attachment: import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3]; // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
Session session =
Session.getInstance(props, null); // Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment"); // create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //fill message
messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); // Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart); // Put parts in message
message.setContent(multipart); // Send the message
Transport.send( message );
}
}
Here is the code to send an attachment: import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class AttachExample {
public static void main (String args[])
throws Exception {
String host = args[0];
String from = args[1];
String to = args[2];
String fileAttachment = args[3]; // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
Session session =
Session.getInstance(props, null); // Define message
MimeMessage message =
new MimeMessage(session);
message.setFrom(
new InternetAddress(from));
message.addRecipient(
Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(
"Hello JavaMail Attachment"); // create the message part
MimeBodyPart messageBodyPart =
new MimeBodyPart(); //fill message
messageBodyPart.setText("Hi"); Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart); // Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source =
new FileDataSource(fileAttachment);
messageBodyPart.setDataHandler(
new DataHandler(source));
messageBodyPart.setFileName(fileAttachment);
multipart.addBodyPart(messageBodyPart); // Put parts in message
message.setContent(multipart); // Send the message
Transport.send( message );
}
}
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail extends Frame {
Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Label label3 = new Label();
TextArea textArea1 = new TextArea();
Label label4 = new Label();
TextField textField3 = new TextField();
Button button1 = new Button();
Button button2 = new Button(); public SendMail() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
SendMail sendMail1 = new SendMail();
sendMail1.setSize (400,400);
sendMail1.show (true);
} private void jbInit() throws Exception {
label1.setBounds(new Rectangle(41, 38, 45, 23));
label1.setText("收信人");
this.setLayout(null);
this.setSize (400,400);
textField1.setBounds(new Rectangle(110, 36, 174, 23));
label2.setBounds(new Rectangle(42, 75, 38, 23));
label2.setText("主题");
textField2.setBounds(new Rectangle(110, 76, 173, 23));
label3.setBounds(new Rectangle(43, 148, 38, 23));
label3.setText("内容");
textArea1.setBounds(new Rectangle(110, 155, 256, 170));
label4.setBounds(new Rectangle(45, 111, 44, 23));
label4.setText("附件");
textField3.setBounds(new Rectangle(110, 115, 173, 23));
button1.setBounds(new Rectangle(70, 348, 88, 24));
button1.setLabel("发送");
button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
button2.setBounds(new Rectangle(244, 348, 88, 24));
button2.setLabel("重填");
button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.add(label1, null);
this.add(textField1, null);
this.add(textField2, null);
this.add(textField3, null);
this.add(textArea1, null);
this.add(label2, null);
this.add(label4, null);
this.add(label3, null);
this.add(button2, null);
this.add(button1, null);
} void button2_actionPerformed(ActionEvent e) {
textField1.setText ("");
textField2.setText ("");
textField3.setText ("");
textArea1.setText ("");
} void button1_actionPerformed(ActionEvent e) {
String to,from,subject,message,attachment;
from="[email protected]";
to=textField1.getText ();
if(to.trim ().equals ("")){
JOptionPane.showMessageDialog(this, "收信人不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
message=textArea1.getText();
attachment=textField3.getText ();
if(message.trim ().equals ("")&&attachment.trim ().equals ("")){
JOptionPane.showMessageDialog(this, "内容和附件不能都为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if(to.indexOf ("@")==-1) {
JOptionPane.showMessageDialog(this, "无效的收信人地址!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
subject=textField2.getText ().trim ();
if(subject.equals (""))
if(JOptionPane.showConfirmDialog(this,"你不需要设置主题吗?","系统提示",0)!=0)
return;
File file=new File(attachment);
if(!attachment.equals ("")){
if(!file.isFile ()){
JOptionPane.showMessageDialog(this, "无效的附件名!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
}
//以上程序是检验输入的有效性 // create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", "192.168.0.1");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false); try{
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.addHeader ("toone","fangjianhua");
if(attachment.equals ("")){
System.out.println ("This is plain mail");
msg.setText (message);
}
else {
System.out.println ("this is a multipart mail");
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(message); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp);
}
msg.setSentDate(new Date());
// send the message
//for(int i=0;i<10;i++)
Transport.send(msg);
//System.out.println ("Send a mail success");
JOptionPane.showMessageDialog(this, "邮件发送成功", "系统提示",JOptionPane.INFORMATION_MESSAGE );
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "发送邮件失败", "错误", JOptionPane.ERROR_MESSAGE);
} }
}
form.htm
========
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>邮件例程 - JavaMail - 发送HTML邮件</title>
</head><body><table border="0" cellspacing="0" cellpadding="0">
<form method="post" action="send.jsp">
<tr>
<td>SMTP主机:</td>
<td><input type="text" name="smtp" size="80"></td>
</tr>
<tr>
<td>发信人:</td>
<td><input type="text" name="from" size="80"></td>
</tr>
<tr>
<td>收信人:</td>
<td><input type="text" name="to" size="80"></td>
</tr>
<tr>
<td>抄送人:</td>
<td><input type="text" name="cc" size="80"></td>
</tr>
<tr>
<td>暗送人:</td>
<td><input type="text" name="bcc" size="80"></td>
</tr>
<tr>
<td>主题:</td>
<td><input type="text" name="subject" size="80"></td>
</tr>
<tr>
<td valign="top">内容:</td>
<td><textarea name="body" rows="5" cols="80"></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="发送"></td>
</tr>
</form>
</table></body>
</html>send.jsp
========
<%--
作者:何志强[[email protected]]
日期:2000-08-16
版本:1.0
功能:邮件例程 - JavaMail - 发送HTML邮件
--%><%
//变量声明
java.lang.String smtp,from,to,cc,bcc,subject,body;//获得用户输入数据
smtp = request.getParameter("smtp");
from = request.getParameter("from");
to = request.getParameter("to");
cc = request.getParameter("cc");
bcc = request.getParameter("bcc");
subject = request.getParameter("subject");
if(subject!=null){
subject = new java.lang.String(subject.getBytes("iso-8859-1"));
}
body = request.getParameter("body");//发送邮件
pipi.mail.HTML.send(smtp,from,to,cc,bcc,subject,body);
%>pipi.jaf.StringDataSource.java
==============================
/*
作者:何志强[[email protected]]
日期:2000-08-16
功能:字符串型数据源
*/package pipi.jaf;public class StringDataSource implements javax.activation.DataSource{
private java.lang.String data;
private java.lang.String type; public StringDataSource(java.lang.String data,java.lang.String type){
this.data = data;
this.type = type;
} public java.io.InputStream getInputStream() throws java.io.IOException{
return new java.io.StringBufferInputStream(data);
} public java.io.OutputStream getOutputStream() throws java.io.IOException{
throw new java.io.IOException("it does not support this method now!");
} public java.lang.String getContentType(){
return type;
} public java.lang.String getName(){
return "pipi";
}
}pipi.mail.HTML.java
===================
/*
作者:何志强[[email protected]]
日期:2000-08-16
功能:发送HTML邮件
*/package pipi.mail;public final class HTML{
public static void send(
java.lang.String smtp, /*SMTP主机地址*/
java.lang.String from, /*发信人*/
java.lang.String to, /*收信人*/
java.lang.String cc, /*抄送人*/
java.lang.String bcc, /*暗送人*/
java.lang.String subject, /*主题*/
java.lang.String body /*内容*/
) throws java.lang.Exception{
//变量声明
java.util.Properties props; //系统属性
javax.mail.Session mailSession; //邮件会话对象
javax.mail.internet.MimeMessage mimeMsg; //MIME邮件对象 //设置系统属性
props = java.lang.System.getProperties(); //获得系统属性对象
props.put("mail.smtp.host",smtp); //设置SMTP主机 //获得邮件会话对象
mailSession = javax.mail.Session.getDefaultInstance(props,null); //创建MIME邮件对象
mimeMsg = new javax.mail.internet.MimeMessage(mailSession); //设置发信人
mimeMsg.setFrom(new javax.mail.internet.InternetAddress(from)); //设置收信人
if(to!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.TO,javax.mail.internet.InternetAddress.parse(to));
} //设置抄送人
if(cc!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.CC,javax.mail.internet.InternetAddress.parse(cc));
} //设置暗送人
if(bcc!=null){
mimeMsg.setRecipients(javax.mail.Message.RecipientType.BCC,javax.mail.internet.InternetAddress.parse(bcc));
} //设置邮件主题
//mimeMsg.setSubject(subject);
mimeMsg.setSubject(subject,"gb2312"); //设置邮件内容
mimeMsg.setDataHandler(new javax.activation.DataHandler(new pipi.jaf.StringDataSource(body,"text/html"))); //发送邮件
javax.mail.Transport.send(mimeMsg);
}
}本套程序使用到JavaMail和JAVABEANS(TM) ACTIVATION FRAMEWORK(JAF):
JavaMail
http://java.sun.com/products/javamail/
JAVABEANS(TM) ACTIVATION FRAMEWORK(JAF)
http://java.sun.com/products/javabeans/glasgow/jaf.html
一、我们可以通过任何支持sun规范中的sun.net.smtp包的JSP引擎(如JSWDK)发送mail。
(警告:使用内置的internal Sun规范包,这将影响到你的jsp程序的可移植性。) 以下scriptlet利用SmtpClient类在jsp文件中发送email。 <%@ page import="sun.net.smtp.SmtpClient, java.io.*" %>
<%
String from="[email protected]";
String to="[email protected], [email protected]";
try{
SmtpClient client = new SmtpClient("mail.xxxxx.xxx");
client.from(from);
client.to(to);
PrintStream message = client.startMessage();
message.println("To: " + to);
message.println("Subject: Sending email from JSP!");
message.println("This was sent from a JSP page!");
message.println();
message.println("Cool beans! :-)");
message.println();
message.println("Govind Seshadri");
message.println("jGuru.com");
message.println();
client.closeServer();
}
catch (IOException e){
System.out.println("ERROR SENDING EMAIL:"+e);
}
%>
二、 JavaMail是官方的 Java mail API,可参考 http://java.sun.com/products/javamail/。虽然该API比 sun.net.smtp.SmtpClient更丰富或者说更复杂,但它是可移植的。这里重新创建了一个 MailSender类,它包含了 JavaMail API。如下所示:
// ms_ prefix is for MailSender class variables
// str prefix is for String
// astr prefix is for array of Strings
// strbuf prefix is for StringBuffers, etc.
public MailSender(
String strFrom, // sender
String[] astrTo, // recipient(s)
String[] astrBCC, // bcc recipient(s), optional
String strSubject, // subject
boolean debugging)
{
ms_strFrom = strFrom; // who the message is from
ms_astrTo = astrTo; // who (plural) the message is to
ms_debugging = debugging; // who (plural) the message is to// set the host
Properties props = new Properties();
props.put("mail.smtp.host", ms_strSMTPHost);// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(ms_debugging);try {
// create a message
ms_msg = new MimeMessage(session);// set the from
InternetAddress from = new InternetAddress(strFrom);
ms_msg.setFrom(from);// set the to
InternetAddress[] address = new InternetAddress[astrTo.length];
for (int i = 0; i astrTo.length; ++i)
{
address[i] = new InternetAddress(astrTo[i]);
}
ms_msg.setRecipients(Message.RecipientType.TO, address);// set the bcc recipients
if (astrBCC != null)
{
address = new InternetAddress[astrBCC.length];
for (int i = 0; i astrBCC.length; ++i)
{
eh.dbg("astrBCC[" + i + "] is: '" + astrBCC[i] + "'");
address[i] = new InternetAddress(astrBCC[i]);
}
ms_msg.setRecipients(Message.RecipientType.BCC, address);
}// set the subject
ms_msg.setSubject(strSubject);// set up the string buffer which will hold the message
ms_strbufMsg = new StringBuffer();} catch (MessagingException mex) {
mex.printStackTrace(System.err);
} catch (Exception ex) {
ex.printStackTrace(System.err);
}
}public void ms_add(String strText)
{
ms_strbufMsg.append(strText);
}public void ms_send()
{
try {
// set the content as plain text
ms_msg.setContent(new String(ms_strbufMsg), "text/plain");// and away
Transport.send(ms_msg);
} catch (Exception ex) {
System.out.println("Caught exception in MailSender.ms_send: " + ex);
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMail
{
public static void main(String args[]) throws Exception
{ String host = "smtp.sina.com.cn";
String from = "[email protected]";
String to = "[email protected]";
String username = "javamail";
String password = "password"; // Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties(); // Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证 // Get session
Session session = Session.getDefaultInstance(props); // watch the mail commands go by to the mail server
session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail"); // Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
我要的是收附件的JSP源程序。
谢谢再来指导!
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SendMail extends Frame {
Label label1 = new Label();
TextField textField1 = new TextField();
Label label2 = new Label();
TextField textField2 = new TextField();
Label label3 = new Label();
TextArea textArea1 = new TextArea();
Label label4 = new Label();
TextField textField3 = new TextField();
Button button1 = new Button();
Button button2 = new Button(); public SendMail() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if(e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
public static void main(String[] args) {
SendMail sendMail1 = new SendMail();
sendMail1.setSize (400,400);
sendMail1.show (true);
} private void jbInit() throws Exception {
label1.setBounds(new Rectangle(41, 38, 45, 23));
label1.setText("收信人");
this.setLayout(null);
this.setSize (400,400);
textField1.setBounds(new Rectangle(110, 36, 174, 23));
label2.setBounds(new Rectangle(42, 75, 38, 23));
label2.setText("主题");
textField2.setBounds(new Rectangle(110, 76, 173, 23));
label3.setBounds(new Rectangle(43, 148, 38, 23));
label3.setText("内容");
textArea1.setBounds(new Rectangle(110, 155, 256, 170));
label4.setBounds(new Rectangle(45, 111, 44, 23));
label4.setText("附件");
textField3.setBounds(new Rectangle(110, 115, 173, 23));
button1.setBounds(new Rectangle(70, 348, 88, 24));
button1.setLabel("发送");
button1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {
button1_actionPerformed(e);
}
});
button2.setBounds(new Rectangle(244, 348, 88, 24));
button2.setLabel("重填");
button2.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) {
button2_actionPerformed(e);
}
});
this.add(label1, null);
this.add(textField1, null);
this.add(textField2, null);
this.add(textField3, null);
this.add(textArea1, null);
this.add(label2, null);
this.add(label4, null);
this.add(label3, null);
this.add(button2, null);
this.add(button1, null);
} void button2_actionPerformed(ActionEvent e) {
textField1.setText ("");
textField2.setText ("");
textField3.setText ("");
textArea1.setText ("");
} void button1_actionPerformed(ActionEvent e) {
String to,from,subject,message,attachment;
from="[email protected]";
to=textField1.getText ();
if(to.trim ().equals ("")){
JOptionPane.showMessageDialog(this, "收信人不能为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
message=textArea1.getText();
attachment=textField3.getText ();
if(message.trim ().equals ("")&&attachment.trim ().equals ("")){
JOptionPane.showMessageDialog(this, "内容和附件不能都为空!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
if(to.indexOf ("@")==-1) {
JOptionPane.showMessageDialog(this, "无效的收信人地址!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
subject=textField2.getText ().trim ();
if(subject.equals (""))
if(JOptionPane.showConfirmDialog(this,"你不需要设置主题吗?","系统提示",0)!=0)
return;
File file=new File(attachment);
if(!attachment.equals ("")){
if(!file.isFile ()){
JOptionPane.showMessageDialog(this, "无效的附件名!", "错误", JOptionPane.ERROR_MESSAGE);
return;
}
}
//以上程序是检验输入的有效性 // create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", "192.168.0.1");
Session session = Session.getDefaultInstance(props, null);
session.setDebug(false); try{
// create a message
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = {new InternetAddress(to)};
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.addHeader ("toone","fangjianhua");
if(attachment.equals ("")){
System.out.println ("This is plain mail");
msg.setText (message);
}
else {
System.out.println ("this is a multipart mail");
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(message); // create the second message part
MimeBodyPart mbp2 = new MimeBodyPart(); // attach the file to the message
FileDataSource fds = new FileDataSource(file);
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setFileName(fds.getName()); // create the Multipart and its parts to it
Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2); // add the Multipart to the message
msg.setContent(mp);
}
msg.setSentDate(new Date());
// send the message
//for(int i=0;i<10;i++)
Transport.send(msg);
//System.out.println ("Send a mail success");
JOptionPane.showMessageDialog(this, "邮件发送成功", "系统提示",JOptionPane.INFORMATION_MESSAGE );
}
catch(Exception ex){
JOptionPane.showMessageDialog(this, "发送邮件失败", "错误", JOptionPane.ERROR_MESSAGE);
} }
}