📄 mailedit.java
字号:
});
toolBarFontNameSize.add(cboFontSize);
cboFont.setToolTipText("字体大小");
final JButton BtnTO = new JButton();
BtnTO.setText("收件人");
frame.getContentPane().add(
BtnTO,
new CellConstraints(1, 3, CellConstraints.FILL,
CellConstraints.DEFAULT));
final JButton BtnCC = new JButton();
BtnCC.setText("抄送");
frame.getContentPane().add(
BtnCC,
new CellConstraints(1, 5, CellConstraints.FILL,
CellConstraints.DEFAULT));
TxtTo = new JTextField();
frame.getContentPane().add(TxtTo, new CellConstraints(3, 3, CellConstraints.DEFAULT, CellConstraints.FILL));
TxtCC = new JTextField();
frame.getContentPane().add(TxtCC, new CellConstraints(3, 5, CellConstraints.DEFAULT, CellConstraints.FILL));
TxtBcc = new JTextField();
frame.getContentPane().add(TxtBcc, new CellConstraints(3, 7, CellConstraints.DEFAULT, CellConstraints.FILL));
final JLabel lblBcc = new JLabel();
lblBcc.setText("BCC");
frame.getContentPane().add(
lblBcc,
new CellConstraints(1, 7, CellConstraints.CENTER,
CellConstraints.DEFAULT));
final JLabel lblSubject = new JLabel();
lblSubject.setText("主题");
frame.getContentPane().add(
lblSubject,
new CellConstraints(1, 9, CellConstraints.CENTER,
CellConstraints.DEFAULT));
TxtSubject = new JTextField();
frame.getContentPane().add(TxtSubject, new CellConstraints(3, 9, CellConstraints.DEFAULT, CellConstraints.FILL));
final JToolBar toolBar_1 = new JToolBar();
frame.getContentPane().add(toolBar_1, new CellConstraints(1, 1, 4, 1));
// 发送邮件
final JButton btnSendEmail = new JButton();
btnSendEmail.setActionCommand("SendEmail");
btnSendEmail.addActionListener(this);
btnSendEmail.setText("发送");
toolBar_1.add(btnSendEmail);
// 保存邮件
final JButton btnSaveEamil = new JButton();
btnSaveEamil.addMouseListener(new MouseAdapter() {
public void mouseClicked(final MouseEvent arg0) {
System.out.println(editorPane.getText());
// StyledDocument St=getStyledDocument(editorPane);
}
});
btnSaveEamil.setText("保存");
toolBar_1.add(btnSaveEamil);
toolBar_1.addSeparator();
//新邮件
final JButton btnNewMail = new JButton();
btnNewMail.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
editorPane.setText("");
TxtTo.setText("");
TxtBcc.setText("");
TxtCC.setText("");
TxtSubject.setText("");
cboAtt.removeAll();
cboHtmlPic.removeAll();
}
});
btnNewMail.setText("新邮件");
toolBar_1.add(btnNewMail);
toolBar_1.addSeparator();
cboPriority = new JComboBox();
cboPriority.setMaximumSize(new Dimension(100, 26));
toolBar_1.add(cboPriority);
cbotexthtml = new JComboBox();
cbotexthtml.addItemListener(new ItemListener() {
public void itemStateChanged(final ItemEvent arg0) {
if (cbotexthtml.getSelectedIndex() == 1)
editorPane.setContentType("text/html");
else
editorPane.setContentType("text/plain");
}
});
cbotexthtml.setMaximumSize(new Dimension(100, 26));
toolBar_1.add(cbotexthtml);
cbotexthtml.setVisible(false);
toolBar_1.addSeparator();
// 测试按钮
final JButton button_3 = new JButton();
button_3.setText("New JButton");
toolBar_1.add(button_3);
final JCheckBox chkBoxReply = new JCheckBox();
chkBoxReply.setText("要求回执");
toolBar_1.add(chkBoxReply);
//chkBoxReply.se
/**
*
*设置文本还是HTML格式
*/
{
final JToolBar toolBarAtt = new JToolBar();
frame.getContentPane().add(
toolBarAtt,
new CellConstraints(1, 15, 3, 1, CellConstraints.LEFT,
CellConstraints.DEFAULT));
final JButton btnAddAtt = new JButton();
btnAddAtt.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
JFileChooser fileChooser = new JFileChooser("*.jpg|*.gif");
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String name = fileChooser.getSelectedFile().getPath();// 获取选择文件的路径
if (name != null)
cboAtt.addItem(name);
}
}
});
btnAddAtt.setText("添加附件");
toolBarAtt.add(btnAddAtt);
final JButton btnDelAtt = new JButton();
btnDelAtt.addMouseListener(new MouseAdapter() {
public void mousePressed(final MouseEvent arg0) {
int nresult = JOptionPane.showConfirmDialog(scrollPane,
"确认要删除附件" + cboAtt.getSelectedItem().toString()
+ "!", "确认删除", JOptionPane.YES_NO_OPTION,
JOptionPane.INFORMATION_MESSAGE);
if (nresult == 0) {
cboAtt.removeItem(cboAtt.getSelectedItem());// JOptionPane.showMessageDialog(frame,
// "nresult["+nresult+"]!");
}
}
});
toolBarAtt.add(btnDelAtt);
btnDelAtt.setText("删除附件");
cboAtt = new JComboBox();
toolBarAtt.add(cboAtt);
cboHtmlPic = new JComboBox();
toolBarAtt.add(cboHtmlPic);
cboHtmlPic.setVisible(false);
final JProgressBar progressBar = new JProgressBar();
frame.getContentPane().add(progressBar,
new CellConstraints(1, 17, 3, 1));
}
/**
URLClassLoader urlLoader = (URLClassLoader)this.getClass().getClassLoader();
URL url = urlLoader.findResource("doc/aaaa.htm");
editorPane.setEditable(false);
try {
editorPane.setPage(url);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
frame.pack(); //
frame.setVisible(true);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getActionCommand().equals("SendEmail"))
{
if ((TxtTo.getText().length()<1) || (TxtSubject.getText().length()<1)) {
JOptionPane.showMessageDialog(scrollPane, "收件人或者主题为空!"); }
else{ //SendEmail
String cHtml=editorPane.getText();
System.out.println( cHtml);
String FoldFile; //int[] order = null;int j=0;
for (int i=cboHtmlPic.getItemCount()-1;i>=0;i--) {
FoldFile = cboHtmlPic.getItemAt(i).toString();
if (cHtml.lastIndexOf(FoldFile)==0){cboHtmlPic.remove(i);}
//order[j]=i; j++;}
}
MailSend send=new MailSend();
send.authentification=true;
send.smtpHost="smtp.126.com";
send.smtpPort="25";
send.PassWord="000229";
send.UserName="dongrujun";
send.From="dongrujun@126.com";
send.ssl=false;
send.TextHtmlType="text/html;";
send.setto(TxtTo.getText().split(";"));
if (TxtBcc.getText().length()>0)
send.setbcc(TxtBcc.getText().split(";"));
if (TxtBcc.getText().length()>0)
send.setcc(TxtBcc.getText().split(";"));
send.setSubject(TxtSubject.getText());
Vector vectorAtt=new Vector();
for (int i=0;i<cboAtt.getItemCount();i++){
vectorAtt.add(cboAtt.getItemAt(i));
}
for (int i=0;i<cboHtmlPic.getItemCount();i++){
vectorAtt.add(cboHtmlPic.getItemAt(i));//Integer.parseInt
}
send.setVAttachment(vectorAtt);
send.setPriority(Integer.toString( cboPriority.getSelectedIndex()+1));
send.setBodyText(cHtml);
try {
// btnSendEmail.setEnabled(false);
/**
SimpleDateFormat format = new SimpleDateFormat(dateformat);
String ff = format.format(new Date()); //+ ".eml";
ff=ff.replaceAll(" ", "");
ff=ff.replaceAll("-", "");
ff=ff.replaceAll(":", "");
File file = new File("c:\\"+ff+".eml");
try {
send.message.writeTo(new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("保存文件失败!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("保存文件失败!");
}
*/
send.EmailSend();
JOptionPane.showMessageDialog(scrollPane, "发送邮件成功!");
// btnSendEmail.setEnabled(true);
} catch (MessagingException e) {
JOptionPane.showMessageDialog(scrollPane, "发送邮件没有成功!");
// btnSendEmail.setEnabled(true);
e.printStackTrace();
}
}
}
if (ae.getActionCommand().equals("LoadHtml"))
{
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"HTM & HTML Files", "htm", "html");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String name = fileChooser.getSelectedFile().getPath();// 获取选择文件的路径
if (name != null)
try {
editorPane.setText("");
String url = "file:/" + name.replace("\\", "/");
editorPane.setPage(url);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(scrollPane, "加载HTML文件["
+ name + "]不成功!"); }
}
}
if (ae.getActionCommand().equals("SaveHtml"))
{
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"HTM & HTML Files", "htm", "html");
fileChooser.setFileFilter(filter);
int flag = fileChooser.showSaveDialog(scrollPane);
if (flag == JFileChooser.APPROVE_OPTION) {
File f = fileChooser.getSelectedFile();
try {
FileWriter fw = new FileWriter(f);
fw.write(editorPane.getText());
fw.close();
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(scrollPane, "保存文件失败["
+ f.getName() + "]!");
}
}
}
if (ae.getActionCommand().equals("InsertImage"))
{
JFileChooser fileChooser = new JFileChooser("*.jpg|*.gif");
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"JPG & GIF Images", "jpg", "gif");
fileChooser.setFileFilter(filter);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
String name = fileChooser.getSelectedFile().getPath();// 获取选择文件的路径
if (name != null) // JOptionPane.showMessageDialog(frame,editorPane.getText());
try {
insertIcon(new ImageIcon(name));
/** String url = "file:/" + name.replace("\\", "/"); //<HR>
insertHTML(editorPane,
"<p><img border=\"1\" src=\"" + name
+ "\"><p>", editorPane
.getSelectionStart());*/
insertHTML(editorPane,"<p><img border=\"1\" src=\"" + name+ "\"><p>");
cboHtmlPic.addItem(name);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(scrollPane, "插入文件失败!"
+ name);
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(scrollPane, "插入文件失败!"
+ name);
}
}
}
}
private void insertHTML(JEditorPane editor, String html)throws IOException, Exception {
HTMLEditorKit kit = (HTMLEditorKit) editor.getEditorKit();
HTMLDocument doc = (HTMLDocument)editor.getDocument();
kit.insertHTML( doc,editor.getCaretPosition(),html,0,0,HTML.Tag.HTML);
// StringReader reader = new StringReader(html);
// kit.read(reader, doc, location);
}
public static final void setCharacterAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
if (p0 != p1) {
StyledDocument doc = getStyledDocument(editor);
doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
}
StyledEditorKit k = getStyledEditorKit(editor);
MutableAttributeSet inputAttributes = k.getInputAttributes();
if (replace) {
inputAttributes.removeAttributes(inputAttributes);
}
inputAttributes.addAttributes(attr);
}
protected static final StyledDocument getStyledDocument(JEditorPane e) {
Document d = e.getDocument();
if (d instanceof StyledDocument) {
return (StyledDocument) d;
}
throw new IllegalArgumentException("document must be StyledDocument");
}
protected static final StyledEditorKit getStyledEditorKit(JEditorPane e) {
EditorKit k = e.getEditorKit();
if (k instanceof StyledEditorKit) {
return (StyledEditorKit) k;
}
throw new IllegalArgumentException("EditorKit must be StyledEditorKit");
}
// 对齐方式
public static final void setParagraphAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
StyledDocument doc = getStyledDocument(editor);
doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}
// 插入图片
public void insertIcon(Icon g) {
StyledEditorKit kit = getStyledEditorKit(editorPane);
MutableAttributeSet inputAttributes = kit.getInputAttributes();
inputAttributes.removeAttributes(inputAttributes);
StyleConstants.setIcon(inputAttributes, g);
replaceSelection(" ", inputAttributes.copyAttributes());
inputAttributes.removeAttributes(inputAttributes);
}
private void replaceSelection(String content, AttributeSet attr) {
StyledDocument doc = getStyledDocument(editorPane);
StyledEditorKit kit = getStyledEditorKit(editorPane);
MutableAttributeSet inputAttributes = kit.getInputAttributes();
if (doc != null) {
try {
Caret caret = editorPane.getCaret();
int p0 = Math.min(caret.getDot(), caret.getMark());
int p1 = Math.max(caret.getDot(), caret.getMark());
if (doc instanceof AbstractDocument) {
if (attr != null) {
((AbstractDocument) doc).replace(p0, p1 - p0, content,
attr);
} else {
((AbstractDocument) doc).replace(p0, p1 - p0, content,
inputAttributes);
}
} else {
if (p0 != p1) {
doc.remove(p0, p1 - p0);
}
if (content != null && content.length() > 0) {
doc.insertString(p0, content, attr != null ? attr
: inputAttributes);
}
}
} catch (BadLocationException e) {
// UIManager.getLookAndFeel().provideErrorFeedback(SendMail.this);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -