先把文件上传到服务器,然后在发邮件。
需要写一个上传文件部分
需要写一个上传文件部分
解决方案 »
- spring+struts1+ibatis跳转页面时出错
- DAO层的异常应该向上抛吗
- 最后的17分,求藏文字符集的问题,谢谢!
- TOMCAT 里用 jsp:include 包含一个 action 出错
- 那位好心人和我说说JBUILDER2006怎么样配置连接数据库,最好能说全一点,60分,不够还加!!
- 求JSP连接oracle7。3例子急用!!!!!!!!!在线等~~~~~~~~~!
- 关于jsp页面网页卡死现象的问题原因征集!
- Struts的简单问题!帮忙啊!
- 我真老土,还是一个关于中文显示的问题?
- 3G来了,我们能做些什么软件
- 为什么我的CPU资源被tomcat占到100%,而且不自动释放。
- <%@ include file="path"%>的使用问题
package mails;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;public class QuickMailAttach {
public static void sendMessage(String smtpHost,String from,String to,String subject,String messageText,String fileName)
throws MessagingException{
//step 1:Configure the mail session
System.out.println("Configuring mail session for:"+smtpHost);
SmtpAuth sa=new SmtpAuth();
sa.getuserinfo("??","??");
sa.getPasswordAuthentication();
java.util.Properties props=new java.util.Properties();
props.put("mail.smtp.auth","true");
props.put("mail.smtp.host",smtpHost);
Session mailSession=Session.getDefaultInstance(props,sa); //Step2:Construct the message
System.out.println("Constructing message -from="+from+" to="+to);
InternetAddress fromAddress=new InternetAddress(from);
InternetAddress toAddress=new InternetAddress(to);
MimeMessage testMessage=new MimeMessage(mailSession);
testMessage.setFrom(fromAddress);
testMessage.addRecipient(javax.mail.Message.RecipientType.TO,toAddress);
testMessage.setSentDate(new java.util.Date());
testMessage.setSubject(subject); //Step3:create a body part to hold the "text" portion of the message
System.out.println("Constructing 'text' body part");
MimeBodyPart textBodyPart=new MimeBodyPart();
textBodyPart.setText(messageText); //Step 4:Create a body part to hold the "file" portion of the message
System.out.println("Attaching 'file' body part:"+fileName);
MimeBodyPart fileBodyPart=new MimeBodyPart();
FileDataSource fds=new FileDataSource(fileName);
fileBodyPart.setDataHandler(new DataHandler(fds));
fileBodyPart.setFileName(fds.getName());
System.out.println("Finished attaching file"); //Step 5:Create a Multipart/container and add the parts
Multipart container=new MimeMultipart();
container.addBodyPart(textBodyPart);
container.addBodyPart(fileBodyPart); //Step 6:Add the Multipart to the actual messge
testMessage.setContent(container);
System.out.println("Messge constructed"); //Step 7:Now send the message
Transport.send(testMessage);
System.out.println("Message sent.");
} public static void main(String[] args){
String smtpHost="smtp.citiz.net";
String from="[email protected]";
String to="[email protected]";
String filename="D:/dd.txt";
String subject="Test message";
StringBuffer theMessage=new StringBuffer();
theMessage.append("ddddhello22,\n\n");
theMessage.append("Hope all is well on you end.\n");
theMessage.append("Cheers");
try{
QuickMailAttach.sendMessage(smtpHost,from,to,subject,theMessage.toString(),filename);
}catch(javax.mail.MessagingException exc){
exc.printStackTrace();
}
} static class SmtpAuth extends javax.mail.Authenticator {
private String user,password; public void getuserinfo(String getuser,String getpassword){
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(user,password);
}
}
}