最近两天,很巧,又写delphi的电子邮件客户端,还写了Java的电子邮件发送的小程序,一个是工作需要,另一个则是帮朋友的忙,有点巧.
1.Java的电子有件发送程序
SendMailBean.java
package adu.com.kingsoft;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMailBean {
private String form;
private String to;
private String hostName;
private String port;
private String userName;
private String password;
private String subject;
private String body;
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPort() {
return port;
}
public void setPort(String port) {
this.port = port;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public boolean sendMail(){
Properties props=new Properties();
// props.put("mail.smtp.protocol","smtp");
props.put("mail.smtp.host",hostName);
props.put("mail.smtp.auth","true");
props.put("mail.smpt.port",port);
Session mailsession=Session.getInstance(props,null); //得到一个发送邮件的会话
Message msg=new MimeMessage(mailsession);
try{
msg.setFrom(new InternetAddress(this.getForm()));
msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(this.getTo()));
msg.setSubject(this.getSubject());
msg.setSentDate(new Date());
msg.setText(this.getBody());
Transport transport = mailsession.getTransport("smtp");
transport.connect(this.getHostName(),this.getUserName(),this.getPassword());
transport.sendMessage(msg,msg.getRecipients(Message.RecipientType.TO));
// System.out.println("邮件以成功发送到
dushengjun@gmail.com");
transport.close();
return true;
}catch(Exception e)
{
System.out.println("产生异常:"+e);
return false;
}
}
}
JavaMailApp .java
package adu.com.kingsoft;
import java.util.Date;
import java.util.Properties;
public class JavaMailApp {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("正在准备发送.....");
SendMailBean smb=new SendMailBean();
smb.setUserName("dushengjun");
smb.setForm("
dushengjun@kingsoft.net");
smb.setHostName("mail.kingsoft.net");
smb.setPassword("***************");
smb.setPort("25");
smb.setTo("
dushengjun@gmail.com");
smb.setSubject("你好");
smb.setBody("你好 Java");
if(smb.sendMail())
System.out.println("发送成功");
else
System.out.println("发送失败");
}
}
同时将如下的包加到classpath中:
activation.jar,dsn.jar,imap.jar,mailapi.jar,pop3.jar,smtp.jar
可以测试了,运行一下JavaMailApp 吧
2.Delphi的电子邮件客户端实现
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection,
IdTCPClient, IdMessageClient, IdSMTP, IdMessage, ComCtrls;
type
TForm1 = class(TForm)
idSMTP: TIdSMTP;
Button1: TButton;
idMessage: TIdMessage;
letter_subject: TEdit;
me_content: TRichEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
receiver_address: TEdit;
Label4: TLabel;
me_address: TEdit;
me_pwd: TLabel;
mypassword: TEdit;
Label5: TLabel;
me_port: TEdit;
myHostName: TEdit;
Label6: TLabel;
Edit1: TEdit;
Label7: TLabel;
Button2: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
subject :String;
rec_address :String;
self_address :String;
password :String;
content :String;
port :Integer;
hostName :String;
begin
subject:=letter_subject.Text;
rec_address:=receiver_address.Text;
self_address:=me_address.Text;
port:=StrToInt(me_port.Text);
password:=mypassword.text;
hostName:=myHostName.Text;
content:=me_content.text;
if idMessage = NIL then
idMessage.Create(Form1);
if idSMTP=NIL then
idSMTP.Create(Form1);
TIdAttachment.Create(IdMessage.MessageParts,edit1.Text);
IdMessage.Body.Assign(me_content.lines);
IdMessage.From.Text := self_address;
IdMessage.ReplyTo.EMailAddresses := self_address;
IdMessage.Recipients.EMailAddresses := rec_address;
IdMessage.Subject := subject;
idSMTP.AuthenticationType := atLogin;
// idSMTP.AuthenticationType:=atNone;
idSMTP.Username := self_address;
idSMTP.Password := password;
idSMTP.Host :=hostName;
idSMTP.Port := port;
try
idSMTP.Connect;
idSMTP.Send(idMessage);
//idSMTP.DisconnectSocket;
idSMTP.Disconnect;
//idSMTP.Destroy;
showmessage('send mail ok');
except
//idSMTP.Destroy;
//idMessage.Destroy;
showmessage('mail send false');
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
if(openDialog1.Execute) then
begin
Edit1.Text:=opendialog1.FileName;
end;
end;
end.