眼尖的先進 可以已經發現這是javaword裡的程式 但小弟覺得使用上還不是很方便 因為要先用遠端登入指令或是找一各沒帳號跟密碼的SMTP Server才能使用 小弟幫它做了一些加強 多了帳號密碼 更貼近使用者
如有其他大大想要在加強 希望也能貼上來 讓大家參考複製內容到剪貼板
代碼:
import java.util.*;
import javax.activation.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.mail.*;
import javax.mail.internet.*;
public class JavaMail extends JFrame implements ActionListener{
JFileChooser fileChooser = null;
JTextField attachment = null; //附件
JTextField server = null;
JTextField mailfrom = null;
JTextField mailto = null;
JTextField subject = null;
JTextArea text = null;
public JavaMail() {
super("JavaMail");
client();
setMessageArea();
setAttachmentFiled();
}
public void client() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
JLabel labelforServer = new JLabel("郵件伺服器: ");
server = new JTextField(50);
JLabel labelforFrom = new JLabel("寄信人: ");
mailfrom = new JTextField(50);
JLabel labelforTo = new JLabel("收信人: ");
mailto = new JTextField(50);
JLabel labelforSubject = new JLabel("標題: ");
subject = new JTextField(50);
JButton sendButton = new JButton("寄信");
sendButton.addActionListener(this);
Insets inset = new Insets(2,2,2,2); //插入
cons.insets = inset;
cons.gridx = 0; cons.gridy = 0;
panel.add(labelforServer,cons);
cons.gridx = 1; cons.gridwidth = 2;
panel.add(server,cons);
cons.gridx = 0; cons.gridy = 1;
panel.add(labelforFrom,cons);
cons.gridx = 1;
panel.add(mailfrom,cons);
cons.gridx = 0; cons.gridy = 2;
panel.add(labelforTo,cons);
cons.gridx = 1;
panel.add(mailto,cons);
cons.gridx = 0; cons.gridy = 3;
panel.add(labelforSubject,cons);
cons.gridx = 1;
panel.add(subject,cons);
cons.gridx = 0; cons.gridy = 4;
panel.add(sendButton,cons);
cons.gridx = 0; cons.gridy = 5;
getContentPane().add(panel,BorderLayout.NORTH); //預設面板位址
}
public void setMessageArea() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
panel.setBorder(new TitledBorder(
new LineBorder(Color.blue),"文字輸入區"));
text = new JTextArea();
JScrollPane js = new JScrollPane(text);
panel.add(js,BorderLayout.CENTER);
getContentPane().add(panel);
}
/*附件類別
public void setAttachmentFiled() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
GridBagConstraints cons = new GridBagConstraints();
JLabel labelforAttachment = new JLabel("附加檔案:");
attachment = new JTextField(50);
JButton openFile = new JButton("附加檔案");
'M.q5U,U%q j openFile.addActionListener(this);
Insets inset = new Insets(2,2,2,2); //取邊距
cons.insets = inset;
cons.gridx = 0; cons.gridy = 0;
panel.add(labelforAttachment,cons);
cons.gridx = 1; cons.gridwidth = 2;
cons.fill = GridBagConstraints.BOTH;
panel.add(attachment,cons);
cons.gridx = 3; cons.gridwidth = 0;
cons.fill = GridBagConstraints.NONE;
panel.add(openFile,cons);
getContentPane().add(panel,BorderLayout.SOUTH);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("附加檔案")) {
fileChooser = new JFileChooser();
fileChooser.addActionListener(this);
int choose = fileChooser.showOpenDialog(this);
if (choose == JFileChooser.APPROVE_OPTION) {
attachment.setText(fileChooser.getSelectedFile().toString());
}
}
if (e.getActionCommand().equals("寄信")) {
new Thread() { //執行緒
public void run() { //執行緒的空方法
try {
Properties props = System.getProperties();
String host = server.getText();
String from = mailfrom.getText();
String to = mailto.getText();
String contentMsg = text.getText();
props.put("mail.smtp.host",host);
props.put("mail.smtp.auth","true");
Authenticator Attester= new Attester();
Session mailSession = Session.getDefaultInstance(props,Attester);
mailSession.setDebug(true);
Message message = new MimeMessage(mailSession);
InternetAddress[] address = InternetAddress.parse(mailto.getText().trim());
message.setFrom(new InternetAddress(mailfrom.getText().trim()));
message.addRecipients(Message.RecipientType.TO,address);
message.setSubject(subject.getText()); // set Subject
// part 1 : 文字內容
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(contentMsg,"text/html;charset=big5");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// part 2 : 附件檔案
if (attachment.getText().length()!=0) {
messageBodyPart = new MimeBodyPart();
DataSource Source = new FileDataSource(attachment.getText());
messageBodyPart.setDataHandler(new DataHandler(Source)); }"
messageBodyPart.setFileName( h"Y |
attachment.getText().substring(
attachment.getText().lastIndexOf("\\")));
multipart.addBodyPart(messageBodyPart);
}
message.setContent(multipart);
Transport.send(message);
}catch (Exception ignored) {}
}
}.start(); //run()只能用開始的方法
}
}
public static void main(String[] args) {
JFrame f = new JavaMail();
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.updateComponentTreeUI(f);
} catch (Exception ignored) {}
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //框架控制
f.setBounds(100,100,800,500); //預設大小
f.show();
}
//類別:認證類別
class Attester extends Authenticator {0T2D0IG T J J p
public PasswordAuthentication getPasswordAuthentication() {
String account = JOptionPane.showInputDialog("請輸入帳號"); //選項面板
String password = JOptionPane.showInputDialog("請輸入密碼"); //有人稱對話框
return new PasswordAuthentication(account,password); //回傳
}
}