我不行了!!快帮帮我!100分奉送!!!!!!(100分)

  • 主题发起人 主题发起人 kaisya
  • 开始时间 开始时间
K

kaisya

Unregistered / Unconfirmed
GUEST, unregistred user!
请问各位高手:
如何用java向某个信箱发e_mail?
即执行java程序完成全部发送动作,目标地址作为参数。
(不是调出OUTLOOK等软件)
 
you may ask yysun
 
到www.csdn.net去看看
 
thank you !Urlzo,I have go there but didn't find something useful;
 
www.csdn.net的专家门诊.
 
好像必须要有服务器端可执行文件支持.
否则只能通过java script加上outlook express等本地已安装的邮件软件完成.
 
好办,出个损招:
有些网页(不必登录)提供了发匿名信的办法,只需要填写对方地址,内容,确定。
这样你在JAVA中应该就可以发了。就是一个"xxxxx/pl?......post=....."
 
下面代码是我编写的SERVLET程序中的发邮件过程,供参考:
import java.io.*;
import java.net.*;
public class UserRegister extends HttpServlet
{
private static final int SMTP_PORT = 25;
private static final char SMTP_ERROR_CODE1 = '4';
private static final char SMTP_ERROR_CODE2 = '5';
private final String host = "smtpserver";//SMTP服务器名
private final Stringdo
main = "yourhostname";//当前执行JAVA程序的机器名
private final String sender = "yourname@yourdomain.com";//寄件地址
private final String recipients = "reviever@target.com";//收件地址
private final String subject = "subject";//邮件标题
protected voiddo
Post(HttpServletRequest req,
HttpServletResponse res)
throws ServletException, IOException
{
//Variables to send mail.
String maildata;
String content;//邮件内容
Vector sessionTrace = new Vector(20);
// Try to send the mail, then
the response, catching
// any IO exception.
try {//try2
// Send the mail.
maildata =
"Date: " + (new Date()).toString() + "/r/n" +
"From: " + sender + "/r/n" +
"To: " + recipients + "/r/n" +
"Subject: " + subject + "/r/n" +
"/r/n" +
content + "/r/n";
sendMail(host,do
main, sender, recipients, subject, maildata, sessionTrace);
// Send a response to the client.
out.println("<tr>");
out.println("<td><span class=font>OK! Mail send successfully!</span></td>");
out.println("</tr>");
}
catch(IOException theException) {//寄件失败
// Send a response page to the client.
out.println("<tr>");
out.println("<td><span class=font>Can't send mail!</span></td>");
out.println("</tr>");
}

}

/**
* sendMail() sends the mail.
*/
protected void sendMail(
String host,
Stringdo
main,
String sender,
String recipients,
String subject,
String maildata,
Vector sessionTrace) throws IOException {
Socket mailSocket;
BufferedReader socketIn;
DataOutputStream socketOut;
String address;
StringTokenizer tokenizer;
// Open the connection to the SMTP server, then
get references to the
// input and output streams.
mailSocket = new Socket(host, SMTP_PORT);
socketIn = new BufferedReader(
new InputStreamReader(mailSocket.getInputStream()) );
socketOut = new DataOutputStream(mailSocket.getOutputStream());
// Get the initial reply from the server.
readReply(socketIn, sessionTrace);
// Greet the server.
sendCommand(socketOut, "HELO " +do
main, sessionTrace);
readReply(socketIn, sessionTrace);
// Send the sender's address.
sendCommand(socketOut, "MAIL FROM: " + sender, sessionTrace);
readReply(socketIn, sessionTrace);
// Send the list of recipients.
tokenizer = new StringTokenizer(recipients, ",");
while (tokenizer.hasMoreElements()) {
sendCommand(socketOut, "RCPT TO: " + tokenizer.nextToken(), sessionTrace);
readReply(socketIn, sessionTrace);
}
// Start the data section.
sendCommand(socketOut, "DATA", sessionTrace);
readReply(socketIn, sessionTrace);
// Send the mail message.
sendCommand(socketOut, maildata + ".", sessionTrace);
readReply(socketIn, sessionTrace);
// End the session.
sendCommand(socketOut, "QUIT", sessionTrace);
readReply(socketIn, sessionTrace);
}
/**
* sendCommand() sends an SMTP command to the SMTP server. An
*/
private void sendCommand(DataOutputStream out, String command, Vector sessionTrace)
throws IOException {
out.writeBytes(command + "/r/n");
sessionTrace.addElement(command);
// System.out.println(command);
}
/**
* readReply() reads the reply from the SMTP server.
*/
private void readReply(BufferedReader reader, Vector sessionTrace)
throws IOException {
String reply;
char statusCode;
reply = reader.readLine();
statusCode = reply.charAt(0);
sessionTrace.addElement(reply);
// System.out.println(reply);
if ( (statusCode == SMTP_ERROR_CODE1) |
(statusCode == SMTP_ERROR_CODE2) ) {
throw (new IOException("SMTP: " + reply));
}
}
}
 
时间太久,强制结束。 wjiachun
 
你是不是用java做,如果是你可以到sun的站点上下载javamail包,就可以了,
我就是这么做的。
 

Similar threads

D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
886
DelphiTeacher的专栏
D
后退
顶部