java 实现发送Email邮件

———-发送E-Mail:

发送电子邮件,必须建立一个到端口25的Socket链接,即SMTP端口;
SMTP是:简单邮件传输协议,用于描述电子邮件消息的格式;
可以链接到任何一个提供SMTP服务的
UNIX上,该服务通常是有sendmail守护进程实现的;
大多数都内置检查功能,只接受信任的用户、域名或Ip的请求;
链接到后,可以发送一个邮件报头(SMTP格式容易生成),后面就是邮件信息!
SMTP规范(RFC 821):,每一行都要以\r再紧跟一个\n来结尾;

package com.corejava;

import .awt.*;

import .awt.event.*;

import .util.*;

import .net.*;

import .io.*;

import javax.swing.*;

/*

This program shows hot to use sockets to send plain text mail messages.

@author Cay Horstmann

@version 1.11 2007-06-25

*/

public class MailTest

{

public static void main(String[] args)

{

EventQueue.invokeLater(new Runnable()

{

public void run()

{

JFrame frame = new MailTestFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

}

});

}

}

/*

The frame for the mail GUI.

*/

class MailTestFrame extends JFrame

{

public MailTestFrame()

{

setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

setTitle(“MailTest”);

setLayout(new GridBagLayout());

// we use the GBC convenience class of Core Java Volume 1 Chapter 9

add(new JLabel(“From:”), new GBC(0, 0).setFill(GBC.HORIZONTAL));

from = new JTextField(20); // 接收发信地址

add(from, new GBC(1, 0).setFill(GBC.HORIZONTAL).setWeight(100, 0));

add(new JLabel(“To:”), new GBC(0, 1).setFill(GBC.HORIZONTAL));

to = new JTextField(20); // 接收地址

add(to, new GBC(1, 1).setFill(GBC.HORIZONTAL).setWeight(100, 0));

add(new JLabel(“SMTP server:”), new GBC(0, 2).setFill(GBC.HORIZONTAL));

smtpServer = new JTextField(20); // 地址

add(smtpServer, new GBC(1, 2).setFill(GBC.HORIZONTAL).setWeight(100, 0));

message = new JTextArea(); // 消息内容

add(new JScrollPane(message), new GBC(0, 3, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

comm = new JTextArea(); // 备注

add(new JScrollPane(comm), new GBC(0, 4, 2, 1).setFill(GBC.BOTH).setWeight(100, 100));

JPanel buttonPanel = new JPanel();

add(buttonPanel, new GBC(0, 5, 2, 1));

JButton sendButton = new JButton();

buttonPanel.add(sendButton);

sendButton.addActionListener(new ActionListener()

{

public void actionPerformed(ActionEvent event)

{

new SwingWorker()

{

protected Void doInBackground() throws Exception

{

comm.setText(“”);

sendMail();

return null;

}

}.execute();

}

});

}

/*

Sends the mail message thath has been authored in the GUI.

*/

public void sendMail()

{

try

{

Socket s = new Socket(smtpServer.getText(), 25);

InputStream inStream = s.getInputStream();

OutputStream outStream = s.getOutputStream();

in = new Scanner(inStream);

out = new PrintWriter(outStream, true /* autoFlush*/);

String hostName = InetAddress.getLocalHost().getHostName();

receive();

send(“HELLO ” + hostName);

receive();

send(“MAIL FROM: “);

receive();

send(“RCPT TO: “);

receive();

send(“DATA”);

receive();

send(message.getText());

send(“.”);

receive();

s.close();

}

catch (IOException e)

{

comm.append(“Error: ” + e);

}

}

/*

Sends a string to the socket and echoes it in the comm text area.

@param s the string to send.

*/

public void send(String s) throws IOException

{

comm.append(s);

comm.append(“\n”);

out.print(s.replaceAll(“\n”, “\r\n”));

out.print(“\r\n”);

out.flush();

}

/*

Receives a string from the socket and displays it in the comm text area.

*/

public void receive() throws IOException

{

String line = in.nextLine();

comm.append(line);

comm.append(“\n”);

}

private Scanner in;

private PrintWriter out;

private JTextField from;

private JTextField to;

private JTextField smtpServer;

private JTextArea message;

private JTextArea comm;

public static final int DEFAULT_WIDTH = 300;

public static final int DEFAULT_HEIGHT = 300;

}

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《java 实现发送Email邮件
本文地址:https://www.zhiletu.com/archives-175.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微