这个程序展示了JAVA在mail方面的能力,核心的代码如下:
package
javamail;
import javax.mail. * ;
import java.util. * ;
import javax.mail.internet. * ;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class DoWord ... {
static int nTotal = 0;//发送次数
public static void send(String _Host,String _From,String _To,String _Subject,String _Text,
String _UserName,String _Pass) ...{
try ...{
// Sina SMTP服务器: smtp.sina.com.cn
// 163 SMTP服务器: smtp.163.com
String host = _Host;//SMTP地址
String from = _From;//邮箱源地址
String to = _To;//邮箱目标地址
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// Get session
for (int i = 0; i< nTotal; i++)...{
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(_Subject);
message.setText(_Text);
message.saveChanges();
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, _UserName, _Pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
} catch (Exception ex) ...{
System.out.println(ex.toString());
}
}
public static void sendfile(String _host,String _from,String _to,String _file,String _msgtest
String _subject,String _name, String _pass) ...{
String host = _host;
String from = _from";
String to = _to;
String filename = _file;
String msgText1 = _msgtest;
String subject = _subject;
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
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);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
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);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport transport = session.getTransport("smtp");
transport.connect(host, _name, _pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException mex) ...{
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) ...{
ex.printStackTrace();
}
}
}
public static void receive(String _host,String _name,String _pass) ...{
try ...{
Session ses = Session.getDefaultInstance(System.getProperties(), null);
Store store = ses.getStore("pop3");
store.connect(_host, _name, _pass);
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
Message arraymessage[] = folder.getMessages();
for (int i = 0, j = arraymessage.length; i < j; i++) ...{
System.out.println(arraymessage[i].getSubject());
}
folder.close(false);
store.close();
} catch (Exception ex) ...{
System.out.println(ex.toString());
}
}
public static void main(String[] args) ...{
DoWord doword = new DoWord();
// doword.send() ;
// doword.sendfile();
// doword.receive() ;
}
}
import javax.mail. * ;
import java.util. * ;
import javax.mail.internet. * ;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
public class DoWord ... {
static int nTotal = 0;//发送次数
public static void send(String _Host,String _From,String _To,String _Subject,String _Text,
String _UserName,String _Pass) ...{
try ...{
// Sina SMTP服务器: smtp.sina.com.cn
// 163 SMTP服务器: smtp.163.com
String host = _Host;//SMTP地址
String from = _From;//邮箱源地址
String to = _To;//邮箱目标地址
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// Get session
for (int i = 0; i< nTotal; i++)...{
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject(_Subject);
message.setText(_Text);
message.saveChanges();
// Send message
Transport transport = session.getTransport("smtp");
transport.connect(host, _UserName, _Pass);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
} catch (Exception ex) ...{
System.out.println(ex.toString());
}
}
public static void sendfile(String _host,String _from,String _to,String _file,String _msgtest
String _subject,String _name, String _pass) ...{
String host = _host;
String from = _from";
String to = _to;
String filename = _file;
String msgText1 = _msgtest;
String subject = _subject;
// create some properties and get the default Session
Properties props = System.getProperties();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(props, null);
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);
// create and fill the first message part
MimeBodyPart mbp1 = new MimeBodyPart();
mbp1.setText(msgText1);
// create the second message part
MimeBodyPart mbp2 = new MimeBodyPart();
// attach the file to the message
FileDataSource fds = new FileDataSource(filename);
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);
// set the Date: header
msg.setSentDate(new Date());
// send the message
Transport transport = session.getTransport("smtp");
transport.connect(host, _name, _pass);
transport.sendMessage(msg, msg.getAllRecipients());
transport.close();
} catch (MessagingException mex) ...{
mex.printStackTrace();
Exception ex = null;
if ((ex = mex.getNextException()) != null) ...{
ex.printStackTrace();
}
}
}
public static void receive(String _host,String _name,String _pass) ...{
try ...{
Session ses = Session.getDefaultInstance(System.getProperties(), null);
Store store = ses.getStore("pop3");
store.connect(_host, _name, _pass);
Folder folder = store.getFolder("Inbox");
folder.open(Folder.READ_WRITE);
Message arraymessage[] = folder.getMessages();
for (int i = 0, j = arraymessage.length; i < j; i++) ...{
System.out.println(arraymessage[i].getSubject());
}
folder.close(false);
store.close();
} catch (Exception ex) ...{
System.out.println(ex.toString());
}
}
public static void main(String[] args) ...{
DoWord doword = new DoWord();
// doword.send() ;
// doword.sendfile();
// doword.receive() ;
}
}
想不到 插入代码 的功能竟是如此的强大,经过1.1415926秒的思考,我就决定用它了
这段代码已经能够基本满足简单的发送和接收邮件的需求了,JBuilder2006真是太神奇了!
选了很久觉得刘涛的照片还是很8错DI