我有类似如下的...电子邮件附件?
public boolean sendmail (String host, String to, String from,
String message, String subject, String cc){
try
{
//Created TCP Connection to server
Socket s = new Socket(host, 25);
//Open our streams.
InputStream inStream = s.getInputStream();
OutputStream outStream = s.getOutputStream();
in = new Scanner(inStream);
out = new PrintWriter(outStream, true);
//get my info!
String hostName = InetAddress.getLocalHost().getHostName();
//e-mail time!
receive();
send("HELO" + host);
receive();
send("MAIL FROM: <" + from + "\n");
receive();
send("RCPT TO: <" + to + "\n");
receive();
send("DATA");
receive();
send("DATA");
receive();
//Make sure to close the everything again!!
out.close();
in.close();
s.close();
return true;
}
catch (Exception e)
{
appendtolog("ERROR: " + e);
return false;
}
}
private void send(String s) throws IOException{
appendtolog(s);
out.print(s.replaceAll("\n", "\r\n"));
out.print("\r\n");
out.flush();
}
private void receive(){
String line = in.nextLine();
appendtolog(line);
}
是有可能只是把附件的地方在那里?我知道有这样使用API的方式,但我不知道有什么方法可以让我锤附件功能合并到或使用类似..
// Set the email attachment file
MimeBodyPart attachmentPart = new MimeBodyPart();
FileDataSource fileDataSource = new FileDataSource(filename) {
@Override
public String getContentType() {
return "application/octet-stream";
}
};
attachmentPart.setDataHandler(new DataHandler(fileDataSource));
attachmentPart.setFileName(filename);
为什么不使用Java Mail API? – 2010-11-29 13:26:47
*“我意识到有更多使用API的方法,但我想知道有什么方法可以打印附件的功能......”*是的,当然可以。您也可以编写自己的操作系统。问题必须是**为什么**?! Java Mail API的使用非常简单,经过充分测试和记录良好......如果您正在做某些事情来学习SMTP的来龙去脉,但是如果您尝试使用“MimeBodyPart”这样,显然不是... – 2010-11-29 13:32:47